在Xcode中链接OpenCV时,架构x86_64的未定义符号错误

sie*_*hie 13 xcode opencv

我在Xcode中链接OpenCV时遇到问题.我使用brew安装了OpenCV:

brew tap homebrew/science
sudo brew install opencv
Run Code Online (Sandbox Code Playgroud)

我开始了一个新的Xcode命令行项目,添加/usr/local/lib/usr/local/include到库和头搜索路径.我还添加了输出pkg-config --libs opencvother linker options.

但是当我尝试编译这个小样本程序时:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

int main(int argc, char *argv[])
{
    cv::Mat test;
    cv::namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
    cv::waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
} 
Run Code Online (Sandbox Code Playgroud)

我收到以下链接器错误:

Undefined symbols for architecture x86_64:
  "cv::namedWindow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
Run Code Online (Sandbox Code Playgroud)

但我可以使用命令行编译程序

g++ `pkg-config --cflags --libs opencv` prog.cpp
Run Code Online (Sandbox Code Playgroud)

所以我认为问题在于Xcode的设置方式.但我无法确定Xcode编译方式与命令行参数之间究竟有什么不同.

有谁知道这个错误的原因或者想知道我可以尝试调查这个问题?

Dan*_*tín 27

在Xcode中,在项目中创建一个新组,右键单击它,选择Add Files to Project,导航到该/usr/local/lib文件夹并添加以下基本库:

libopencv_core.dylib,libopencv_ml.dylib,libopencv_video.dylib

在早期版本的OpenCV中,库名称可以是:

libcxcore.dylib,libcvaux.dylib,libcv.dylib,libhighgui.dylib,libml.dylib

(这些库可能位于另一条路径中,具体取决于您在Mac上安装OpenCV的方法.)

编辑:

如果您在链接阶段包含动态库,则OP不需要上述行.

选择项目,转到Build Settings选项卡,按c ++标准库过滤,并将此参数设置为libstdc ++(GNU C++标准库).

  • 使用`libstdc ++`解决了我的问题. (3认同)

小智 7

我已将Build Settings - > c ++标准库设置为Compiler Default.错误消失了.