Setting up OpenCV in QT on OSX - macos

I am trying to set up OpenCV to work with QT on OSX 10.7.5/MacbookPro 2.5 Ghz Intel Core 2 Duo. I've seen a few related question here (How to link opencv in QtCreator and use Qt library and How do you set up OpenCV with QT in OSX?) but there isn't much detail. On the QT site and in my web searches all of the information seems to be about Windows or Linux.
I've listed what my setup and code is below. When I run the code I am getting an error: :-1: error: symbol(s) not found for architecture x86_64
Does this mean it's not finding stuff because the paths are wrong or because I may have built openCV for x86_32? Is there a way of checking the latter? I'm not so savvy with parts of the configuration and setup process.
Update 2
The console output is below - perhaps the error is obvious?
02:44:38: Running steps for project RP_openCV_01...
02:44:38: Configuration unchanged, skipping qmake step.
02:44:38: Starting: "/usr/bin/make"
clang++ -headerpad_max_install_names -mmacosx-version-min=10.6 -o RP_openCV_01 main.o -L/usr/local/lib -1ibopencv_core.2.4.6,dylib -1ibopencv_imgproc.2.4.6.dylib -F/Users/rise/Qt5.0.2/5.0.2/clang_64/lib -framework QtCore
clang: warning: argument unused during compilation: '-1ibopencv_core.2.4.6,dylib'
clang: warning: argument unused during compilation: '-1ibopencv_imgproc.2.4.6.dylib'
Undefined symbols for architecture x86_64:
"cv::_InputArray::_InputArray(cv::Mat const&)", referenced from:
_main in main.o
"cv::namedWindow(std::string const&, int)", referenced from:
_main in main.o
"cv::Mat::deallocate()", referenced from:
_main in main.o
"cv::imread(std::string const&, int)", referenced from:
_main in main.o
"cv::imshow(std::string const&, cv::_InputArray const&)", referenced from:
_main in main.o
"cv::waitKey(int)", referenced from:
_main in main.o
"cv::fastFree(void*)", 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: *** [RP_openCV_01] Error 1
02:44:38: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project RP_openCV_01 (kit: Desktop Qt 5.0.2 clang 64bit)
When executing step 'Make'
What I have so far (the tl;dr):
built and installed the latest version of openCV (2.4.6) from source and tested it with some command line programs.
installed the latest version of QT (5.1) and am able to run all the examples, etc.
specified the paths in the project file (below)
QT += core
QT -= gui
TARGET = RP_openCV_01
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += /usr/local/include\
LIBS += -L/usr/local/lib
tried to specify the path in the project settings (below). I added /usr/local/include and /usr/local/lib
The simple example code in main.cpp
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main() {
// read an image
cv::Mat image= cv::imread("dog.jpg");
// create image window named "My Image"
cv::namedWindow("My Image");
// show the image on window
cv::imshow("My Image", image);
// wait key for 5000 ms
cv::waitKey(5000);
return 1;
}
Update 1
Another thing I tried based on a tutorial was to specify the libs in the QT profile (as in image below). The tutorial was for Windows though and I didn't know if or how OSX is different. Where in the Windows example it is -1ibopencv_core246d I tried it with and without the separating periods but without the "d". Of course the full name of a lib is "libopencv_core.2.4.6.dylib" etc.
These sorts of important details always trip me up but it's often assumed in tutorials that one knows this stuff.
LIBS += -L/usr/local/lib \
-1ibopencv_core.2.4.6 \
-1ibopencv_imgproc.2.4.6 \
-1ibopencv_features2d.2.4.6 \
-1ibopencv_highgui.2.4.6

Just a naive mistake
The proper declaration is:
LIBS += -L/usr/local/lib \
-lopencv_core \
-lopencv_imgproc \
-lopencv_features2d\
-lopencv_highgui

I just stumbled over the same problem. Here is how I solved it:
As sansuiso already suggested, the problem appears to be the C++-STD-lib you are trying to link against. You are probably trying to link libc++ (LLVM ...) but instead libstdc++ (GNU C++ ...) should be used.
Using the Qt Creator you can add the following to your .pro-file:
mac: 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
}
This answer could also be interesting for your problem: https://stackoverflow.com/a/12852913/1141118
Hope this helps!

You are facing 2 potential issues here:
first one is, on Mac platforms, depending on the compiler (clang or gcc) you will not link against the same stdc++ library (I ran into this issue while trying to get some static opencv lib working with an iOS project). However, it does not seem to be the case here since you do not have C++ errors, but it's worth checking what compiler you use in each case;
second one, QtCreator is a graphical app. Thus it is not aware of your terminal-session settings. This includes the path to dynamic libraries in /usr/local. You can avoid this by different approaches: using a static opencv lib, using install_tool to change the path to the library coded in the output binary, or by using the preferences of QtCreator to set the DYLD_LIBRARY_PATH variable to something like /usr/local/lib when it runs your program.

Here are the steps I took to setup Opencv in QT in OSX:
1- Install QT
2- Install OpenCV4
3- Add the following lines in the project file:
INCLUDEPATH += /usr/local/include/opencv4\
LIBS += -L/usr/local/lib \
-lopencv_core \
-lopencv_imgproc \
-lopencv_features2d\
-lopencv_highgui
QMAKE_MACOSX_DEPLOYMENT_TARGET = 11.0
4- In the Project section, under the Built section, uncheck the following:
Add build library search path to DYLD_LIBRARY_PATH and DYLD_FRAMEWORK_PATH
The following links are helpful:
Dlink
Warning Version

Related

Issue when linking lapack and fftw3 libraries

I have a Fortran 90 program which uses lapack subroutines, and is successfully running on my Ubuntu system. Now I want to run it on Mac (OS X Version 10.11.4). I am using gfortran compiler as a part of gcc, installed from homebrew repositories, and lapack library, installed in /usr/local/lib/.
When I try to compile my code, I get the following error:
gfortran my_prog.f90 -L/usr/local/lib/ -llapack
Undefined symbols for architecture x86_64:
"_daxpy_", referenced from:
_zggbal_ in liblapack.a(zggbal.o)
...
"_ztrmv_", referenced from:
_zlarft_ in liblapack.a(zlarft.o)
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
After some google search I understood that the problem is because the linking. When I compile it like this, everything works well:
gfortran my_prog.f90 -llapack
Also when llapack from framework accelerate is used, the compiler doesn't complain.
gfortran my_prog.f90 -framework accelerate
The libraries are of x86-64 architecture:
lipo -info *.a
input file libfftw3.a is not a fat file
input file liblapack.a is not a fat file
Non-fat file: libfftw3.a is architecture: x86_64
Non-fat file: liblapack.a is architecture: x86_64
LAPACK is not the only one which gives me an error, later the same problem appears with FFTW3. Could you please give me a hint to the solution to this problem?
FFTW is not part of the accelerate framework. If you want to use it, you need to add -lfftw3 to the compile options as well.
If the libraries are not in the default LIBRARY_PATH, you might need to specify -L/path/to/fftw/libs as well. The same holds for the include path if you are using its modules -I/path/to/fftw/includes.
Note that the vDSP part of the library also provides FFT implementations. You might not need FFTW at all.

Xcode 6.2 OSX Undefined symbols for functions in other files

I'm sure this is a total nube question but if someone could explain to me what's going wrong I'd be very grateful.
I create a new Cocoa App in XCode. Call it LinkerTest. This basic app will build and run, putting a simple blank window up.
Add a new .cpp file using the C++ File template. Call it Test.cpp. This creates Test.h too.
In Test.cpp add a simple function:
int TestMe(void)
{
return 1;
}
Declare my function in Test.h
int TestMe(void);
In my AppDelegate.m (which was created automatically when I create the app) add
#include "Test.h"
In the applicationDidFinishLaunching method add:
printf("Test = %d\n", TestMe());
Now try to build. Everything compiles ok, but it fails to link. This is the linker command:
Ld Build/Products/Debug/LinkerTest.app/Contents/MacOS/LinkerTest normal x86_64
cd /Users/chip/LinkerTest
export MACOSX_DEPLOYMENT_TARGET=10.10
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -L/Users/chip/LinkerTest/Build/Products/Debug -F/Users/chip/LinkerTest/Build/Products/Debug -filelist /Users/chip/LinkerTest/Build/Intermediates/LinkerTest.build/Debug/LinkerTest.build/Objects-normal/x86_64/LinkerTest.LinkFileList -Xlinker -rpath -Xlinker #executable_path/../Frameworks -mmacosx-version-min=10.10 -stdlib=libc++ -fobjc-arc -fobjc-link-runtime -Xlinker -dependency_info -Xlinker /Users/chip/LinkerTest/Build/Intermediates/LinkerTest.build/Debug/LinkerTest.build/Objects-normal/x86_64/LinkerTest_dependency_info.dat -o /Users/chip/LinkerTest/Build/Products/Debug/LinkerTest.app/Contents/MacOS/LinkerTest
And this is the error I get:
Undefined symbols for architecture x86_64:
"_TestMe", referenced from:
-[AppDelegate applicationDidFinishLaunching:] in AppDelegate.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So any idea what I've done wrong? Seems to me that adding the file it should link too, but it doesn't. I know the file Test.cpp gets compiled, if I add garbage to the file, then the compiler immediately yacks on that garbage.
EDIT: A couple more things.
1) Looking at this stackoverflow(Getting XCode to include, compile and link existing (C++) codebase in XCode 4.3(.1)) question it seems similar but not my issue. I can confirm that my test.cpp is listed in my Compile Sources under Build Phases.
2) Looking in the LinkerTest/Build/Intermediates/LinkerTest.build/Debug/LinkerTest.build/Objects-normal/x86_64 folder I find Test.o, Test.d, and Test.dia which suggests to me that Test.cpp is in fact compiling. In that folder I also find LinkerTest.LinkFileList which when opened with a text editor shows that Test.o should be linked.
The answer is that when you create a default Cocoa app the AppDelegate file is a .m file. Changing that file to be AppDelegate.mm fixes the problem. My uninformed guess is that a .m file will only handle .c files and to correctly handle .cpp files you must use .mm files. Why xcode's default files for a default Cocoa App wouldn't be .mm files is beyond my pay grade, but there it is.

MathGL and FLTK cmake on Mac OS X: Not finding symbols

I'm having trouble getting cmake to find and link the necessary libraries for MathGL and FLTK.
Linking CXX executable filter.app/Contents/MacOS/filter
Undefined symbols for architecture x86_64:
"_mgl_create_graph_fltk", referenced from:
_main in filter.cpp.o
"_mgl_fltk_thr", referenced from:
_main in filter.cpp.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'm using the default FLTK finder cmake script, which comes with brewed CMake 3.0.2 on OS X.
Here are the relevant portions of my CMakeLists.txt:
find_package(MathGL 2.2 REQUIRED)
include_directories(${MATHGL2_INCLUDE_DIRS})
find_package(FLTK REQUIRED)
include_directories(${FLTK_INCLUDE_DIR})
#link_directories(${FLTK_LIBRARIES}) # tried it with and without this line
add_executable(filter ${BUNDLE_MODE} src/filter.cpp)
target_link_libraries(
filter
${MATHGL2_LIBRARIES}
${FLTK_LIBRARIES}
${OPENGL_LIBRARIES}
)
If I uncomment the link_directories line, I get an additional error:
CMake Warning (dev) at CMakeLists.txt:73 (link_directories):
This command specifies the relative path
-framework Carbon -framework Cocoa -framework ApplicationServices -lz
as a link directory.
Policy CMP0015 is not set: link_directories() treats paths relative to the
source dir. Run "cmake --help-policy CMP0015" for policy details. Use the
cmake_policy command to set the policy and suppress this warning.
It seems like it's trying to do -L-framework Carbon, etc., when these are already being linked via the BUNDLE_MODE var in add_executable. I suspect this particular FindFLTK2.cmake was not properly tested on OS X, and am not quite sure how to fix it.
Can anyone suggest a simple fix?
Found the problem. It seems that the instructions in both FindMathGL.cmake and FindMathGL2.cmake were incorrect. Here is the corrected cmake instruction set. This also works for Qt, not just FLTK.
find_package(MathGL2 REQUIRED COMPONENTS FLTK) # or Qt instead of FLTK
include_directories(${MATHGL2_INCLUDE_DIRS})
add_executable(filter ${BUNDLE_MODE} src/filter.cpp) # leave out BUNDLE_MODE if not on Darwin
target_link_libraries(
filter
${MATHGL2_LIBRARIES}
${OPENGL_LIBRARIES}
)
Much easier.

Link issues with QtMacExtras using Qt 5.0.2

I need the QtMacExtras framework to use its QMacCocoaViewContainer class.
I have been able to get the framework and compile it easily using qmake/make without issues.
However, when trying to link my binary with it I get undefined symbols errors.
The compilation of the objects works like a charm.
g++ -mmacosx-version-min=10.7 --exported_symbols_list=symbols.exp -Wl,-x -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/usr/lib -o myBinary *.o -bind_at_load -F/pathToMy/Frameworks -framework QtCore -framework QtMacExtras -framework QtGui -framework QtWidgets #...
Undefined symbols for architecture x86_64:
"QMacCocoaViewContainer::QMacCocoaViewContainer(objc_object*, QWidget*)", referenced from:
WbDockWidget::setContentWindowId(unsigned long long)in WbDockWidget.o
ld: symbol(s) not found for architecture x86_64
The code to instantiate the class is the following:
#include <QtMacExtras/QMacCocoaViewContainer>
// ...
NSView *view = reinterpret_cast<NSView *>(id);
QWidget *widget = new QMacCocoaViewContainer(view, this);
Commenting this code allows to compile my project.
The framework seems correctly linked, seems to include correctly the headers, and seems to contain the symbols (!):
$ nm -g Frameworks/QtMacExtras.framework/Versions/5/QtMacExtras | grep -i cocoa
0000000000009240 T __ZN22QMacCocoaViewContainer11qt_metacallEN11QMetaObject4CallEiPPv
0000000000009200 T __ZN22QMacCocoaViewContainer11qt_metacastEPKc
0000000000004c00 T __ZN22QMacCocoaViewContainer12setCocoaViewEP6NSView
0000000000011130 S __ZN22QMacCocoaViewContainer16staticMetaObjectE
0000000000004b60 T __ZN22QMacCocoaViewContainerC1EP6NSViewP7QWidget
0000000000004b70 T __ZN22QMacCocoaViewContainerC2EP6NSViewP7QWidget
0000000000004d10 T __ZN22QMacCocoaViewContainerD0Ev
0000000000004d40 T __ZN22QMacCocoaViewContainerD1Ev
0000000000004d90 T __ZN22QMacCocoaViewContainerD2Ev
00000000000091e0 T __ZNK22QMacCocoaViewContainer10metaObjectEv
0000000000004e20 T __ZNK22QMacCocoaViewContainer9cocoaViewEv
0000000000011360 S __ZTI22QMacCocoaViewContainer
000000000000ce70 S __ZTS22QMacCocoaViewContainer
0000000000011160 S __ZTV22QMacCocoaViewContainer
0000000000004d50 T __ZThn16_N22QMacCocoaViewContainerD0Ev
0000000000004e10 T __ZThn16_N22QMacCocoaViewContainerD1Ev
Any suggestion to be able to use this class would be more than welcome.
Sincerely.
This problem occurs when you build as C++.
The easiest way to fix this is to switch the extension to .mm, but if you force the toolchain to build as Objective C++ by whatever means it should fix the issue.

How can I compile and use Haskell as a C library using Xcode LLVM compiler?

I want to set up a project to take my .hs code, and my main .c program, and result in a statically linked executable through the use of LLVM compiler. I can get things working via ghc command line options to build a .hs, produce the stubs, and compile and link a driver application using ghc entirely. However, I get various issues within Xcode.
My first issue was that I of course need to use 32 bit compiling environment in Xcode. That solved, I had to fiddle with paths to explicitly include the HsFFI.h. That solved, I get a linker error:
Ld "build/Debug/FFI Test.app/Contents/MacOS/FFI Test" normal i386
cd "/Users/rcl/TestXCodeProjects/FFI Test"
setenv MACOSX_DEPLOYMENT_TARGET 10.6
/Developer/usr/bin/clang -arch i386
-isysroot /Developer/SDKs/MacOSX10.6.sdk
"-L/Users/rcl/TestXCodeProjects/FFI Test/build/Debug"
"-L/Users/rcl/TestXCodeProjects/FFI Test/FFI Test"
"-F/Users/rcl/TestXCodeProjects/FFI Test/build/Debug"
-filelist "/Users/rcl/TestXCodeProjects/FFI Test/build/FFI Test.build/
Debug/FFI Test.build/Objects-normal/i386/FFI Test.LinkFileList"
-mmacosx-version-min=10.6 -framework Cocoa
"/Users/rcl/TestXCodeProjects/FFI Test/FFI Test/ForeignExportCost.a"
-o "/Users/rcl/TestXCodeProjects/FFI Test/build/Debug/FFI Test.app/
Contents/MacOS/FFI Test"
Undefined symbols for architecture i386:
"_hs_init", referenced from:
-[FFI_TestAppDelegate applicationDidFinishLaunching:] in FFI_TestAppDelegate.o
"_simpleFunction", referenced from:
-[FFI_TestAppDelegate applicationDidFinishLaunching:] in FFI_TestAppDelegate.o
"_hs_exit", referenced from:
-[FFI_TestAppDelegate applicationDidFinishLaunching:] in FFI_TestAppDelegate.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The "simpleFunction" is in "ForeignExportCost.a" library which I compile using ghc like this:
ghc -no-hs-main -fPIC -c ForeignExportCost.hs
ghc -no-hs-main -shared ForeignExportCost.o -o ForeignExportCost.a
What am I missing or doing wrong?
Ugh - it looks like the answer to my question is detailed here, telling me how to painfully add a ton of .a's to my project. And this blog post gave some helpful tips to getting on the way.
Although if someone tells me "hey wait, there's an easier way than iteratively figuring out failed deps" that would be awesome. Because I want to reuse this framework several times and this is a real pain of a way to get things up and going!

Resources