解决 undefined reference to cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>,....

When using opencv, you may encounter undefined reference to cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
the main reason is that the version of the function used is different from the version of the referenced library function. Make sure that the version of the function used is consistent with the version of the referenced library function.

1. How to know whether the function version is the same as the one referenced

1.1 Use nm to analyze the target file

  1. Generate .o file
g++ -E -I/usr/local/include/opencv4/ -L/usr/local/lib -lopencv_highgui  -lopencv_imgcodecs -lopencv_imgproc -lopencv_core  /home/lvrobot/OpencvStudy/fangwenyuansu.cpp -o /home/lvrobot/OpencvStudy/fangwenyuansu.i  //预处理
g++ -S -I/usr/local/include/opencv4/ -L/usr/local/lib -lopencv_highgui  -lopencv_imgcodecs -lopencv_imgproc -lopencv_core  /home/lvrobot/OpencvStudy/fangwenyuansu.i -o /home/lvrobot/OpencvStudy/fangwenyuansu.s //编译
g++ -c -I/usr/local/include/opencv4/ -L/usr/local/lib -lopencv_highgui  -lopencv_imgcodecs -lopencv_imgproc -lopencv_core  /home/lvrobot/OpencvStudy/fangwenyuansu.s -o /home/lvrobot/OpencvStudy/fangwenyuansu.o  //汇编
  1. Use nm -C fangwenyuansu.oto see the external functions used
U cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)
U cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)
  1. Check the function type corresponding to the reference library, here is mainly to check libopencv_imgcodecs.a, libopencv_highgui.a (according to the function used)nm -C libopencv_highgui.a/ nm -C libopencv_imgcodecs.a
 U cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)
 T cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)

Mainly check whether the function signature is consistent.

2. Solve the problem

2.1. Inconsistent function versions

Inconsistent versions of C++ and gcc compilers used for compilation will lead to inconsistent function versions

According to the information, the following methods can be solved

  • Just add -D_GLIBCXX_USE_CXX11_ABI=0 when compiling (refer to the -Dmacro=defn part of "g++ Command Line Parameters").
  • Add a macro definition in each source file: #define _GLIBCXX_USE_CXX11_ABI 0.
    You can try it yourself
  • Recompile in the local environment to ensure that the compiler for compiling opencv is the same as the one used in the environment

2.2. The function version is the same, and there are multiple opencv in the environment, which leads to the mismatch between the header file and the library used

编译opencv时开启-D OPENCV_GENERATE_PKGCONFIG=ON
(cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_GENERATE_PKGCONFIG=ON -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH= …/opencv4/opencv_contrib/modules -D OPENCV_EXAMPLES=ON …)

Use to pkg-config --cflags opencv4get opencv header file path

-I/usr/local/include/opencv4

pkg-config --libs opencv4Get the library path using

-L/usr/local/lib -lopencv_gapi -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_video -lopencv_calib3d -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_core

All can be added, it is best to select the reference according to the function used

2.3. When there is no problem with the corresponding function version and path, you should pay attention to the path of the reference library

correct compile command

g++ -g -I/usr/local/include/opencv4/ /home/lvrobot/OpencvStudy/fangwenyuansu.cpp -o /home/lvrobot/OpencvStudy/fangwenyuansu -L/usr/local/lib -lopencv_highgui  -lopencv_imgcodecs -lopencv_imgproc -lopencv_core  

insert image description here
Incorrect compile command (wrong location of referenced library)

g++ -g -I/usr/local/include/opencv4/ -L/usr/local/lib -lopencv_highgui  -lopencv_imgcodecs -lopencv_imgproc -lopencv_core /home/lvrobot/OpencvStudy/fangwenyuansu.cpp -o /home/lvrobot/OpencvStudy/fangwenyuansu 

insert image description here
Pay attention to this too

Guess you like

Origin blog.csdn.net/u011573853/article/details/126345418