简体   繁体   English

OpenCv + Mac OS X + QT Creator =奇怪的链接错误?

[英]OpenCv + mac os x + qt creator = strange linking bug?

I'm trying to build simple OpenCV app using qt creator on Mac Os X 10.9. 我正在尝试在Mac Os X 10.9上使用qt creator创建简单的OpenCV应用程序。 So i've installed OpenCv and Qt4 using brew: 所以我使用brew安装了OpenCv和Qt4:

brew install opencv
brew install qt4

and then downloaded qt creator. 然后下载了qt creator。 Than i've made simple project - main.cpp: 比我做了一个简单的项目-main.cpp:

#include <QCoreApplication>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Mat m;
    m = Mat(2, 3, 4);
    cv::threshold(m, m, 123, 200, 1);
    m = cv::imread("asdasd", 1); //problem is here!
    return a.exec();
}

and .pro file: 和.pro文件:

QT       += core
QT       -= gui
TARGET = opencv_test
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
LIBS += -L/usr/local/lib \
    -lopencv_core \
    -lopencv_imgproc \
    -lopencv_highgui \
    -lopencv_objdetect \
    -lopencv_calib3d

INCLUDEPATH += /usr/local/include/opencv
DEPENDPATH += /usr/local/include/opencv/include

INCLUDEPATH += $$PWD/../../../usr/local/include
DEPENDPATH += $$PWD/../../../usr/local/include
DEPENDPATH += $$PWD/../../../usr/local/lib

Now the weird part - if i comment this line: m = cv::imread("asdasd", 1); //problem is here! 现在奇怪的部分 - 如果我评论这一行: m = cv::imread("asdasd", 1); //problem is here! m = cv::imread("asdasd", 1); //problem is here! and build project, everything is fine - it will compile and link without any problem. 并构建项目,一切都很好-它将毫无问题地进行编译和链接。 So it seems that .dylib files are ok, because i can use OpenCV functions - Mat object constructor and threshold function. 所以似乎.dylib文件没问题,因为我可以使用OpenCV函数Mat对象构造函数和threshold函数。 But! 但! If i don't comment line with imread i get this error: :-1: błąd:symbol(s) not found for architecture x86_64 or to be more precise: 如果我不对imread进行注释,则会出现以下错误:: :-1: błąd:symbol(s) not found for architecture x86_64或更确切地说:-1: błąd:symbol(s) not found for architecture x86_64

Undefined symbols for architecture x86_64: "cv::imread(std::string const&, int)", 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) 架构x86_64的未定义符号:“cv :: imread(std :: string const&,int)”,引用自:main.o中的_main ld:未找到架构x86_64 clang的符号:错误:链接器命令失败并退出代码1(使用-v查看调用)

Generally the problem is just a part of bigger problem - i'm trying to build much bigger project using qt creator, but linker can't find some(but not all!) functions. 通常,问题只是更大问题的一部分-我正在尝试使用qt creator创建更大的项目,但链接器无法找到某些(但不是全部!)功能。 In project which uses lot of OpenCV functions linker has got problems only with: 在使用大量OpenCV功能的项目中,链接器仅在以下方面出现问题:

cv::VideoCapture::VideoCapture(std::string const&)
cv::destroyWindow(std::string const&)
cv::CascadeClassifier::detectMultiScale(cv::Mat const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, double, int, int, cv::Size_<int>, cv::Size_<int>)
cv::CascadeClassifier::CascadeClassifier(std::string const&)
cv::imshow(std::string const&, cv::_InputArray const&)

In this project i'm using much more functions (mostly basic image processing - threshold, image copying, drawing basic primitives, finding contours) and linker can handle them without any problem. 在这个项目中,我使用了更多的功能(主要是基本的图像处理 - 阈值,图像复制,绘制基本图元,查找轮廓),链接器可以毫无问题地处理它们。

What i've already tried - building OpenCv and Qt4(only using build brew install qt4 --from-source ) from sources. 我已经尝试过 - 从源brew install qt4 --from-source构建OpenCv和Qt4(仅使用build brew install qt4 --from-source )。 I've builded OpenCv using this tutorial - http://sadeepj.blogspot.com/2012/03/installing-and-configuring-opencv-to.html I've also tried a lot of CMAKE options - -DCMAKE_OSX_ARCHITECTURES= x86_64 or i386 and -DCMAKE_CXX_FLAGS="-stdlib=libc++" . 我已使用本教程构建了OpenCv- http: //sadeepj.blogspot.com/2012/03/installing-and-configuring-opencv-to.html我还尝试了许多CMAKE选项-DCMDCM_OSX_ARCHITECTURES -DCMAKE_OSX_ARCHITECTURES= x86_64或i386和-DCMAKE_CXX_FLAGS="-stdlib=libc++" I've also tried to build it using gcc(defautt compiler is clang) version 4.2, but it failed(building failed). 我也尝试使用gcc(defautt编译器是clang)版本4.2来构建它,但它失败了(构建失败)。 i've tried with OpenCV 2.4.8 and 2.4.2. 我尝试过OpenCV 2.4.8和2.4.2。 Also i've tried to set: 我也尝试过设置:

CMAKE_CXX_FLAGS += -std=c++11
CMAKE_CXX_FLAGS += -stdlib=stdlibc++

Still no luck. 仍然没有运气。

I've checked whether builded .dylib files are x86_64: 我检查了构建的.dylib文件是否为x86_64:

/usr/local/lib[master]$ lipo -info libopencv_core.dylib Non-fat file: libopencv_core.dylib is architecture: x86_64 / usr / local / lib [master] $ lipo -info libopencv_core.dylib非脂肪文件:libopencv_core.dylib是体系结构:x86_64

and whether highgui file contains any information about imread function(i know that it is not a proof that it export this function): 以及highgui文件是否包含有关imread函数的任何信息(我知道它不是它导出此函数的证据):

/usr/local/lib[master]$ nm libopencv_highgui.dylib | grep imread
00000000000069b0 T __ZN2cv6imreadERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEEi
0000000000006ac0 t __ZN2cvL7imread_ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEEiiPNS_3MatE

Information which might be important: Mac OS X 10.9, 64 bit. 可能重要的信息:Mac OS X 10.9(64位)。

/usr/local/lib[master]$ clang --version
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
/usr/local/lib[master]$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix

Btw - isn't it strange that running gcc --version in fact runs clang ? 顺便说一句-运行gcc --version实际上运行clang并不奇怪吗?

So the question is - what can i do to compile and build it? 所以问题是 - 我能做些什么来编译和构建它? Should i try using gcc or maybe something else will solve this problem? 我应该尝试使用gcc还是别的东西可以解决这个问题? Also - if someone could upload his libraries and share a link it, so i could try with them it would be great. 此外 - 如果有人可以上传他的图书馆并分享链接,那么我可以尝试使用它们会很棒。 Any solution that will solve this problem will be fine for me - i can build everything from source, use any compiler, linker, etc.. - just help me make it working and i will be happy :) 任何可以解决此问题的解决方案对我来说都是很好的-我可以从源代码构建所有内容,可以使用任何编译器,链接器等。-只需帮助我使其工作,我就会很高兴:)

Ok - finally after 4 days of searching i've solved this problem. 好的 - 经过4天的搜索后我终于解决了这个问题。 Generally solution is simple - just add: 通常,解决方案很简单-只需添加:

QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9

to you .pro file. 你的.pro文件。 If don't want to add it to every .pro file you can set it "globally" - just set it in this file: 如果不想将它添加到每个.pro文件,您可以将其设置为“全局” - 只需将其设置在此文件中:

Qt5.2.0/5.2.0-rc1/clang_64/mkspecs/macx-clang/qmake.conf 

More information about this bug/problem: 有关此错误/问题的更多信息:
Qt5.1/Qt5.2 + Mac OS 10.9 (Mavericks) + XCode 5.0.2, Undefined symbols for architecture x86_64 Qt5.1 / Qt5.2 + Mac OS 10.9(Mavericks)+ XCode 5.0.2,架构x86_64的未定义符号
http://qt-project.org/forums/viewthread/35646/ <-- mostly here http://qt-project.org/forums/viewthread/35646/ < - 主要是在这里

//edit: //编辑:
This solution only works with CLang, no luck with gcc. 此解决方案仅适用于CLang,不适用于gcc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM