OpenCV symbols are undefined using gcc 5.1

筅森魡賤 提交于 2021-01-29 02:40:21

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/30064618/opencv-symbols-are-undefined-using-gcc-5-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!