我使用的是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.app/Content/Developer/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-苹果-达尔文13.1.0 Thread型号:波六
$ /usr/local/bin/gcc -v 使用内置规范。COLLECT_GCC=/usr/local/bin/gcc COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-apple-darwin13.0.0/4.9.0/lto-wrapper 目标:x86_64-apple-darwin13.0.0 配置:../gcc-4.9-20131215/配置 --enable-languages=c ,fortran 线程模型:POSIX GCC 版本 4.9.0 20131215(实验性)(GCC)
简短的回答是:你不能使用 GCC 在 OS X 10.9 及更高版本上构建 C 代码,因为 Apple 切换到使用 libc 而不是 libstdc,而 GCC 与该运行时不兼容。已经进行了许多尝试来寻找解决方法,但最终证明是不可行的。