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

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`

Related

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

Boost compiling flags gcc errors

I have a problem with compiling the example form LenMuse - this is the example.
I used flags from the provided tutorial and I added a few more to get rid of some other problems, but I can't solve all of them. This is my output from gcc:
/usr/lib/liblomse.so: undefined reference to `boost::thread::start_thread()'
/usr/lib/liblomse.so: undefined reference to `boost::thread::timed_join(boost::posix_time::ptime const&)'
/usr/lib/liblomse.so: undefined reference to `boost::this_thread::sleep(boost::posix_time::ptime const&)'
/usr/lib/liblomse.so: undefined reference to `boost::thread::~thread()'
This is the command I used:
gcc interface.cpp -o interface `pkg-config --cflags x11` `pkg-config --cflags liblomse` `pkg-config --libs liblomse` `pkg-config --libs x11` -lstdc++ -lboost_system -I/usr/include/boost/ -lboost_filesystem -lboost_thread
I tried to compile with the flag -lboost_thread-mt but gcc cannot find this flag.
I have boost 1.53.0, on the page author says that is required boost 1.43 or higher. My system is ArchLinux.
I solved this problem by downgrade boost library to version 1.49.0.

OpenCV 2.4.2: Undefined References

libcv-dev install
10.04
Any ideas as to where the following might be defined?
ahcarpenter#ahcarpenter-laptop:~$ g++ objectmarker.o -o objectmarker
objectmarker.o: In function `on_mouse(int, int, int, int, void*)':
objectmarker.cpp:(.text+0x12f): undefined reference to `cvCloneImage'
objectmarker.cpp:(.text+0x1d1): undefined reference to `cvRectangle'
objectmarker.cpp:(.text+0x1ea): undefined reference to `cvShowImage'
objectmarker.cpp:(.text+0x1f4): undefined reference to `cvReleaseImage'
objectmarker.o: In function `main':
objectmarker.cpp:(.text+0x391): undefined reference to `cvNamedWindow'
objectmarker.cpp:(.text+0x3aa): undefined reference to `cvSetMouseCallback'
objectmarker.cpp:(.text+0x4da): undefined reference to `cvLoadImage'
objectmarker.cpp:(.text+0x50f): undefined reference to `cvShowImage'
objectmarker.cpp:(.text+0x519): undefined reference to `cvWaitKey'
objectmarker.cpp:(.text+0x53f): undefined reference to `cvReleaseImage'
objectmarker.cpp:(.text+0x54e): undefined reference to `cvDestroyWindow'
objectmarker.cpp:(.text+0xd7f): undefined reference to `cvReleaseImage'
objectmarker.cpp:(.text+0xdf3): undefined reference to `cvDestroyWindow'
collect2: ld returned 1 exit status
I tried this and it worked for me:
sudo g++ -I/usr/include/opencv main.cpp -o main -lopencv_core -lopencv_imgproc
-lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d
-lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann -I /lib/
-I /home/ubuntu/micros/opencv_directory/include/opencv
I got it from: https://askubuntu.com/questions/239891/opencv-program-wont-compile-quantal
It seems you're not linking against the OpenCV libraries; according to this guide, one way to compile an OpenCV program (after OpenCV has been properly configured) is
g++ `pkg-config opencv --cflags` my_code.cpp -o my_code `pkg-config opencv --libs`
If your C++ code is compiled in a separate step, you can probably drop the --cflags part.
You need to include libraries using pkg-config and cflags.
$ g++ -ggdb `pkg-config --cflags opencv` -o `basename filename.cpp .cpp` filename.cpp `pkg-config --libs opencv
see this link for more details http://www.jayrambhia.com/blog/2012/05/08/beginning-opencv/

Undefined reference to `mysql_init'

I am trying to compile my program on my new server, but it's not working for me at the moment.
Error log is:
rasmus#web01:~/c++$ make test
g++ `mysql_config --cflags --libs` main.cpp logger.cpp cpulogger.cpp -o test
/tmp/ccPaMZUy.o: In function `CPULogger':
/home/rasmus/c++/cpulogger.cpp:7: undefined reference to `mysql_init'
/home/rasmus/c++/cpulogger.cpp:8: undefined reference to `mysql_real_connect'
/home/rasmus/c++/cpulogger.cpp:10: undefined reference to `mysql_get_client_info'
/tmp/ccPaMZUy.o: In function `~CPULogger':
/home/rasmus/c++/cpulogger.cpp:16: undefined reference to `mysql_close'
collect2: ld returned 1 exit status
make: *** [all] Error 1
As you can see I am compiling against MySQL - I have checked that mysql.h is present in include paths.
What am I missing?
cpulogger.cpp has #include "cpulogger.h" at the top, then cpulogger.h has this:
#include <iostream>
#include <fstream>
#include <mysql/mysql.h>
The compiler does not complain about missing mysql/mysql.h, so that part must work?
Output of mysql_config:
rasmus#web01:~/c++$ mysql_config --cflags --libs
-I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -g
-L/usr/lib -lmysqlclient -lpthread -lz -lm -lrt -ldl
Makefile:
all:
g++ `mysql_config --cflags --libs` main.cpp logger.cpp cpulogger.cpp -o test
test: all
./test
It's a fresh Ubuntu server installation with a mysql-server install on it.
[solved]:
Putting linker libraries at the end of the compiler commands works.
all:
g++ main.cpp logger.cpp cpulogger.cpp -o test `mysql_config --cflags --libs`
See answer below for explanation.
The order of arguments to the linker is significant. Use mysql-config after listing the files that need it. The linker will see that cpulogger.o needs mysql_init and look in libraries listed after it for the symbol. If the libraries were listed earlier in the arguments they won't be searched again.

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!).