我在OS X 10.9上安装了opencv-2.4.8.2。我正在尝试编译一个简单的代码:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 2 )
{
printf("usage: DisplayImage.out <Image_Path>n");
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
printf("No image data n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
和clang (/usr/bin/g++ -arch x86_64 pkg-config opencv --libs
test.cpp)可以做到这一点,但不能使用gcc 4.9 (/usr/local/bin/g++ -arch x86_64 pkg-config opencv --libs
test.cpp)。
这是我在gcc 4.9中得到的:
$ /usr/local/bin/g++ `pkg-config opencv --libs` test.cpp
Undefined symbols for architecture x86_64:
"cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)", referenced from:
_main in cc52UZjK.o
"cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)", referenced from:
_main in cc52UZjK.o
"cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)", referenced from:
_main in cc52UZjK.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
有谁能帮我弄清楚为什么吗?关于编译器的信息:
$/usr/bin/gcc -v配置:——prefix=/Applications/Xcode。应用程序/内容/开发/usr——with-gxx-include-dir =/usr/include/c++/4.2.1。准备Apple LLVM version 5.1 (clang-503.0.40)(基于LLVM 3.4svn)目标:x86_64-apple-darwin13.1.0线程模型:posix
$/usr/local/bin/gcc使用内置规格。COLLECT_GCC =/usr/地方/bin/gccCOLLECT_LTO_WRAPPER =/usr/地方/libexec gcc/x86_64-apple-darwin13.0.0/4.9.0/lto-wrapper目标:x86_64-apple-darwin13.0.0配置:../gcc-4.9-20131215/configure——enable-languages=c++,fortran线程模型:posixgcc版本4.9.0 20131215(实验版)(gcc)
简短的回答是:你不能在OS X 10.9及更新版本上使用GCC来构建c++代码,因为苹果公司改用libc++而不是libstdc++,并且GCC与该运行时不兼容。为了找到一个解决办法,已经做了许多尝试,但最终证明是不可行的。