0

I want compile and this example of OpenCV : https://docs.opencv.org/ref/master/d3/d50/group__imgproc__colormap.html

When I want to compile : g++ myfile.cxx -o myfile I get this error :

fatal error opencv2/core.hpp not such file or directory

So I open the .bashrc and I add that : export PATH=${PATH}:/usr/local/include/opencv4 and when I compile I do now that : g++ myfile.cxx -o myfile -I/usr/include/opencv4

And now I get many error about undefined reference :

/usr/bin/ld : /tmp/ccISlbHF.o : dans la fonction « main » :
tryopencv.cxx:(.text+0xb1) : référence indéfinie vers « cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int) »
/usr/bin/ld : tryopencv.cxx:(.text+0x195) : référence indéfinie vers « cv::applyColorMap(cv::_InputArray const&, cv::_OutputArray const&, int) »
/usr/bin/ld : tryopencv.cxx:(.text+0x20e) : référence indéfinie vers « cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&) »
/usr/bin/ld : tryopencv.cxx:(.text+0x245) : référence indéfinie vers « cv::waitKey(int) »
/usr/bin/ld : /tmp/ccISlbHF.o : dans la fonction « cv::Mat::~Mat() » :
tryopencv.cxx:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3d) : référence indéfinie vers « cv::fastFree(void*) »
/usr/bin/ld : /tmp/ccISlbHF.o : dans la fonction « cv::Mat::release() » :
tryopencv.cxx:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4f) : référence indéfinie vers « cv::Mat::deallocate() »
collect2: error: ld returned 1 exit status

I search on many forums and many people speak about this : pkg-config opencv --cflags --libs I need to put in this command opencv4 and not opencv because it doesn't work and with opencv4 I get this :

-I/usr/include/opencv4/opencv -I/usr/include/opencv4 -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_shape -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_ml -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core

I'm a little lost: How can I fix the problem to compile and test with OpenCV?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Newbi
  • 31
  • 6
  • The output of the pkg-config command is the options you are supposed to use when you compile your program. I.e. `g++ myfile.cxx -o myfile $(pkg-config opencv4 --cflags --libs)` – john Jul 18 '20 at 18:59
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Thomas Sablik Jul 18 '20 at 19:07
  • Does this answer your question? [opencv not working for c++ on ubuntu 20.04](https://stackoverflow.com/questions/62968443/opencv-not-working-for-c-on-ubuntu-20-04#comment111355417_62968443) – Thomas Sablik Jul 18 '20 at 19:07
  • Thanks you very much all of your answer ! I don't put the $ and the () between the command that's why it doesn't work but now it works thanks for you very much guys ! – Newbi Jul 18 '20 at 21:28

2 Answers2

2

You can use pkg-config in the compile command

g++ myfile.cxx -o myfile `pkg-config opencv4 --cflags --libs`

It will set the include paths and libraries

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • Yes, I forgot the $ and the () or the `` between the commands that's why the command doesn't work ! Thanks you very much ! – Newbi Jul 18 '20 at 21:29
0

You have to add the libraries to the compiler command (-l parameters):

g++ myfile.cxx -o myfile  -I/usr/include/opencv4 -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_shape -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_ml -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core

The needed ones, may be not all of them.

Manuel
  • 2,526
  • 1
  • 13
  • 17
  • Yes, thanks for your answer ! The others comments proposes to put `pkg-config opencv4 --cflags --libs` or $(pkg-config opencv4 --cflags --libs) instead of the parameters it's more clear on the terminal but thanks you very much ! – Newbi Jul 18 '20 at 21:31
  • @Newbi yes, using `pkg-config` is easier and safer (as includes and libs could change on software upgrades). You should accept @ThomasSabik response. My response was only to show you there are linker options for the libraries also. – Manuel Jul 20 '20 at 11:27