3

I am trying to build a sample from OpenCV. Unfortunately I always get these errors while building.

I made an installation with opencv_contrib. OpenCV is installed in ~/lib/pokus/installed (contains bin include lib share). I am buiding example ~/lib/pokus/installed/share/OpenCV/samples/cpp/opencv_version.cpp (which came with OpenCV).

My g++ command:

g++ $(pkg-config --cflags --libs ~/lib/pokus/installed/lib/pkgconfig/opencv.pc) opencv_version.cpp -o test

Result:

opencv_version.cpp:(.text+0x78): undefined reference to `cv::CommandLineParser::CommandLineParser(int, char const* const*, cv::String const&)'
opencv_version.cpp:(.text+0xa8): undefined reference to `cv::CommandLineParser::has(cv::String const&) const'
opencv_version.cpp:(.text+0xc6): undefined reference to `cv::CommandLineParser::printMessage() const'
opencv_version.cpp:(.text+0xd7): undefined reference to `cv::CommandLineParser::check() const'
opencv_version.cpp:(.text+0xea): undefined reference to `cv::CommandLineParser::printErrors() const'
opencv_version.cpp:(.text+0x113): undefined reference to `cv::CommandLineParser::has(cv::String const&) const'
opencv_version.cpp:(.text+0x12a): undefined reference to `cv::getBuildInformation()'
opencv_version.cpp:(.text+0x180): undefined reference to `cv::CommandLineParser::~CommandLineParser()'
opencv_version.cpp:(.text+0x1bc): undefined reference to `cv::CommandLineParser::~CommandLineParser()'
opencv_version.cpp:(.text+0x1f8): undefined reference to `cv::CommandLineParser::~CommandLineParser()'
/tmp/ccRHDRg5.o: In function `cv::String::String(char const*)':
opencv_version.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to `cv::String::allocate(unsigned long)'
/tmp/ccRHDRg5.o: In function `cv::String::~String()':
opencv_version.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status

Content of ~/lib/pokus/installed/lib/pkgconfig/opencv.pc

# Package Information for pkg-config
prefix=~/lib/pokus/installed
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 3.2.0
Libs: -L${exec_prefix}/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cvv -lopencv_dpm -lopencv_freetype -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_plot -lopencv_dnn -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core
Libs.private: -L/usr/lib/x86_64-linux-gnu -lQt5Test -lQt5Concurrent -lQt5OpenGL -lpng -lz -ltiff -ljasper -ljpeg -lImath -lIlmImf -lIex -lHalf -lIlmThread -ldc1394 -lavcodec-ffmpeg -lavformat-ffmpeg -lavutil-ffmpeg -lswscale-ffmpeg -lQt5Core -lQt5Gui -lQt5Widgets -ldl -lm -lpthread -lrt -lGLU -lGL -ltbb
Cflags: -I${includedir_old} -I${includedir_new}

Sorry for so frequently asked question, but none of the others seems to solve my problem. Thanks for all tips!

JankaSvK
  • 51
  • 5
  • See [this answer](http://stackoverflow.com/a/43305704/1362568) to [this question](http://stackoverflow.com/q/43264100/1362568) – Mike Kinghan Apr 09 '17 at 10:36
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Mike Kinghan Apr 09 '17 at 10:36

2 Answers2

2

The file name must come before the params:

g++  opencv_version.cpp  $(pkg-config --cflags --libs ~/lib/pokus/installed/lib/pkgconfig/opencv.pc) -o test
1

I had the same issue trying to work with OPENCV 4.3.0 after installation and so. In order to add the libs and compile properly.

Worked for me in order to test the opencv_version.cpp

g++  opencv_version.cpp  $(pkg-config --cflags --libs opencv4) -o opencv_version

Result

./opencv_version
Welcome to OpenCV 4.3.0
Hugo Mallo
  • 11
  • 2