OpenCV:"架构x86_64的未定义符号:"在尝试编译时

时间:2016-10-20 07:45:39

标签: c opencv

环境: 操作系统:macOS, OpenCV:2.4.12

我刚刚开始学习OpenCV,我从一本书中写下了一段代码,即

#include <opencv/highgui.h>

int main(int argc, char **argv) {
        int iscolor = -1;
        IplImage* img = cvLoadImage(argv[1], iscolor);
        cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
        cvShowImage("Example1", img);
        cvWaitKey(0);

        cvReleaseImage(&img);
        cvDestroyWindow("Example1");
        return 0;
}

当我尝试使用此命令(gcc opencv1.c -o opencv1)编译此代码时,显示以下消息时出现错误。

Undefined symbols for architecture x86_64:
  "_cvDestroyWindow", referenced from:
      _main in opencv1-78cabd.o
  "_cvLoadImage", referenced from:
      _main in opencv1-78cabd.o
  "_cvNamedWindow", referenced from:
      _main in opencv1-78cabd.o
  "_cvReleaseImage", referenced from:
      _main in opencv1-78cabd.o
  "_cvShowImage", referenced from:
      _main in opencv1-78cabd.o
  "_cvWaitKey", referenced from:
      _main in opencv1-78cabd.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

有人知道解决方案吗?

1 个答案:

答案 0 :(得分:2)

编译时需要包含OpenCV库,pkg-config可以提供帮助:

gcc `pkg-config --cflags opencv` `pkg-config --libs opencv` opencv1.c -o opencv1