问题
I am on OS X 10.9, with opencv-2.4.8.2 installed. I am trying to compile a simple code:
#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;
}
and can do that with clang (/usr/bin/g++ -arch x86_64 pkg-config opencv --libs
test.cpp), but not with gcc 4.9 (/usr/local/bin/g++ -arch x86_64 pkg-config opencv --libs
test.cpp).
This is what I get with 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
Can anyone help me figure out why?
info about my compilers:
$ /usr/bin/gcc -v Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) Target: x86_64-apple-darwin13.1.0 Thread model: posix
$ /usr/local/bin/gcc -v Using built-in specs. COLLECT_GCC=/usr/local/bin/gcc COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-apple-darwin13.0.0/4.9.0/lto-wrapper Target: x86_64-apple-darwin13.0.0 Configured with: ../gcc-4.9-20131215/configure --enable-languages=c++,fortran Thread model: posix gcc version 4.9.0 20131215 (experimental) (GCC)
回答1:
The short answer is: you cannot use GCC to build C++ code on OS X 10.9 and newer, because Apple switched to using libc++ instead of libstdc++ and GCC is incompatible with that runtime. Many attempts have been made to find a workaround, but in the end it turned out to be infeasible.
来源:https://stackoverflow.com/questions/23503551/opencv-compile-with-clang-ok-with-gcc-not-ok-os-x-10-9