Undefined reference to cv::imread

2019-09-10 06:35发布

问题:

I know this has been asked several times but still I am struggling with linker error undefined reference for simple opencv code. This is my code:

#include <opencv2/highgui.hpp>
using namespace cv;

int main(int argc, char** argv) {
    Mat inputImage = imread(argv[1]);
    imshow("Input Image", inputImage);

    waitKey(0);
}

I am using eclipse IDE, cygwin g++ compiler and opencv 3.0. I have correctly given include path, library search path as "C:\opencv\build\x64\vc11\lib" & "C:\opencv\build\x64\vc11\staticlib" and libraries: opencv_highgui300,opencv_highgui300d,opencv_core300,opencv_core300d,opencv_imgcodecs300,opencv_imgcodecs300d and I think these are quite sufficient for this simple code as clear from opencv documentation. But still getting linker error for imread, imshow, waitkey as mentioned. I tried with pkg-config but pkg-config is giving problem on my 64 bit machine: error while loading shared libraries and couldn't solve that either.

Below is build output:
make all
Building file: ../ShowImage.cpp
Invoking: Cygwin C++ Compiler
g++ -I"C:\opencv\build\include\opencv" -I"C:\opencv\build\include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"ShowImage.d" -MT"ShowImage.d" -o "ShowImage.o" "../ShowImage.cpp" Finished building: ../ShowImage.cpp

Building target: Test.exe
Invoking: Cygwin C++ Linker
g++ -L"C:\opencv\build\x64\vc11\lib" -L"C:\opencv\build\x64\vc11\staticlib" -o "Test.exe" ./ShowImage.o -lopencv_highgui300 -lopencv_highgui300d -lopencv_core300 -lopencv_core300d -lopencv_imgcodecs300 -lopencv_imgcodecs300d

./ShowImage.o: In function main':* */cygdrive/d/Gateway_Firmware/ImProcessing_WS/Test/Debug/../ShowImage.cpp:12: undefined reference tocv::imread(cv::String const&, int)' /cygdrive/d/Gateway_Firmware/ImProcessing_WS/Test/Debug/../ShowImage.cpp:12:(.text+0x44): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `cv::imread(cv::String const&, int)'

Am I missing anything? Any help will be greatly appreciated.

回答1:

In case anyone stumbles upon this for other reasons, I have had the problem where OpenCV link options on an x86 Ubuntu 18.04 docker container differ from those on an Ubuntu 16.04 Raspberry Pi, and I solved the problem by replacing the linker flag -lopencv_imgcodecs with the linker flag -lopencv_highgui.