Undefined symbols for architecture x86_64 Using QT with Opencv - qt

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.

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

Related

Using devtools::document to compile Rcpp code that includes foreign package

I'm trying to develop an R package (named gpuUtils) that will import C++ code that makes use of RViennaCL for GPU utilization.
I installed RViennaCL. Then, I followed Hadley's guidelines for package development and usage of C++ code, and created a C++ file with these lines:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
#include "viennacl/ocl/backend.hpp"
using namespace Rcpp;
Then, when running devtools::document I get this error:
g++ -m64 -I/usr/include/R -DNDEBUG -I/usr/local/include -I"/home/ndr/R/x86_64-redhat-linux-gnu-library/3.3/Rcpp/include" -I"/home/ndr/R/x86_64-redhat-linux-gnu-library/3.3/RcppArmadillo/include" -I"/home/ndr/R/x86_64-redhat-linux-gnu-library/3.3/RViennaCL/include" -fpic -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -c gpuUtils.cpp -o gpuUtils.o
In file included from /home/ndr/R/x86_64-redhat-linux-gnu-library/3.3/RViennaCL/include/viennacl/ocl/backend.hpp:26:0,
from gpuUtils.cpp:3:
/home/ndr/R/x86_64-redhat-linux-gnu-library/3.3/RViennaCL/include/viennacl/ocl/context.hpp:28:19: fatal error: CL/cl.h: No such file or directory
#include <CL/cl.h>
^
compilation terminated.
make: *** [gpuUtils.o] Error 1
OpenCL and CUDA are installed and I do see CL/cl.h in my system:
$ ls -1 /usr/local/cuda-9.0/include/CL
cl_egl.h
cl_ext.h
cl_gl_ext.h
cl_gl.h
cl.h
cl.hpp
cl_platform.h
opencl.h
and
$ ls -1 /usr/include/CL/
cl2.hpp
cl_egl.h
cl_ext.h
cl_gl_ext.h
cl_gl.h
cl.h
cl.hpp
cl_platform.h
opencl.h
Also, in my DESCRIPTION file I have these lines:
LinkingTo: Rcpp, RViennaCL (>= 1.7.1.7)
Imports: Rcpp
I guess that either -I/usr/include/ or -I/usr/local/cuda-9.0/include/ have to be included in my g++ call, but I'm not sure how to get Rstudio to do that.
Any idea?

Cute Report Test Fail

I'm testing a Cute report writer, working with QT. My problem is that when i add the include directory to the .pro file i get the following error:
11:50:41: Running steps for project ReportTest...
11:50:41: Starting: "C:\Qt\5.9.1\mingw53_32\bin\qmake.exe" C:\Users\dkolb\Documents\Develop\test\ReportTest\ReportTest.pro -spec win32-g++
Info: creating stash file C:\Users\dkolb\Documents\Develop\test\build-ReportTest-Desktop_Qt_5_9_1_MinGW_32bit-Release\.qmake.stash
11:50:44: The process "C:\Qt\5.9.1\mingw53_32\bin\qmake.exe" exited normally.
11:50:44: Starting: "C:\Qt\Tools\mingw530_32\bin\mingw32-make.exe" qmake_all
mingw32-make: Nothing to be done for 'qmake_all'.
11:50:48: The process "C:\Qt\Tools\mingw530_32\bin\mingw32-make.exe" exited normally.
11:50:48: Starting: "C:\Qt\Tools\mingw530_32\bin\mingw32-make.exe"
C:/Qt/Tools/mingw530_32/bin/mingw32-make -f Makefile.Release
mingw32-make[1]: Entering directory 'C:/Users/dkolb/Documents/Develop/test/build-ReportTest-Desktop_Qt_5_9_1_MinGW_32bit-Release'
'C:\Qt\5.9.1\mingw53_32\bin\uic.exe' ../ReportTest/mainwindow.ui -o ui_mainwindow.h
g++ -c -fno-keep-inline-dllexport -pipe -O2 -std=gnu++11 -Wextra -Wall -W -fexceptions -mthreads -DUNICODE -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I../ReportTest -I. -I"C:/Program Files (x86)/ExaroLogic/CuteReport/development/include/cutereport" -IC:/Qt/5.9.1/mingw53_32/include -IC:/Qt/5.9.1/mingw53_32/include/QtWidgets -IC:/Qt/5.9.1/mingw53_32/include/QtGui -IC:/Qt/5.9.1/mingw53_32/include/QtANGLE -IC:/Qt/5.9.1/mingw53_32/include/QtCore -Irelease -I. -IC:/Qt/5.9.1/mingw53_32/mkspecs/win32-g++ -o release/main.o ../ReportTest/main.cpp
Makefile.Release:346: recipe for target 'release/main.o' failed
mingw32-make[1]: Leaving directory 'C:/Users/dkolb/Documents/Develop/test/build-ReportTest-Desktop_Qt_5_9_1_MinGW_32bit-Release'
Makefile:36: recipe for target 'release' failed
mingw32-make[1]: *** [release/main.o] Error -1073741502
mingw32-make: *** [release] Error 2
11:50:58: The process "C:\Qt\Tools\mingw530_32\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project ReportTest (kit: Desktop Qt 5.9.1 MinGW 32bit)
When executing step "Make"
11:50:58: Elapsed time: 00:17.
For testing I'm using an empty gui application.It does compile when i take the include out.
I have download the 32bit version of the report writer as i'm compiling on 32bits, but i don't think that QT is even near the dlls. Here is the .pro file:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = ReportTest
TEMPLATE = app
INCLUDEPATH += "C:/Program Files (x86)/ExaroLogic/CuteReport/development/include/cutereport"
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui

Error while executing Qt console application program on Mac os X

I am trying to run a simple console application (Hello World program) on Qt 5.6.0. Here is the code:
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "Hello World";
return a.exec();
}
But it's generating error. ([main.o]error 2).
11:37:44: Running steps for project test... 11:37:44: Configuration
unchanged, skipping qmake step. 11:37:44: Starting: "/usr/bin/make"
/Applications/Xcode
7.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
-c -pipe -stdlib=libc++ -g -std=gnu++11 -isysroot /Applications/Xcode 7.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk
-mFile Doesn't Exist, Will Create: /Applications/Xcode Invalid Arguments -version-min=10.7 -Wall -W -fPIC -DQT_QML_DEBUG
-DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../test -I. -I../Qt5.6.0/5.6/clang_64/lib/QtWidgets.framework/Headers -I../Qt5.6.0/5.6/clang_64/lib/QtGui.framework/Headers -I../Qt5.6.0/5.6/clang_64/lib/QtCore.framework/Headers -I. -I/Applications/Xcode -I7.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/OpenGL.framework/Headers
-I/Applications/Xcode -I7.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/AGL.framework/Headers/
-I. -I../Qt5.6.0/5.6/clang_64/mkspecs/macx-clang -F/Users/macwaves/Qt5.6.0/5.6/clang_64/lib -o main.o ../test/main.cpp /bin/sh: -c: line 0: unexpected EOF while looking for matching `''
/bin/sh: -c: line 1: syntax error: unexpected end of file make: ***
[main.o] Error 2 11:37:44: The process "/usr/bin/make" exited with
code 2. Error while building/deploying project test (kit: Desktop Qt
5.6.0 clang 64bit) When executing step "Make" 11:37:44: Elapsed time: 00:00.
I tried to set PATH (/Users/macwaves/Qt5.6.0/5.6/clang_64/bin). Renamed my Xcode 7.app to Xcode7.app but, problem still persists.
Can anybody help?
Here is a snapshot of my problem :
I have just uninstall(5.6.0) and reinstalled the older version (5.5.1) on my mac os x. It works smoothly. :)

RInside and Rcpp in c++x11

I would like to use RInside in a c++x11 code to call R routine. By default, R seems to install a c++98 version of the library and thus the linking does not success. I wonder what are the different steps I should proceed to install c++x11 version of RInside (but I guess that I need to recompile all R/R package, is it ?) and if some more simple solution exists. Thanks in advance (I work on MacOs)
Update:
I reinstalled Rcpp and RInside doing (my Makevars is empty so c++98 version)
sudo R CMD INSTALL ~/Downloads/Rcpp_0.10.4.tar.gz
sudo R CMD INSTALL ~/Downloads/RInside_0.2.10.tar.gz
Then I compile the hello world example using (no cx11) :
clang++ -I/Library/Frameworks/R.framework/Versions/2.15/Headers/ -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/RInside/include/ -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/ -c ../src/Gui/test.cc -o testOut.cc.o
clang++ testOut.cc.o -o testOut -L/Library/Frameworks/R.framework/Versions/2.15/Resources/library/RInside/lib/x86_64 -L/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/lib/x86_64 -framework R -lRInside -lRcpp
That produces me:
Hello, world!
However, by adding the x11 option:
clang++ -I/Library/Frameworks/R.framework/Versions/2.15/Headers/ -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/RInside/include/ -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/ -c ../src/Gui/test.cc -o testOut.cc.o -stdlib=libc++ -std=c++11
clang++ testOut.cc.o -o testOut -L/Library/Frameworks/R.framework/Versions/2.15/Resources/library/RInside/lib/x86_64 -L/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/lib/x86_64 -framework R -lRInside -lRcpp -stdlib=libc++ -std=c++11
I get (when linking):
Undefined symbols for architecture x86_64:
"RInside::parseEvalQ(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
_main in testOut.cc.o
"RInside::operator[](std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
_main in testOut.cc.o
"Rcpp::Environment::assign(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, SEXPREC*) const", referenced from:
bool Rcpp::Environment::assign<char [15]>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const (&) [15]) const in testOut.cc.o
When recompiling Rcpp and RInside using c++x11 options
clang++ -I/Library/Frameworks/R.framework/Versions/2.15/Headers/ -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/RInside/include/ -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/ -c ../src/Gui/test.cc -o testOut.cc.o -stdlib=libc++ -std=c++11
clang++ testOut.cc.o -o testOut -L/Library/Frameworks/R.framework/Versions/2.15/Resources/library/RInside/lib/x86_64 -L/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/lib/x86_64 -framework R -lRInside -lRcpp -stdlib=libc++ -std=c++11
liking is ok but ./test gives me a seg fault.
Add: my clang version is : Apple clang version 4.1 (tags/Apple/clang-421.11.66)
R is compiled with a c compiler. You don't necessarily need to use the same to build packages. So it is perfectly valid to use clang to compile add on packages. I have this in my ~/.R/Makevars file to use clang and C++11 for package:
CC=clang
CXX=clang++
CXXFLAGS= -stdlib=libc++ -std=c++11
Also, you might want to have a look at Rcpp11, a C++11 centric redesign of Rcpp. It probably would not be too hard to fork RInside to use Rcpp11 instead of Rcpp.
I am not aware of different linker requirements. What you have should just work. Otherwise please show your actual linking steps and errors, preferably in a reproducible way I could look at at my end too.
Witness the following. We first build with default flags:
edd#max:~/svn/rinside/pkg/inst/examples/standard$ grep ^CXXFLAGS ~/.R/Makevars
CXXFLAGS= -g -O3 -Wall -pipe -Wno-unused -pedantic # plain C++
edd#max:~/svn/rinside/pkg/inst/examples/standard$ make rinside_sample0
ccache g++-4.7 -I/usr/share/R/include \
-I/usr/local/lib/R/site-library/Rcpp/include \
-I/usr/local/lib/R/site-library/RInside/include -g -O3 -Wall \
-pipe -Wno-unused -pedantic -Wall \
rinside_sample0.cpp -L/usr/lib/R/lib -lR -lblas \
-llapack -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp \
-Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib \
-L/usr/local/lib/R/site-library/RInside/lib \
-lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib \
-o rinside_sample0
edd#max:~/svn/rinside/pkg/inst/examples/standard$ ./rinside_sample0
Hello, world!
edd#max:~/svn/rinside/pkg/inst/examples/standard$
Now I am simply adding -std=c++11 to my CXXFLAGS:
edd#max:~/svn/rinside/pkg/inst/examples/standard$ grep ^CXXFLAGS ~/.R/Makevars
CXXFLAGS= -g -O3 -Wall -pipe -Wno-unused -pedantic -std=c++11
edd#max:~/svn/rinside/pkg/inst/examples/standard$
and remake the binary:
edd#max:~/svn/rinside/pkg/inst/examples/standard$ rm rinside_sample0
edd#max:~/svn/rinside/pkg/inst/examples/standard$ make rinside_sample0
ccache g++-4.7 -I/usr/share/R/include \
-I/usr/local/lib/R/site-library/Rcpp/include \
-I/usr/local/lib/R/site-library/RInside/include \
-g -O3 -Wall -pipe -Wno-unused \
-pedantic -std=c++11 -Wall \
rinside_sample0.cpp -L/usr/lib/R/lib -lR \
-lblas -llapack -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp \
-Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib \
-L/usr/local/lib/R/site-library/RInside/lib \
-lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib \
-o rinside_sample0
edd#max:~/svn/rinside/pkg/inst/examples/standard$ ./rinside_sample0
Hello, world!
edd#max:~/svn/rinside/pkg/inst/examples/standard$
To deploy C++11, you do not need a new compiler or project.
Just add a single option to your compiler flags -- it is that easy.
And just to make it more plain, we can also add these three lines to rinside_sample0.cpp to make it a C++11 program:
auto magic = 42; // C++11
R["magic"] = magic;
R.parseEvalQ("print(magic)");
and (with the required -std=c++11 flag) it too builds and runs. Stricly no change required at the Rcpp or RInside end to deploy them with a C++11-capable compiler.

undefined reference to `tan' but math.h has been included

I want to compile the sample pthread code from http://pages.cs.wisc.edu/~travitch/pthreads_primer.html (Mutex section). As I run this command:
gcc -pedantic -Wall -o theaded_program pth.c -lpthread
which is stated in the link, I get this error
pth.c:45:5: warning: ISO C90 forbids mixed declarations and code [-pedantic]
/tmp/ccajksBv.o: In function `opponent':
pth.c:(.text+0x4a): undefined reference to `tan'
/tmp/ccajksBv.o: In function `main':
pth.c:(.text+0x131): undefined reference to `tan'
collect2: ld returned 1 exit status
However #include <math.h> is there in the code!! The gcc version is 4.6
You should add -lm to your compiler option.
Besides of that, you could also change -lpthread to -pthread.
In the end, it has to be like this: gcc -pedantic -Wall -o theaded_program pth.c -pthread -lm

Resources