Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

c++ - 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. So i've installed OpenCv and Qt4 using brew:

brew install opencv
brew install qt4

and then downloaded qt creator. Than i've made simple project - 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:

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! 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. 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:

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)

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. In project which uses lot of OpenCV functions linker has got problems only with:

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. 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++". I've also tried to build it using gcc(defautt compiler is clang) version 4.2, but it failed(building failed). i've tried with OpenCV 2.4.8 and 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:

/usr/local/lib[master]$ lipo -info libopencv_core.dylib Non-fat file: libopencv_core.dylib is architecture: 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):

/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.

/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 ?

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? 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 :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok - finally after 4 days of searching i've solved this problem. Generally solution is simple - just add:

QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9

to you .pro file. If don't want to add it to every .pro file you can set it "globally" - just set it in this file:

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
http://qt-project.org/forums/viewthread/35646/ <-- mostly here

//edit:
This solution only works with CLang, no luck with gcc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...