3

I am using QT's IDE with OpenCV. The code works fine in Xcode but when I copy them to QT for GUI design, it doesn't work at all. I searched like crazy...

Here is the error message:

    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -c -pipe -g -gdwarf-2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -mmacosx-version-min=10.9 -Wall -W -fPIE -DQT_CORE_LIB -I../../../../Qt/5.2.1/clang_64/mkspecs/macx-clang -I../Hello -I/usr/local/include -I../../../../Qt/5.2.1/clang_64/lib/QtCore.framework/Versions/5/Headers -I. -I. -F/Users/Chenxi/Qt/5.2.1/clang_64/lib -o main.o ../Hello/main.cpp
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -headerpad_max_install_names -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -mmacosx-version-min=10.9 -o Hello main.o   -F/Users/Chenxi/Qt/5.2.1/clang_64/lib -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_objdetect -lopencv_calib3d -framework QtCore 
Undefined symbols for architecture x86_64:
  "cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > 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)
make: *** [Hello] Error 1

main.cpp:

So it should be the link error with opencv I think.

#include <QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cv.h>
#include <iostream>


using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    cout<<"hello world"<<endl;
    Mat test(10,10,CV_8UC1);
    cout<<test.cols<<endl;

    Mat test1 = imread("/Users/master/desktop/FYP/GUI/Demo_db/neg_1.png");

    return a.exec();
}

.pro file:


QT       += core

QT       -= gui

TARGET = Hello
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

INCLUDEPATH += /usr/local/include

LIBS += -L/usr/local/lib \
    -lopencv_core \
    -lopencv_imgproc \
    -lopencv_highgui \
    -lopencv_objdetect \
    -lopencv_calib3d


SOURCES += main.cpp

I have tried this answer but it doesn't work for me: QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.6 to 10.9

Could anyone help me? Thanks.

Community
  • 1
  • 1
foresea
  • 73
  • 7
  • Please show your project file. – László Papp May 17 '14 at 02:59
  • @LaszloPapp I just update my question, I now just test a imread() function of Opencv, the error still occurs. – foresea May 17 '14 at 04:55
  • @LaszloPapp could you please take a look of my updated question? – foresea May 17 '14 at 06:58
  • Are you sure not mixing 32 and 64 bits? Opencv is installed in your /usr/local/lib? – László Papp May 17 '14 at 07:14
  • Yes, I am sure. "lipo -info libopencv_core.dylib Non-fat file: libopencv_core.dylib is architecture: x86_64" and "clang --version Apple LLVM version 5.1 (clang-503.0.38) (based on LLVM 3.4svn) Target: x86_64-apple-darwin13.1.0 Thread model: posix" – foresea May 17 '14 at 07:23
  • Does it work if you comment out the opencv lines in the main.cpp? – László Papp May 17 '14 at 11:51
  • Yes, it works after comment out the imread() line. It can output the cols and rows of Mat test. – foresea May 17 '14 at 12:17
  • Have you read the 3 lines at the bottom [here](http://www.executionunit.com/blog/2012/10/27/xcode-std-link-errors/). Also, have you tried to pass `-stdlib=libc++` to the compiler? – László Papp May 17 '14 at 12:21

1 Answers1

2

I think you will need to change from the default gnu C++ std implementation to the following by passing it to your compiler, which is presumably clang:

-stdlib=libc++

In short, do not mix gcc and clang if you do that because unfortunately they are not 100% abi compatible, sadly. You could check which one the libopencv was built with:

otool -L libopencv-version.dylib
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Hi, thanks soooo much for your answer. I also noticed that in Xcode I can use Opencv only under the "libstdc++ (GNU C++ standard library)". Could you please tell me how to change the QT's setting? By inserting "-stdlib=libc++" into .pro file? – foresea May 17 '14 at 12:35
  • 1
    @OwenZhang: try to put that into this variable: http://qt-project.org/doc/qt-5/qmake-variable-reference.html#qmake-cxxflags So, you would write this: `QMAKE_CXXFLAGS += -stdlib=libc++`. – László Papp May 17 '14 at 12:37
  • I put "QMAKE_CXXFLAGS += -stdlib = libc++" at the end of .pro file. But no luck...same error. – foresea May 17 '14 at 12:47
  • have you checked with make VERBOSE=1 that it is actually passed the compiler? If not, you will need to provide an [SSCCE](http://sscce.org). – László Papp May 17 '14 at 12:48
  • Sorry, I am quite new to the compiler thing...Could you tell me the details of VERBOSE? Thanks a lot. – foresea May 17 '14 at 13:04
  • Already did :) Run `make VERBOSE=1` from the command line, or in the project settings add those options to make. – László Papp May 17 '14 at 13:10
  • You mean in the Terminal? It backs as"$ make VERBOSE=1 make: *** No targets specified and no makefile found. Stop." – foresea May 17 '14 at 13:17
  • @OwenZhang: you need to run qmake first to generate the makefile, but it is better if you just set VERBOSE=1 in the project settings of QtCreator. Also, show your pro file in the meantime. – László Papp May 17 '14 at 13:17
  • 1
    @OwenZhang: is this resolved? If yes, select an answer. – László Papp Jul 20 '14 at 03:17