OpenCv linking error ( gcc, windows 7 x64) - c++

I have coded OpenCv program on Linux.
Now I'm porting it to Windows ( gcc, windows 7 x64).
Includes are:
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
The compilation command on the Linux (working well):
g++ `pkg-config --libs --cflags opencv` -I. -o tester tester.c -lopencv_core
-lopencv_calib3d -lopencv_imgproc -lopencv_highgui -lopencv_objdetect
The compilation command on the Windows is:
g++ -I"C:/opencv/build/include" -L"C:\opencv\build\x86\vc12\lib"
tester.cpp -lopencv_core249 -lopencv_calib3d249
-lopencv_imgproc249 -lopencv_highgui249 -lopencv_objdetect249 -o tester
The last time i was using vc12 opencv libs for mingw it was working well.
Compilation error is:
C:\Users\Lukasz\AppData\Local\Temp\ccZXWyKF.o:tester.cpp:(.text+0x115): undefine
d reference to `cv::CascadeClassifier::load(std::string const&)'
C:\Users\Lukasz\AppData\Local\Temp\ccZXWyKF.o:tester.cpp:(.text+0x194): undefine
d reference to `cv::VideoCapture::VideoCapture(std::string const&)'
C:\Users\Lukasz\AppData\Local\Temp\ccZXWyKF.o:tester.cpp:(.text+0x1be): undefine
d reference to `cv::VideoCapture::isOpened() const'
C:\Users\Lukasz\AppData\Local\Temp\ccZXWyKF.o:tester.cpp:(.text+0x208): undefine
d reference to `cv::VideoCapture::get(int)'
(...)
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\L
ukasz\AppData\Local\Temp\ccZXWyKF.o: bad reloc address 0x0 in section `.data'
collect2.exe: error: ld returned 1 exit status
It may be very lame question, but i need someone with fresh look. I'm just tired.
I was using awfull CMD and PowerShell... but in the MinGW shell (C:\MinGW\msys\1.0) - same error when using pkg-config:
g++ `pkg-config --cflags opencv` -I. -o tester tester.c -lopencv_core -lopencv_calib3d -lopencv_imgproc -lopencv_highgui -lopencv_objdetect `pkg-config --libs opencv`
sh: pkg-congfig: command not found

Related

cannot find -lopencv_contrib cannot find -lopencv_legacy

I have installed opencv it compiled 100%, i have run a command:
pkg-config --cflags --libs opencv
It's output is:-I/usr/include/opencv -I/usr/include/opencv4 -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -lopencv_core -lopencv_ml -lopencv_features2d -lopencv_objdetect -lopencv_flann -lopencv_video -lopencv_highgui
But when i try to compile a c++ code it gives cannot find error,
I don't know what to do.
Thanks..
~/cpp_test$
g++ main.cpp -o output `pkg-config --cflags --libs opencv`
/usr/bin/x86_64-linux-gnu-ld: cannot find -lopencv_contrib
/usr/bin/x86_64-linux-gnu-ld: cannot find -lopencv_legacy
collect2: error: ld returned 1 exit status
If you installed OpenCV 4, I'm pretty confident that you met the same problem as mine.
The solution is actually pretty simple, you just need to run
g++ main.cpp -o output `pkg-config --cflags --libs opencv4`
instead of
g++ main.cpp -o output `pkg-config --cflags --libs opencv`
after installation of Opencv, symbolic linking has to be done to make a link for the library to a known lib location.
Try the command on terminal sudo ldconfig, to dynamically link the library.
And then compile g++ main.cpp -o output $(pkg-config --cflags --libs opencv).

C++: undefined reference to std::thread while compiling opencv and tensorflow with g++

I want to perform some preprocessing, with opencv, on an image which will be fed to a tensorflow model.
The preprocessing part reads in a .png file, the code works as a standalone program, but I want to use it inside the program that runs the tensorflow model, so I generated an object file as below. The program for preprocessing works perfectly as standalone.
g++ -std=c++11 preProcess.cpp -I/home/dpk/anaconda2/include/libpng16 -I/usr/local/include/opencv2 -L/home/dpk/anaconda2/lib -lpng16 -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_core -c
The tensorflow program, Inference.cc was compiled as below
g++ -std=c++11 preProcess.o Inference.cc -I/home/dpk/anaconda2/include/libpng16 -I/usr/local/include/opencv -I/usr/local/include/opencv2 -I/usr/local/include/tf -I/usr/local/include/eigen3 -I/usr/local/include/tf/bazel-genfiles -g -Wall -D_DEBUG -Wshadow -Wno-sign-compare -w -L/usr/local/lib/libtensorflow_cc -L/home/dpk/anaconda2/lib -lpng16 -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_flann -lopencv_core `pkg-config --cflags --libs protobuf` -ltensorflow_cc -o inference
This throws the following error
/usr/local/lib/libtensorflow_cc.so: undefined reference to `std::thread::_State::~_State()#GLIBCXX_3.4.22'
/usr/local/lib/libtensorflow_cc.so: undefined reference to `std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)())#GLIBCXX_3.4.22'
/usr/local/lib/libtensorflow_cc.so: undefined reference to `typeinfo for std::thread::_State#GLIBCXX_3.4.22'
This error seems to be caused by linking -L/home/dpk/anaconda2/lib, because compilation goes smoothly when I don't link that folder , but then the program fails to read .png files. Hence it appears that, that folder is necessary for handling .png.
I need my program to read .png files as well as run the tensorflow model. How can I make both of them work?
Tensorflow is built with gcc 6 because there is a linker bug which prevents the successful compilation with gcc 5. You must build and link your program with gcc 6 to avoid this linker error.

Undefined reference to `g_mutex_lock' when compiling with OpenCV and caffe

When I compile my code that uses OpenCV and caffe, I get the following linker errors:
../libs/libopencv_highgui.so: undefined reference to `g_mutex_lock'
../libs/libgstbase-0.10.so.0: undefined reference to `g_cond_init'
../libs/libopencv_highgui.so: undefined reference to `g_mutex_unlock'
../libs/libopencv_highgui.so: undefined reference to `g_cond_broadcast'
../libs/libgstreamer-0.10.so.0: undefined reference to `g_cond_wait_until'
../libs/libopencv_highgui.so: undefined reference to `g_mutex_new'
...
collect2: ld returned 1 exit status
This my g+ commmand:
g++ -DCPU_ONLY=1 test.cpp -o test -I../include -I../include/openblas -L../libs
-Wl,-rpath=../libs -lcaffe -lglog -lboost_system -lopencv_core -lopencv_highgui
-lopencv_imgproc
What am I missing?
Yep, this is a simple case of not linking against glib.
In general, to fix this kind of thing, google one of the symbol names(g_mutex_lock, for example) to figure out what library it comes from. In this case, it is glib. Then, look in their documentation for help compiling applications that use their library(if you can't figure it out on your own). In this case, you end up here.
Once you have made sure you have all required packages installed, you augment your compiler options as needed.
In your case, try g++ -DCPU_ONLY=1 test.cpp -o test `pkg-config --cflags glib-2.0` -I../include -I../include/openblas `pkconfig --libs glib-2.0` -L../libs -Wl,-rpath=../libs -lcaffe -lglog -lboost_system -lopencv_core -lopencv_highgui -lopencv_imgproc
Note the use of pkg-config. For reference, compiling a sample application that uses only glib might look like this: gcc hello.c `pkg-config --cflags --libs glib-2.0`

c++ compile error when I try to learn opencv_train_features

I'm jackaroo to learn how to train features of opencv, and I refer to these under url.
http://coding-robin.de/2013/07/22/train-your-own-opencv-haar-classifier.html
And above article author recommand to learn with his project, like here.
https://github.com/mrnugget/opencv-haar-classifier-training
And I study step by step,
but in the same mould after I copy mergevec.cpp to my opencv apps's directory, like this
cp src/mergevec.cpp ~/opencv-2.4.9/apps/haartraining
cd ~/opencv-2.4.9/apps/haartraining
And then, I want to obtain executable file mergevec by using the following methods
g++ `pkg-config --libs --cflags opencv` -I. -o mergevec mergevec.cpp\
cvboost.cpp cvcommon.cpp cvsamples.cpp cvhaarclassifier.cpp\
cvhaartraining.cpp\
-lopencv_core -lopencv_calib3d -lopencv_imgproc -lopencv_highgui -lopencv_objdetect
And I get error
/tmp/cc9GpmMW.o: In function `JpgDatasetGenerator::JpgDatasetGenerator(char const*)':
cvhaartraining.cpp:(.text+0xafd5): undefined reference to `IOutput::createOutput(char const*, IOutput::OutputType)'
/tmp/cc9GpmMW.o: In function `PngDatasetGenerator::PngDatasetGenerator(char const*)':
cvhaartraining.cpp:(.text+0xb24d): undefined reference to `IOutput::createOutput(char const*, IOutput::OutputType)'
cvhaartraining.cpp:(.text+0xb24d): undefined reference to `IOutput::createOutput(char const*, IOutput::OutputType)'
I try to sovle the problem by looking through opecv forum's articles and found almost nothing.
So, um, could anybody help me? thanks a lot..
IOutput is an interface where their methods are declared at ioutput.h and must be implemented somewhere. I found out they were implemented at cvsamplesoutput.cpp so we just need ask gcc to compile that file. For that the correct command should be:
g++ `pkg-config --libs --cflags opencv` -I. -o mergevec mergevec.cpp\
cvboost.cpp cvcommon.cpp cvsamples.cpp cvhaarclassifier.cpp\
cvhaartraining.cpp cvsamplesoutput.cpp\
-lopencv_core -lopencv_calib3d -lopencv_imgproc -lopencv_highgui -lopencv_objdetect

Compiling a basic OpenCV + Cuda program on linux

I've worked with opencv on linux in the past, but not with cuda. I've struggled with the following compilation error for months. And after trying many solutions i gave up and worked with windows. However, i really want to work on linux. This is the command i'm using to compile the threshold example given on the opencv_gpu website.
nvcc `pkg-config --libs opencv` -L. -L/usr/local/cuda/lib -lcuda -lcudart `pkg-config --cflags opencv` -I. -I/usr/local/cuda/include threshold.cpp -o threshold
here is the error:
/tmp/tmpxft_0000171b_00000000-1_threshold.o: In function `main':
threshold.cpp:(.text+0x124): undefined reference to `cv::gpu::Stream::Null()'
threshold.cpp:(.text+0x156): undefined reference to `cv::gpu::threshold(cv::gpu::GpuMat const&, cv::gpu::GpuMat&, double, double, int, cv::gpu::Stream&)'
threshold.cpp:(.text+0x16d): undefined reference to `cv::gpu::GpuMat::download(cv::Mat&) const'
/tmp/tmpxft_0000171b_00000000-1_threshold.o: In function `cv::gpu::GpuMat::GpuMat(cv::Mat const&)':
threshold.cpp:(.text._ZN2cv3gpu6GpuMatC1ERKNS_3MatE[cv::gpu::GpuMat::GpuMat(cv::Mat const&)]+0x63): undefined reference to `cv::gpu::GpuMat::upload(cv::Mat const&)'
/tmp/tmpxft_0000171b_00000000-1_threshold.o: In function `cv::gpu::GpuMat::~GpuMat()':
threshold.cpp:(.text._ZN2cv3gpu6GpuMatD1Ev[cv::gpu::GpuMat::~GpuMat()]+0xd): undefined reference to `cv::gpu::GpuMat::release()'
collect2: ld returned 1 exit status
make: *** [all] Error 1
In order to help you I had to download and install CUDA 4.0 (with driver 4.0.21) and then download and compiled OpenCV 2.3 for my Macbook Pro, on Mac OS X 10.6.8.
The sample code from OpenCV_GPU was successfully compiled on my machine through:
g++ threshold.cpp -o threshold `pkg-config --cflags --libs opencv` -lopencv_gpu
You were missing the flag -lopencv_gpu , which is not included by pkg-config.
This looks like a linker problem. I don't know, if nvcc follows the same conventions as gcc, but I would try:
nvcc `pkg-config --cflags opencv` -L. -L/usr/local/cuda/lib -I. -I/usr/local/cuda/include -o threshold threshold.cpp `pkg-config --libs opencv` -lcuda -lcudart
More in general: If you write
gcc t.cpp -lB -lA
it means that libB depends on symbols from libA; t.cpp can depend on symbols from libA and libB.
Instead of using pkg-config in the nvcc line I would suggest just manually pointing the compiler at the opencv library and include files. Perhaps you could just run pkg-config --libs opencv on the command line and copy the necessary libs into your nvcc command. It seems nvcc is only choking on the opencv libs (it can't find them for sure!).