2014-04-08 59 views
1

我是新來打開cv。目前我正在嘗試測試我是否可以運行一個簡單的文件。試圖編譯一個簡單的opencv C++文件

// Example showing how to read and write images 
#include <opencv2/opencv.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv/cvaux.h> 

int main(int argc, char** argv) 
{ 
    IplImage * pInpImg = 0; 

    // Load an image from file - change this based on your image name 
    pInpImg = cvLoadImage("bear.jpg", CV_LOAD_IMAGE_UNCHANGED); 
    if(!pInpImg) 
    { 
    fprintf(stderr, "failed to load input image\n"); 
    return -1; 
    } 

    // Write the image to a file with a different name, 
    // using a different image format -- .png instead of .jpg 
    if(!cvSaveImage("my_image_copy.png", pInpImg)) 
    { 
    fprintf(stderr, "failed to write image file\n"); 
    } 

    // Remember to free image memory after using it! 
    cvReleaseImage(&pInpImg); 

    return 0; 
} 

我編譯它:

g++ `pkg-config –cflags opencv` cv.cpp -o cv `pkg-config –libs opencv` 

我得到這個錯誤:

Undefined symbols for architecture x86_64: 
    "_cvLoadImage", referenced from: 
     _main in cv-zQ5X30.o 
    "_cvReleaseImage", referenced from: 
     _main in cv-zQ5X30.o 
    "_cvSaveImage", referenced from: 
     _main in cv-zQ5X30.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

我丟在這裏,任何幫助將是非常讚賞。 謝謝。

+0

你在Mac上? –

+0

是的,我在MacOSX 10.8.5 – drifterOcean19

+0

@ drifterOcean19嘗試編譯與-m32標誌添加。它看起來像你想編譯一個64位應用程序,並連接一個32位的OpenCV庫G ++'pkg配置-cflags opencv' cv.cpp -m32 -o CV'pkg配置-libs opencv' –

回答

1

我認爲這個問題可能會使用「-libs」和「-cflags」,而不是「--libs」和「--cflags」,而不是使用:

g++ `pkg-config --cflags opencv` cv.cpp -o cv `pkg-config --libs opencv` 
+0

我試過並得到了鏗鏘聲:錯誤:不支持的選項'--cflags' 鏗鏘聲:錯誤:不支持的選項'--libs' 鏗鏘聲:錯誤:沒有這樣的文件或目錄:'pkg-config' clang:error:沒有這樣的文件或目錄:'opencv' clang:error:no such file or directory:'pkg-config' clang:error:no such file or directory:'opencv' – drifterOcean19

+0

@KeillRandor我認爲您需要實際執行'pkg-config'部分,像這樣'$(pkg-config --cflags opencv)'和'$(pkg-config --libs opencv)'。 –

+0

對不起,我忘了引號(如在你的文章中)我會編輯答案.. –