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
Related
This question already has answers here:
undefined reference to `std::__cxx11::basic_string in Boost on Travis CI
(2 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 11 months ago.
I'm facing the apparently very typical undefined reference with OpenCV linking with g++. I'm running in two machines, both with opencv/2.3.14.6 installed, linux OS, same code to compile, using pkg-config. I checked 'pkg-config opencv --libs' and in both is the same result
-L/usr/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui
-lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo
-lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -lrt -lpthread -lm -ldl
The machine where it fails has CentOS Linux release 7.9.2009 (will call it centos machine), the one were compilation passes Linux version 4.19.0-9-amd64 (will call it debian machine).
In my code, I use #include <opencv2/opencv.hpp>, which should include all opencv libs, however, I tried also including the specific libs, same result, debian machine all good, centos machine fails. I even tried running one of the examples from OpenCV documentation where most of the functions I get flagged are being used, got the same result. This is part of the error stack, same error goes for imread, imshow, namedwindow and videowriter, everything else seems fine:
test.cpp:(.text+0x1af4): undefined reference to `cv::putText(cv::Mat&, std::cxx11::basic_string<char, std::char_traits, std::allocator > const&, cv::Point, int, double, cv::Scalar, int, int, bool)'
I checked and all those functions do exist in the libs being included, for example, putText is contained in opencv2/core/core.hpp
CV_EXPORTS_W void putText( Mat& img, const string& text, Point org,
int fontFace, double fontScale, Scalar color,
int thickness=1, int lineType=8,
bool bottomLeftOrigin=false );
This is the command line I'm using to compile
g++ -Wall -std=c++11 -o test test.cpp `pkg-config --cflags --libs opencv`
Also tried this next one, just in case
g++ -Wall -std=c++11 -o test test.cpp `pkg-config opencv --cflags --libs`
And this
g++ -Wall -std=c++11 -o test test.cpp $(pkg-config opencv --cflags --libs)
Also tried replacing pkg-config opencv --cflags --libs with the direct libs list, still not able to compile in centos machine, and I do need to run in centos machine, but am unable to, most posts I've checked are solved with adding missing pkg-config opencv --cflags --libs to command line, code syntax error in files to compile, missing file or reinstalling opencv, but none of those help me. Also tried using different gcc versions as recommended here, but same result. I wonder if there is something else I can try or if I'm doomed to use debian machine (centos machine is an HPC machine, code takes a week to run in debian machine!).
EDIT: for anyone having similar issues, see comments and check dual ABI, in my case, I was trying to use external libraries compiled with different gcc versions, solution is to ensure they are all compiled using compatible compiler versions.
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).
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.
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`
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