在Xcode中链接OpenCV时,体系结构x86_64错误的未定义符号 [英] Undefined symbols for architecture x86_64 error when linking OpenCV in Xcode
问题描述
我在Xcode中链接OpenCV时遇到问题. 我使用brew安装了OpenCV:
I have a problem linking OpenCV in Xcode. I installed OpenCV using brew:
brew tap homebrew/science
sudo brew install opencv
我开始了一个新的Xcode命令行项目,并在库和标头搜索路径中添加了/usr/local/lib
和/usr/local/include
.
我还将pkg-config --libs opencv
的输出添加到了other linker options
.
I started a new Xcode commandline project, added /usr/local/lib
and /usr/local/include
to library and header search path.
I also added the output of pkg-config --libs opencv
to other linker options
.
但是当我尝试编译这个小示例程序时:
But when I try to compile this small sample program:
#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;
}
我收到以下链接器错误:
i get the following linker error:
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
但是我可以使用以下命令在命令行上编译程序
But I am able to compile the program on the command line using
g++ `pkg-config --cflags --libs opencv` prog.cpp
所以我认为问题在于Xcode的设置方式.但是我无法确定Xcode的编译方式和命令行参数之间到底有什么区别.
So I think the problem is the way Xcode is set up. But I am not able to determine what exactly is different between the way Xcode compiles and my commandline argument.
有人知道这个错误的原因吗,或者有一个想法我可以尝试调查这个问题吗?
Does anyone know the reason for this error or has an idea what I could try to investigate the problem?
推荐答案
在Xcode中,在您的项目中创建一个新组,右键单击它,选择将文件添加到 Project ,导航到/usr/local/lib
文件夹并添加以下基本库:
In Xcode create a new group inside your project, right click it, choose Add Files to Project, navigate to the /usr/local/lib
folder and add the following basic libraries:
libopencv_core.dylib,libopencv_ml.dylib,libopencv_video.dylib
libopencv_core.dylib, libopencv_ml.dylib, libopencv_video.dylib
在以前的OpenCV版本中,库名称可能是:
In previous versions of OpenCV the library names could be:
libcxcore.dylib,libcvaux.dylib,libcv.dylib,libhighgui.dylib, libml.dylib
libcxcore.dylib, libcvaux.dylib, libcv.dylib, libhighgui.dylib, libml.dylib
(这些库可能位于其他路径中,具体取决于您在Mac上安装OpenCV的方法.)
(The libraries may be in another path depending on the method you used to install OpenCV on your Mac.)
如OP所述,如果在链接阶段包含动态库,则上述行不是必需的.
The above lines should not be necessary if you are including the dynamic libraries in the linking phase as the OP explains.
选择您的项目,转到 Build Settings 标签,按 c ++标准库进行过滤,然后将此参数设置为 libstdc ++(GNU C ++标准库).
Select your project, go to the Build Settings tab, filter by c++ standard library, and set this parameter to libstdc++ (GNU C++ standard library).
这篇关于在Xcode中链接OpenCV时,体系结构x86_64错误的未定义符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!