c++ - 使用 gcc 5.1 未定义 OpenCV 符号

标签 c++ macos opencv gcc

我有一个非常基本的程序来测试 OpenCV 是否有效。

#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;
}

还有一个像这样找到 OpenCV 的 CMakeLists.txt 文件:

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

这适用于使用 OpenCV 2.4.10 和 clang 602.0.49 的 Mac OSX 10.10,但是如果我尝试通过将 set(CMAKE_CXX_COMPILER/usr/gcc-5.1.0/bin/g++-5.1 .0) 在我的 CMakeLists.txt 中,出现以下错误:

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

有什么问题,我该如何解决?

最佳答案

请使用您用于编译上述代码的相同版本的 gcc 重新编译您的 OpenCV。这个问题应该会消失。来自官方 gcc 页面,

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.

关于c++ - 使用 gcc 5.1 未定义 OpenCV 符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30064618/

相关文章:

c++ - 了解多重继承中的虚拟表

css - Windows 中几乎所有浏览器的滚动条(不是 Mac)

c++ - 如何仅将光流 vector 应用于检测到的圆

c++ - OpenCV 中的调试断言错误

python - 在 Mac 上安装 virtualenvwrapper - 未找到 hook_loader.py pkg_resources

c++ - 使用 OpenCV 2.4.3 的双线性插值的 VideoCapture 旋转

c++ - 如何正确移动带有区域的窗口?

c++ - 使用 MinGW 静态链接到 Windows 上的 libarchive

c++ - 如何在派生类中覆盖模板类的静态方法

java - 在 Mac OS 10.6.8 上安装 JDK 7