Marshall S. Lee Marshall S. Lee - 1 month ago 14
C Question

OpenCV: "Undefined symbols for architecture x86_64:" while attempting to compile

Environment:
OS: macOS,
OpenCV: 2.4.12

I just started learning OpenCV, and I wrote down a code from a book, which is,

#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;
}


When I tried to compile this code with this command -
gcc opencv1.c -o opencv1
, it showed an error with the following message.

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)


Does anybody know the solution?

Answer

You need to include the OpenCV libraries when compiling, pkg-config can help:

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