Mac下用g++编译opencv程序报错

时间:2022-02-07 18:45:22

问题描述:

在Mac下安装好opencv, 安装:

 

  bash  brew install opencv  

 

 写了一个opencv程序:

 

 ``` C++

//作用就是:取视频的每一帧,然后每隔50帧保存一次。

include

include "opencv2/opencv.hpp"

using namespace cv;

int main(int argc, char argv[])

{

CvCapture
capture = cvCaptureFromAVI("./test.mp4");

int i = 0;

IplImage* img = 0;

char image_name[25];

cvNamedWindow( "vivi");

int nFrame = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);

while (1) {
img = cvQueryFrame(capture);
cvShowImage("vivi", img);
char key = cvWaitKey(20); std::cout << i << std::endl; i++;
if (i % 50 != 0)
continue; sprintf(image_name, "%s%d%s", "image_", i, "_.jpg"); cvSaveImage(image_name, img); if ( i == nFrame ) {
break;
}
} return 0;

```

编辑好后保存为Test.cpp

编译: g++ Test.cpp -o Test

错误:

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

大概知道是链接错误,google以后发现,是因为找到不到相应的opencv所需要的库。

解决办法:

利用pkg-config 获取相应的库, 安装命令:
brew install pkg-config
重新安装opencv
brew reinstall opencv
编译程序
g++ $(pkg-config --cflags --libs opencv) Test.cpp -o Test
可以查看具体引用库
pkg-config --cflags --libs opencv

-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core -lopencv_hal

参考链接:

http://*.com/questions/24985713/opencv-undefined-symbols-for-architecture-x86-64-error