我想在mac上的qt中使用openCV lib。我读了很多关于如何配置qt(我有5.1)的pro文件来使用OpenCV,但没有什么对我有用。 我做了什么:
1) sudo port install opencv
2) sudo port install pkg-config
3) add to *.pro file: LIBS += `pkg-config --libs opencv`
但我只得到错误:
Target: x86_64-apple-darwin13.0.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -headerpad_max_install_names -macosx_version_min 10.9.0 -o opencv_pokus3.app/Contents/MacOS/opencv_pokus3 -L/opt/local/lib -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk main.o mainwindow.o moc_mainwindow.o -lopencv_core -lopencv_highgui -framework QtWidgets -framework QtGui -framework QtCore -framework OpenGL -framework AGL -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0/lib/darwin/libclang_rt.osx.a -F/Applications/Qt5.1.1/5.1.1/clang_64/lib
Undefined symbols for architecture x86_64:
"cv::imread(std::string const&, int)", referenced from:
_main in main.o
"std::allocator<char>::allocator()", referenced from:
_main in main.o
"std::allocator<char>::~allocator()", referenced from:
_main in main.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)", referenced from:
_main in main.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [opencv_pokus3.app/Contents/MacOS/opencv_pokus3] Error 1
12:56:15: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project opencv_pokus3 (kit: Desktop Qt 5.1.1 clang 64bit)
When executing step 'Make'
12:56:15: Elapsed time: 00:00.
我尝试将第3步更改为:
LIBS += -L/opt/local/lib \
-lopencv_calib3d \
-lopencv_contrib \
-lopencv_core \
-lopencv_features2d \
...
但不幸的是,结果相同......
PS:我可以通过终端编译openCV helloWorld程序:
g++ `pkg-config --libs --cflags opencv` -o helloWorld helloOpenCV.cpp
or
clang++ `pkg-config --libs --cflags opencv` -o helloWorld hello.cpp
一切都很好......
我的配置: OSX 10.9, QT 5.1.1, opencv 2.4.7通过macports安装, pkg-config 0.28通过macports安装,
简单的hello world程序:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
...
char var;
cv::Mat img;
img = cv::imread("./imgres.jpg");
cv::namedWindow("Image");
cv::imshow("Image", img);
std::cin >> var;
...
你有什么提示,怎么办?
编辑:从终端列出:
pkg-config --libs opencv
/opt/local/lib/libopencv_calib3d.dylib /opt/local/lib/libopencv_contrib.dylib /opt/local/lib/libopencv_core.dylib /opt/local/lib/libopencv_features2d.dylib /opt/local/lib/libopencv_flann.dylib /opt/local/lib/libopencv_gpu.dylib /opt/local/lib/libopencv_highgui.dylib /opt/local/lib/libopencv_imgproc.dylib /opt/local/lib/libopencv_legacy.dylib /opt/local/lib/libopencv_ml.dylib /opt/local/lib/libopencv_nonfree.dylib /opt/local/lib/libopencv_objdetect.dylib /opt/local/lib/libopencv_photo.dylib /opt/local/lib/libopencv_stitching.dylib /opt/local/lib/libopencv_superres.dylib /opt/local/lib/libopencv_ts.a /opt/local/lib/libopencv_video.dylib /opt/local/lib/libopencv_videostab.dylib
pkg-config --libs-only-L opencv
应该由此产生问题吗?为什么-L是空的?
答案 0 :(得分:1)
您没有发布完整的错误消息,并且您不清楚使用哪个编译器。如果您使用的是GCC版本,我认为问题可能实际上并不是找到OpenCV库(因为看起来您正在这样做),但是这些库中缺少符号。
这可能是因为Apple在10.9中将默认的C ++运行时库更改为libc ++。这个改变还引入了一个不同的C ++符号修改方案,即使用针对libc ++的clang构建的OpenCV库中的符号与给定OpenCV头的g ++不同 - 我相信这就是你看到缺少符号的原因。 / p>
即使符号重整是相同的,使用g ++从MacPorts编译OpenCV也不是一个好主意,因为g ++只支持libstdc ++作为运行时,这意味着你自己的软件会尝试使用libstdc ++而OpenCV会尝试使用libc ++作为C ++运行时。这会在同一进程中混合两个不同的(并且不兼容的)运行时并导致未定义的行为(最多随机崩溃)。
总之,在10.9及以上版本中使用g ++编译C ++代码意味着您还必须使用g ++手动编译所需的所有库。作为替代方案,请尝试使用clang ++作为编译器。
答案 1 :(得分:0)
错误发生在椅子和键盘之间......
我按照这个建议行事 Qt5.1/Qt5.2 + Mac OS 10.9 (Mavericks) + XCode 5.0.2, Undefined symbols for architecture x86_64
但我只点击了qmake并构建,而不是“全部清理”,所以main.o一直是为osx 10.6编译的......