10

Environment : Mac OS 10.9 + Qt5.1/Qt5.2 + OpenCV2.4.7 + XCode(5.0.2)

I can compile the following program via terminal

g++ -L/usr/local/lib -lopencv_core -lopencv_highgui \
    -I/usr/local/include  main.cpp

The program a.out runs normally.

However, when using Qt 5.1/5.2 to run this OpenCV program, I got "Undefined symbols for architecture x86_64".

However, Qt5 works normally for a simple HelloWorld c++ program.

What is going on ?

Here is the code.

#include <iostream>
#include "opencv2/highgui/highgui.hpp"

using namespace std ;
using namespace cv ;

int main()
{
    Mat img ;

    img = imread("image.jpg") ;

    return 0;
}

And this is the project setting

INCLUDEPATH += /usr/local/include

LIBS += -L/usr/local/lib

LIBS += -lopencv_core -lopencv_highgui -v

cache()

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

Here is the compile message

/Users/XXX/Qt5.2.0/5.2.0-beta1/clang_64/bin/qmake -spec macx-clang CONFIG+=debug -o Makefile /Users/XXX/Desktop/untitled/untitled.pro

/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.9.sdk -mmacosx-version-min=10.6 -o untitled main.o   -L/usr/local/lib -lopencv_core -lopencv_highgui -v 

with -v to show invocation

Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
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.6.0 -o untitled -lcrt1.10.6.o -L/usr/local/lib -syslibroot

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk main.o -lopencv_core -lopencv_highgui -lstdc++ -lSystem

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0/lib/darwin/libclang_rt.osx.a

And here is the error message

Undefined symbols for architecture x86_64:
"cv::imread(std::string const&, int)", referenced from:
      _main in main.o
.... and so on

I checked the problem has nothing to do with -lstdc++ or -std=c++11 or -stdlib=libstdc++. by using command line compile.

Any ideas or suggestion?

Mat
  • 202,337
  • 40
  • 393
  • 406
maythe4thbewithu
  • 387
  • 1
  • 2
  • 12
  • you aren't linking in OpenCV it would seem. or imread isn't in the ones that you linked – Grady Player Dec 03 '13 at 22:12
  • Thank you for the comment. I also think the problem is the linker. I have tried to run a simple boot:filesystem program. It gives me the same error. – maythe4thbewithu Dec 04 '13 at 03:30
  • the problem could be that you need another component to be linked, like `-lopencv_core -lopencv_highgui -lopencv_somethingforimages` – Grady Player Dec 04 '13 at 16:49
  • Since I can compile in the terminal with "g++ -L/usr/local/lib -lopencv_core -lopencv_highgui -I/usr/local/include main.cpp" I think this problem does not caused by the missing -l. Any way thinks for your help, but I think I should go back to Mac OS X10.8, since I have worked on this for 5 days. – maythe4thbewithu Dec 04 '13 at 18:26
  • well you may end up with different version of that lib if you have it installed more than once... you can find the version that you are using and try to read the symbols with `nm` to see if it is in the library. – Grady Player Dec 04 '13 at 18:53
  • 1
    First, I should thinks for the suggestion of using nm. I didn't use nm to check OpenCV library, instead, I check both main.o from Qt and g++. I think I found the origin of the problem and a (at least) working solution. I also put this question on [link]http://qt-project.org/forums/viewthread/35646/ The solution is ; **You have to add `QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9` to the .pro file**. Other number, such as 10.6, 10.7, and 10.8 will fail. 10.6 is number generated by Qt5 on my computer. – maythe4thbewithu Dec 05 '13 at 15:03

3 Answers3

12

This issue is answered in this post

http://qt-project.org/forums/viewthread/35646/

and explained by sandy.martel

I quoted here

Qt binary distribution compile with -stdlib=libstdc++ to be compatible with 10.6, Xcode 5 on 10.9 will select -stdlib=libc++ by default (for OS X 10.7 and better only). So symbol using classes from the standard library (like std::string in this case) will not resolve correctly at link time. This is why you’re seeing this error (Undefined symbols for architecture x86_64) . Look with which standard library opencv is built: otool -L libopencv_XXX.dylib. You’ll have to rebuild it with the right one or change Qt’s mkspec to use the newer one.

I solve this by changing ../Qt5.2.0/5.2.0-rc1/clang_64/mkspecs/macx-clang/qmake.conf

from

QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.6

to

QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9

And remember clean your project before rebuild.

maythe4thbewithu
  • 387
  • 1
  • 2
  • 12
  • 2
    Note that while you can just change the deployment target and everything will link fine, you still have to make sure only one of the two possible C++ runtime libraries (libc++ and libstdc++) will be present in your final program. Passing objects allocated using one of the runtimes to a library that uses the other leads to crashes. You need to make sure all the libraries you're trying to use that export C++ APIs are built against the same C++ runtime. – neverpanic Dec 07 '13 at 13:45
  • 3
    I have changed QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9, but still it didn't work, why – user824624 Feb 11 '15 at 20:33
0

In case you came here, because you changed config+=11 and nothing happend, try to clean your project before rebuild.

user1767754
  • 23,311
  • 18
  • 141
  • 164
0

The problem is not with the system. On Mac you just have to include imgcodecs header and include the respective lib files also. This will solve all the trouble you had.

-lopencv_imgcodecs

Anubhav Rohatgi
  • 458
  • 1
  • 6
  • 16