1

I have a very basic program to test that OpenCV works.

#include <opencv2/opencv.hpp>

int main() {
    cv::namedWindow("Window", CV_WINDOW_AUTOSIZE);

    cv::VideoCapture video_capture;
    video_capture.open(0);

    while (true) {
        cv::Mat image;
        video_capture.read(image);

        cv::imshow("Window", image);

        if (cv::waitKey(1) == 27) {
            break;
        }
    }

    return 0;
}

And a CMakeLists.txt file that finds OpenCV like so:

find_package(OpenCV REQUIRED)
...
target_link_libraries(Project ${OpenCV_LIBS})

This works on Mac OSX 10.10 using OpenCV 2.4.10 and clang 602.0.49, but If I try to use gcc 5.1 by putting set(CMAKE_CXX_COMPILER /usr/gcc-5.1.0/bin/g++-5.1.0) in my CMakeLists.txt, I get the following error:

Undefined symbols for architecture x86_64:
  "cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)", referenced from:
      _main in main.cpp.o
  "cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)", referenced from:
      _main in main.cpp.o

What's the problem and how do I fix it?

Tyler
  • 486
  • 1
  • 6
  • 20

2 Answers2

0

Please recompile your OpenCV using the same version of gcc that you use to compile the above code. This problem should hopefully disappear. From the official gcc pages,

If you get linker errors about undefined references to symbols that involve types in the std::__cxx11 namespace or the tag [abi:cxx11] then it probably indicates that you are trying to link together object files that were compiled with different values for the _GLIBCXX_USE_CXX11_ABI macro. This commonly happens when linking to a third-party library that was compiled with an older version of GCC. If the third-party library cannot be rebuilt with the new ABI then you will need to recompile your code with the old ABI.

hAcKnRoCk
  • 1,118
  • 3
  • 16
  • 30
  • With versions of gcc before 6, this can be accomplished by calling cmake with CXXFLAGS set to "-std=c++11" or "-std=c++14": `CXXFLAGS="-std=c++11" cmake ..` – tweej Mar 15 '17 at 21:44
0

We had this problem testing in a CentOS 7.9 container with gcc 5.3.1. We tried many solutions as shown here, here, and here, and although none worked as-is, the associated discussion and comments -- especially about using linker verbosity ('-Wl,--verbose') to understand what it's trying to do -- contributed pieces of the puzzle. After a day of struggle we solved it by downloading a later version of libstdc++ (6.0.21 vs 6.0.19) to /usr/lib64 and breaking the libstdc++.so symlink to point at the newer version. This allowed older OpenCV libs to link and -- so far -- has not broken anything else.

Note that we only changed the libstdc++ .so -- downloading various rpms, trying to upgrade the container, adding -D_GLIBCXX_USE_CXX11_ABI=0 build flag, etc - none of those worked.

We found it odd the issue didn't occur in our testing with gcc4 and gcc6-9 -- only with gcc5. As far as I can tell, after gcc 5 was released in 2015 time-frame, it wasn't long before this issue had significant attention, and updated versions of libstdc++ came along soon after.