OpenCV static linking error

2019-05-31 04:24发布

I want to use OpenCV as a static library. After compiling the library as a static library now i'm testing with some examples to see all the dependencies.

The dependencies added to compile are -staticpkg-config --libs opencv-lpthread -ljpeg -ltiff -lz -ljasper -lpng12

But obtain the following Error message:

g++ -c -g -Wall -O2 `pkg-config --cflags opencv` formas.cpp 
g++ -g -Wall -O2 `pkg-config --cflags opencv` formas.o -o paint -static `pkg-config --libs opencv` -lpthread -ljpeg -ltiff -lz -ljasper -lpng12 
/usr/local/lib/libopencv_core.a(system.o): In function `cv::tempfile(char const*)': 
system.cpp:(.text._ZN2cv8tempfileEPKc+0x3a): warning: the use of `tmpnam' is dangerous, better use `mkstemp' 
/usr/local/lib/libopencv_core.a(system.o): In function `cv::getTickCount()': 
system.cpp:(.text._ZN2cv12getTickCountEv+0x2a): undefined reference to `clock_gettime' 
/usr/local/lib/libopencv_highgui.a(grfmt_png.o): In function `cv::PngDecoder::readHeader()': 
grfmt_png.cpp:(.text._ZN2cv10PngDecoder10readHeaderEv+0xde): undefined reference to `png_set_longjmp_fn' 
/usr/local/lib/libopencv_highgui.a(grfmt_png.o): In function `cv::PngEncoder::write(cv::Mat const&, std::vector<int, std::allocator<int> > const&)': 
grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x134): undefined reference to `png_set_longjmp_fn' 
/usr/local/lib/libopencv_highgui.a(grfmt_png.o): In function `cv::PngDecoder::readData(cv::Mat&)': 
grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x141): undefined reference to `png_set_longjmp_fn' 
collect2: ld returned 1 exit status 
make: *** [all] Error 1

I made something wrong or there an error in OpenCV library.

OpenCV version: 2.3.1 (latest version) formas --> example which create an image an draw some figures and save into a file. So simple.

Thanks in advance.

1条回答
孤傲高冷的网名
2楼-- · 2019-05-31 04:47

It seems you have compiled OpenCV with libpng 1.4 or newer but trying to link with libpng 1.2

What cmake command have you used to configure OpenCV?

Update:

You need to add -lrt to the list of libraries you link for the clock_gettime function.

查看更多
登录 后发表回答