我正在尝试使用 Ubuntu Quantal 中的 OpenCV 编译一个简单的程序。我已经安装了所有可用的 OpenCV 软件包。我的代码如下
#include <stdio.h>
#include <highgui.h>
#include <cv.h>
int main(int argc, char *argv[]) {
IplImage* img=0; /* pointer to an image */
printf("Hello\n");
if(argv[1] != 0)
img = cvLoadImage(argv[1], 0); // 1 for color
else
printf("Enter filename\n");
if(img != 0) {
cvNamedWindow("Display", CV_WINDOW_AUTOSIZE); // create a window
cvShowImage("Display", img); // show image in window
cvWaitKey(0); // wait until user hits a key
cvDestroyWindow("Display");
}
else
printf("File not found\n");
return 0;
}
我的编译命令是g++ -I/usr/include/opencv -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann main.cpp -o main
我收到以下错误
/tmp/ccUQ4Tm4.o: In function `main':
main.cpp:(.text+0x45): undefined reference to `cvLoadImage'
main.cpp:(.text+0x6b): undefined reference to `cvNamedWindow'
main.cpp:(.text+0x7c): undefined reference to `cvShowImage'
main.cpp:(.text+0x86): undefined reference to `cvWaitKey'
main.cpp:(.text+0x90): undefined reference to `cvDestroyWindow'
collect2: ld returned 1 exit status
make: *** [all] Error 1
我从上述错误中了解到,找到库没有问题,但编译器找不到其中的函数。你能帮我解决这个问题吗?
答案1
最后包含所有库。
例如
g++ -I/usr/include/opencv main.cpp -o main -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
它会解析符号,因为它按照在命令行上写入的相同顺序找到库。