1

I am teaching my self the book "opencv2 computer vision application programming cookbook" using QT creator 3.2.1 / QT 5.3.2 (clang5.0 (apple)). When I tried to build one program I had warnings as follows:

Undefined symbols for architecture x86_64:
  "cv::imread(std::string const&, int)", referenced from:
      MainWindow::on_pushButton_clicked() in mainwindow.o
      ColorDetectController::setInputImage(std::string) in mainwindow.o
  "cv::imshow(std::string const&, cv::_InputArray const&)", referenced from:
      MainWindow::on_pushButton_clicked() in mainwindow.o
      MainWindow::on_pushButton_2_clicked() in mainwindow.o
  "std::allocator<char>::allocator()", referenced from:
      MainWindow::on_pushButton_clicked() in mainwindow.o
      MainWindow::on_pushButton_2_clicked() in mainwindow.o
  "std::allocator<char>::~allocator()", referenced from:
      MainWindow::on_pushButton_clicked() in mainwindow.o
      MainWindow::on_pushButton_2_clicked() in mainwindow.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)", referenced from:
      MainWindow::on_pushButton_clicked() in mainwindow.o
      MainWindow::on_pushButton_2_clicked() in mainwindow.o
  "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
      MainWindow::on_pushButton_clicked() in mainwindow.o
      MainWindow::on_pushButton_2_clicked() in mainwindow.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: *** [controller.app/Contents/MacOS/controller] Error 1
14:49:33: **The process "/usr/bin/make" exited with code 2**.
Error while building/deploying project controller (kit: Desktop Qt 5.3 clang 64bit)
When executing step "Make"

And here's my .pro file:

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = controller
TEMPLATE = app


SOURCES += main.cpp\
           mainwindow.cpp \
           ../colorDetection/colorDetector.cpp \
           colorDetectController.cpp

HEADERS  += mainwindow.h \
            ../colorDetection/colorDetector.h \
            colorDetectController.h

FORMS    += mainwindow.ui

CONFIG += c++11
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.10


INCLUDEPATH += "/usr/local/opt/opencv/include" \

LIBS += -L"/usr/local/opt/opencv/lib" \
        -lopencv_core \
        -lopencv_highgui \
        -lopencv_imgproc

I met similar warnings before, but I solved that by adding these two lines into .pro file:

CONFIG += c++11

QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.10

However, this time it doesn't work. Can anyone explain to me how to solve this ? Thanks a lot.

Lake_Lagunita
  • 521
  • 1
  • 4
  • 20

1 Answers1

0

It aren't warnings, but errors. Linker errors. In particular, the line

"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)", referenced from:

Means that it couldn't even link a standard std::string, which is weird.

It couldn't find libraries for x64 architecture. I doesn't see in your config a switch to compile for this architecture, hence I am assumiing it is a default behavior, which in turn means that an according library (at least with std::string) should have been shipped with the compiler.

It led me to this question (though it was for gcc), where the problem was in libstdc++ being too old, and thus it didn't contained some definitions for C++11 standard.

Then I looked at the page with description for std::basic_string, and found that since c++11 in members was used std::allocator, which is what I see inside the braces of the undefined reference about a string.

Hence the problem with your c++ library being too old. You need to update your compiler. Also another interesting thing I noticed from your post: you probably saying a wrong version of clang. You told it is clang-5.0, so I looked at the version in my Kubuntu, and it was just 3.4. I wondered: did I so long not updated the system? Then I decided to peek at the clang site, and found that the latest version (which is written as being «in progress») is just 3.7.

The last thing I need to mention is the two undefined references at the beginning — you just need to add a path to a library that contains functions cv::imread, and cv::imshow.

Community
  • 1
  • 1
Hi-Angel
  • 4,933
  • 8
  • 63
  • 86
  • Thanks. As for the Clang version, I just clicked "Qt Creator"-"about Qt Creator" and then it shows Clang 5.0 (see my screen shot on https://www.dropbox.com/s/96ecm7uob5niown/%E5%B1%8F%E5%B9%95%E5%BF%AB%E7%85%A7%202015-06-18%20%E4%B8%8B%E5%8D%887.00.04.png?dl=0) For the two undefined references, I checked my code found that I added path to all imread and imshow function, which is weired. – Lake_Lagunita Jun 19 '15 at 02:05
  • Hi, I have solved the problem by adding two #include directives in main window.cpp file. Thanks for your help! – Lake_Lagunita Jun 19 '15 at 02:26
  • @LeonloveKaren that is weird. See: a compilation is different stages, one of which *(in short)* translates a source to machine code, and another one is when your app linked. The two I mentioned aren't very tied, i.e. the success of the first stage have little to do with a success of the second one, and vice versa. The linking stage shouldn't suddenly become working just because you added an include — cuz if the include was a problem, your code just wouldn't compile. Perhaps it was a glitch in environment variables…? Anyway, since it wan't the first time, I bet you will meet the problem again. – Hi-Angel Jun 19 '15 at 04:42
  • That's true... I am now trying to solving another similar problem... Hoping I'm lucky.... – Lake_Lagunita Jun 19 '15 at 06:23
  • @ Hi-Angel actually I am not CS major and all my C++ knowledges are from stanford CS106B, so I am not very clear about how to configure the C++ and the compiler... I found these are really nested. Do you have some good recommendation for me to make them clear? Thanks – Lake_Lagunita Jun 19 '15 at 06:44
  • Hm… I am not sure what to advice ☺ Well, everything you see in the in «.pro» file goes via command line to a compiler. You should somewhere see an output with the compiler invocation and its options. This means that in hard cases you can copy everything to terminal by hand, and experiment with different values. E.g. try to pass a full path to a library and see if it worked — it *(tho isn't good idea in general)* could help you understand whether library indeed have symbols you need. So, in fact you can write a code with usual text editor, and compile it by hand with invocation `clang++`. – Hi-Angel Jun 19 '15 at 09:45
  • @LeonloveKaren Aside advices, you will often meet cases when something mistirously *[not-]* work *(like this one)*. Remember, nothing can happen mystically, everything have a reason, and if you'll dig with enough strenght, eventually you will find it. Also it is good, that you using \*nix. Tho I've never used OS X, but rather X11 *( ☺ )*, I guess they're pretty similar. It's just that developing *(in non-interpreted lang)* on Windows® is a real pain — no good terminal *(powershell didn't solve)*, no `/proc/` filesystem, once you used GUI — stdout automatically disabled by OS; and many more… – Hi-Angel Jun 19 '15 at 09:46
  • @LeonloveKaren I think you need to read through a few linking tutorials. Try e.g. [this one](http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html). Before you begin reading, I should say, that a compiler usually calls different executable for linking stage, so called `linker`. But in fact you can use in terminal a name of the compiler itself, unless you need to pass some linker specific arguments. Also, you probably find more tutorials for GCC than for clang — don't worry about it, most of their options are the same. – Hi-Angel Jun 19 '15 at 10:15
  • Thanks for your advice, @Hi-Angel. – Lake_Lagunita Jun 19 '15 at 17:13