OpenCv + mac os x + qt creator = strange linking bug? - c++

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

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.

Related

Qt 5.3.2 on Mac Snow Leopard: ld: symbol(s) not found for architecture x86_64 [clang, objective-c and c++]

I read through many questions of the same nature, but didn't find something specific to what I needed.
I am compiling a project on Snow Leopard (10.6.8) and Qt 5.3.2.
In my program's main method I check if it's on mac and bring up a cocoa-based window, otherwise creating the standard QApplication and MainWindow configuration. I have an .mm objective-c class that contains the window code and when include its corresponding header file, it gives me this error:
Undefined symbols for architecture x86_64:
"QMacApplication::QMacApplication()", 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)
I am using modified code from QT's official github repo for qtmacextras, putting the cocoa window code into its own library which I can import and use in other applications. See https://code.qt.io/cgit/qt/qtmacextras.git/tree/examples/macextras/macfunctions?h=5.14, although I'm using the 5.3.2 compatible commit.
Why am I getting this error?
This error can come from a missing framework as other questions have pointed out.
But the reason for this particular error was that I was missing some lines in my project pro file.
I was using OBJECTIVE_SOURCES to include my .mm file, but I was not including OBJECTIVE_HEADERS with my header file -- even though it didn't contain any objective-c code and I already specified it in the HEADERS directive.
Here's a concise version of my .pro file:
TARGET = QMacApplication
TEMPLATE = lib
QT += widgets core macextras
QT_PRIVATE += gui-private core-private
QMAKE_CXXFLAGS_GNUCXX11 = -std=c++98
CONFIG -= c++11
load(qt_build_config)
ios {
LIBS_PRIVATE += -framework UIKit
} else {
LIBS_PRIVATE += -framework AppKit
}
SOURCES += \
window.cpp
HEADERS += qmacapplication.h\
qmacapplication_global.h \
window.h
OBJECTIVE_SOURCES += qmacapplication.mm
OBJECTIVE_HEADERS += qmacapplication.h
It compiles when I remove qmacapplication.h from the HEADERS directive, but it seems safe to leave it in as well.
This is using xcode 4.2 if it matters.

Qt && firebase C++ SDK linking issues on iOS

I have following build env:
host: OSX 10.12 Sierra
XCode 9.2
Qt 5.10.1 for iOS
QtCreator 4.7.0
firebase 5.2
and try to build my application from QtCreator for iOS with the firebase support, but it fails with following linking errors:
Undefined symbols for architecture arm64:
"_OBJC_CLASS_$_FIRMessaging", referenced from:
objc-class-ref in firebase_messaging(messaging_231c52c311096cfce13e67fa91eb9ac5.o)
"_OBJC_CLASS_$_FIRApp", referenced from:
objc-class-ref in firebase(app_ios_814e1620d4f88024cea4bade26623a67.o)
"_OBJC_CLASS_$_FIROptions", referenced from:
objc-class-ref in firebase(app_ios_814e1620d4f88024cea4bade26623a67.o) ld: symbol(s) not
found for architecture arm64 clang: error: linker command failed with
exit code 1 (use -v to see invocation)
I have simplified my project to following code:
== main.cpp ==
#include <QCoreApplication>
#include <firebase/app.h>
#include <firebase/messaging.h>
#include <firebase/util.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
::firebase::App *fapp = ::firebase::App::Create();
Q_UNUSED(fapp);
return a.exec();
}
and a project file is:
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
# Check for GOOGLE_FIREBASE_SDK environment variable.
ENV_GOOGLE_FIREBASE_SDK = $$(GOOGLE_FIREBASE_SDK)
# Or define GOOGLE_FIREBASE_SDK path here.
GOOGLE_FIREBASE_SDK =
isEmpty(ENV_GOOGLE_FIREBASE_SDK) {
isEmpty(GOOGLE_FIREBASE_SDK) {
message("GOOGLE_FIREBASE_SDK" environment variable not detected!)
}
}
INCLUDEPATH += $$(GOOGLE_FIREBASE_SDK)
INCLUDEPATH += $$(GOOGLE_FIREBASE_SDK)/include
SOURCES += \
main.cpp
FCM_LIBS_PATH = $$(GOOGLE_FIREBASE_SDK)/frameworks/ios/universal
message("FCM_LIBS_PATH = $$FCM_LIBS_PATH")
LIBS += -F$$FCM_LIBS_PATH \
-framework firebase_messaging \
-framework firebase \
-framework Foundation \
-framework UserNotifications \
-framework UIKit \
-framework CoreGraphics
The firebase SDK contains different frameworks directories for different architectures :
~/firebase_cpp_sdk/frameworks/ios/universal
~/firebase_cpp_sdk/frameworks/ios/amd64
~/firebase_cpp_sdk/frameworks/ios/i386
~/firebase_cpp_sdk/frameworks/ios/x86_64
~/firebase_cpp_sdk/frameworks/ios/armv7
As I see that error related to 'arm64' architecture, so, I have changed the project file LIBS to use 'arm64' instead of 'universal', but this does not help.
Also I tried to build the project from the XCode, using qmake's generated xcode.project file, but there are same error.
I looked on stackoverflow a similar issues, but that workarounds does not help:
I tried to remove the /Users/admin/Library/Developer/Xcode/DerivedData directory.
I tried to play with the XCode options "Build Settings -> Build Active Architecture Only -> yes|no"
I looked a code from the following projects:
qtcloudmessaging: https://github.com/qt/qtcloudmessaging
QtFirebase: https://github.com/Larpon/QtFirebase
But I don't understand why this linker error happens.. Maybe is it a qmake bug?
The error means the project lacks Firebase pod.
firebase_cpp_sdk alone is not enough, in addition, it requires Firebase Core library.
In order add Firebase lib, you should migrate to use cocoapods.
In the project's source root, run
pod init
a new file Podfile would be then generated.
Open the file, add the following line to the target
pod 'Firebase/Analytics'
and save the file.
After running
pod update
successfully, open the .xcworkspace and build as usual.

Trouble compiling c++ program on Mac OSX 10.9 with opencv

I'm trying to compile a C++ program on Mac OSX 10.9 via the command line (and clang) that uses opencv (version 2.4.12) and am running into some problems.
I'm trying to compile the program by running the following in Terminal:
clang --std=c++11 -stdlib=libc++ test.cpp -L/usr/local/Cellar/opencv/2.4.12/lib -lopencv_core.2.4.12 -lopencv_ml.2.4.12 -lopencv_video.2.4.12 -lopencv_ml -lopencv_video -lc++
However I'm getting the following error:
Undefined symbols for architecture x86_64:
"cv::namedWindow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
Along with a string of other opencv functions that can't be found.
Upon doing some additional research, I found out that the problem might be associated with using libc++ instead of libstdc++ as the library. I tried switching to libstdc++ and was stuck with the following error.
fatal error: 'array' file not found
#include <array>
I researched that error and found out that for the Mac, it was because of using libstdc++ instead of libc++. Essentially, I'm kind of stuck in a loop and need some help compiling this program. Any thoughts?

cv::VideoCapture from string gives linker error in Qt5 on Mac

Problem
I am trying to get the OpenCV VideoCapture class running in a Qt project.
When I call it with an int (0 for the video camera on my mac), it works fine:
#include "opencv2/opencv.hpp"
#include <opencv2/highgui/highgui.hpp>
...
cv::VideoCapture cap(0);
The project compiles and when I run it, the light on my camera goes on.
BUT: when I try to compile it with a std::string as argument, I get a linker error:
#include "opencv2/opencv.hpp"
#include <opencv2/highgui/highgui.hpp>
...
cv::VideoCapture cap("/Users/xxxxx/small.mp4");
results in:
Undefined symbols for architecture x86_64:
"cv::VideoCapture::VideoCapture(std::string const&)", referenced from:
MainWindow::MainWindow(QWidget*) 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)
althought the constructor from std::string should actually exist:
http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-videocapture
Settings
My specifications:
Qt 5.4.0 (Clang 6.0 (Apple), 64 bit)
Mac OSX 10.10.1
OpenCV 2.4.9 installed from homebrew
My .pro file:
QT += core gui widgets
TARGET = VideoCaptureTest
TEMPLATE = app
SOURCES += main.cpp mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += /usr/local/include
LIBS += -L/usr/local/lib
LIBS += -lopencv_core
LIBS += -lopencv_imgproc
LIBS += -lopencv_highgui
LIBS += -lopencv_ml
LIBS += -lopencv_video
LIBS += -lopencv_videostab
LIBS += -lopencv_features2d
LIBS += -lopencv_calib3d
LIBS += -lopencv_objdetect
LIBS += -lopencv_contrib
LIBS += -lopencv_legacy
LIBS += -lopencv_flann
What I tried
1.
I found this question on SO, which seems to be the exact same problem as mine:
Linking error: undefined reference to `cv::VideoCapture::open(std::string const&)`
It seem my problem could have something to do with clashing versions of OpenCV. But I checked my include directories and my lib directories and I seem to only have 2.4.9 installed. Is there maybe a different version that comes with Qt?
I tried including version 2.4.9 directly in my .pro file with
LIBS += -lopencv_core2.4.9
...
But how can I make sure I include this version as well in my source file?
2.
I though it might have something to do with Mac and x86_64 stuff. I included the following lines, which I found somewhere online, in my .pro file:
CONFIG += x86 ppc x86_64 ppc64
CONFIG += MAC_CONFIG
MAC_CONFIG {
QMAKE_CXXFLAGS = -std=c++11 -stdlib=libstdc++ -mmacosx-version-min=10.7
QMAKE_LFLAGS = -std=c++11 -stdlib=libstdc++ -mmacosx-version-min=10.7
}
But this also showed no effect.
3.
Here (http://answers.opencv.org/question/14772/solved-linking-error-undefined-reference-to-cvvideocaptureopenstdstring-const/) it is suggested to try cv::String instead of std::string. I also tried this:
#include "opencv2/opencv.hpp"
...
cv::String s("/Users/xxxxx/small.mp4");
cv::VideoCapture cap(s);
and strangely I still get the exact same error:
Undefined symbols for architecture x86_64:
"cv::VideoCapture::VideoCapture(std::string const&)", referenced from:
MainWindow::MainWindow(QWidget*) 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)
which seems like the cv::String is actually converted to std::string, I guess because the compiler somewhere finds the definition of the constructor with std::string, but the linker does not?
4.
Apparently there was a similar problem with OSX 10.9, not with cv::VideoCapture, but in general resulting also in linking error for x86_64. The solution was to include
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9
in the .pro file. I tried this as well with 10.9 and 10.10 (which I am actually using). Still the same error. Could this be a problem with my version of OSX?
Help
I found no other suggestions on how to solve the problem. Does somebody have an idea what I am doing wrong? Any help appreciated!
Patrick
The answer has been given in the discussion(#wezside) of the link (item 1 of your description).
for OpenCV 3.0 (also 2.4.9 ?)
you need to add
LIBS += -lopencv_videoio
to the .pro, which is missing in yours.
After researching for several hours, I found that adding QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.11 to your .pro file and removing the previous .pro.user file and build directory can solve the problem. The second step is required.
I'm on El Capitan.

SFML 2.1 Undefined symbols for architecture x86_64 error

I have recently installed SFML 2.1 and am trying to compile and run from the terminal, NOT Xcode, the sample program that they have on their website at http://www.sfml-dev.org/tutorials/2.0/start-linux.php.
Path to where I installed SFML:
/Users/.../Desktop/C++_Code/SMFL/SFML-2.1-osx-gcc-universal
However, I keep getting the error of:
Undefined symbols for architecture x86_64:
"sf::String::String(char const*, std::__1::locale const&)", referenced from:
_main in window-48cb4a.o
After googling it, I came across this question: SFML 2.1 RenderWindow linking error
In the accepted answer, the person said that if I wanted to use g++, I had to download the "GCC" version. So, I proceeded to download the GCC - universal 32+64 bits (OS X 10.5+) version and then followed the instructions for installing and compiling SFML on linux, since there were none for mac (http://www.sfml-dev.org/tutorials/2.0/start-linux.php) by doing the following:
Since I had installed SFML in a non-standard location, I told my compiler where to find the header files like so:
-I /Users/.../Desktop/C++_Code/SMFL/SFML-2.1-osx-gcc-universal/Frameworks/SFML.framework/Headers
I also had to tell my compiler where to find the SMFL libraries:
-L /Users/.../Desktop/C++_Code/SMFL/SFML-2.1-osx-gcc-universal/lib
I needed to also tell the library loader where to find the SFML libraries from:
export LD_LIBRARY_PATH=/Users/.../Desktop/C++_Code/SMFL/SFML-2.1-osx-gcc-universal/lib
Then, I put the sfml-audio.framework, sfml-window.framework, sfml-graphics.framework, sfml-network.framework, and sfml-system.framework in my Frameworks directory (/Library/Frameworks).
Finally, I tried to compile my program in the following ways:
g++ window.cpp -I /Users/.../Desktop/C++_Code/SMFL/SFML-2.1-osx-gcc-universal/Frameworks/SFML.framework/Headers/SFML -L /users/.../Desktop/C++_Code/SMFL/SFML-2.1-osx-gcc-universal/lib -framework sfml-graphics -framework sfml-window -framework sfml-system
g++ window.cpp -framework sfml-graphics -framework sfml-window -framework sfml-system
But, I keep getting the same error. I have continued to google the error, but all I keep getting are questions where people are using Xcode and not the terminal. Any help would be greatly appreciated!
I figured it out at last! First off, as Joachim Pileborg pointed out, I need to install the clang version of SFML for mac since I was using g++ to compile which is clang++ in disguise, as Joachim Pileborg also pointed out. Next, I had to go through the instructions again for setting up SFML with Linux (http://www.sfml-dev.org/tutorials/2.0/start-linux.php).
However, the icing on the cake was when I read this question:
OSX GCC how to specify sfml library source?
...and this question:
FreeType error when building in XCode
...which got me thinking: maybe I should try moving my freetype.framework folder (which can be found in the extlibs directory) into my /Library/Frameworks directory. Once I did this and compiled using
g++ window.cpp -framework sfml-graphics -framework sfml-window -framework sfml-system
It compiled and ran successfully!!! I am very happy now!