cannot find -lopencv_contrib cannot find -lopencv_legacy - c++

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

Related

program comiled ok using pkg-config --cflgas opencv and pkg-config --ldflag but not executable (ubuntu 16.04)

Recently I installed opencv on my ubuntu 16.04 machine(I've done this many times before and used it ok). I tried compiling yolo program, it compiled ok and ran ok. When I set OPENCV=1 in the Makefile, it compiles the program with opencv. To set the compile and link flags it has these two lines.
LDFLAGS+= `pkg-config --libs opencv` -lstdc++
COMMON+= `pkg-config --cflags opencv`
My pkg-config commands work correctly. See the output below.
ckim#chan-ubuntu:~/YOLOV2/darknet$ pkg-config --cflags opencv
-I/home/ckim/opencv_install/installation/OpenCV-3.4/include/opencv -I/home/ckim/opencv_install/installation/OpenCV-3.4/include
ckim#chan-ubuntu:~/YOLOV2/darknet$ pkg-config --libs opencv
-L/home/ckim/opencv_install/installation/OpenCV-3.4/lib -lopencv_stitching -lopencv_videostab -lopencv_superres -lopencv_surface_matching -lopencv_dnn_objdetect -lopencv_line_descriptor -lopencv_aruco -lopencv_img_hash -lopencv_xobjdetect -lopencv_dpm -lopencv_freetype -lopencv_stereo -lopencv_face -lopencv_objdetect -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_saliency -lopencv_hfs -lopencv_ccalib -lopencv_tracking -lopencv_datasets -lopencv_plot -lopencv_optflow -lopencv_ximgproc -lopencv_fuzzy -lopencv_bioinspired -lopencv_highgui -lopencv_videoio -lopencv_text -lopencv_dnn -lopencv_reg -lopencv_hdf -lopencv_bgsegm -lopencv_rgbd -lopencv_sfm -lopencv_xfeatures2d -lopencv_calib3d -lopencv_shape -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_ml -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
A couple of days ago, I tried compiling a simple opencv pgrogram that I find on a web page and tried compiling it. (Iwanted to test a basic display window's behavior). The program was like this. very typical)
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(1); // Wait for a keystroke in the window
return 0;
}
When I compile and link it with command
gcc -c test.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv` -o test
it gives me no error and I see the file test but it's not executable. When I run it after setting chmod +x test, I get
ckim#chan-ubuntu:~/opencvtest$ ./test
bash: ./test: cannot execute binary file: Exec format error
ckim#chan-ubuntu:~/opencvtest$ file test
test: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
What could be wrong?
As per the comment you're command line...
gcc -c test.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv` -o test
includes the -c flag which tells the compiler to compile only -- don't link. Also. as you're code is c++ rather than c you need to link with the correct runtime libraries. That means using g++ rather than gcc. So the command should be...
g++ test.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv` -o test

Running OpenCV in Mac OS terminal with opencv_contrib

I'm trying to compile and run OpenCV through the command line. I can get the main Open CV libraries to work however I think my problem is with using the opencv_contrib libraries.
I have installed Open CV with home brew.
I can compile and run the below code fine from another SO question:
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main( int argc, char** argv )
{
Mat src = Mat(Size(320,240),CV_64F);;
namedWindow("test");
cout << "press any key to close" << endl;
while(true){
randn(src,0,1.0);
imshow("test",src);
if(waitKey() > 0) break;
}
}
This is compiled like this:
g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" -lopencv_core -lopencv_highgui -o cv
Then ran ./main
However when I try and run anything with the opencv_contrib libraries I get this error when compiled like this:
g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core -o cv
error:
Undefined symbols for architecture x86_64:
"cv::xfeatures2d::SURF::create(double, int, int, bool, bool)", referenced from:
_main in cv-f48298.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 believe this is likely due to not specifying the opencv_contrib libraries. I understand they come installed with homebrew. In my Cellar directory I have a directory for opencv and opencv_contrib. I'm not sure if the opencv_contrib directory needs to be located within the opencv directory.
But I believe everything is there as with this:
pkg-config --libs --cflags opencv
It outputs:
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core
But then if I add that to my command like:
g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" pkg-config --libs --cflags opencv -o cv
I get this:
clang: error: unsupported option '--libs'
clang: error: unsupported option '--cflags'
clang: error: no such file or directory: 'pkg-config'
clang: error: no such file or directory: 'opencv'
I'm able to compile the same code in Xcode and I only needed to add the search paths for the openCV directory and add the libs within there. I can run the code by opening the products folder in finder and running ./cv with images passed in. But I'm struggling with doing this all through the command line.
Any help would be great!
You want to add the output of the pkgconfig tool (you listed it above: -I/usr/local/include/opencv ...) to your compilation command line to compile correctly.
You could just manually copy this output text into your compiler invocation command:
g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core -o cv
This is usually automated by calling pkgconfig inline:
g++ cv.cpp -I"/usr/local/Cellar/opencv/3.4.1_5/include" -L"/usr/local/Cellar/opencv/3.4.1_5/lib/" `pkg-config --libs --cflags opencv` -o cv
Notice the backticks in this though - they are missing in your command line. With the backticks pkgconfig will be executed and the output (which you know what it looks like) will be inserted into the command line.

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

OpenCv linking error ( gcc, windows 7 x64)

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

How can I pass flags to R when it is compiling C++ code to be used in a package?

I am trying to use some code from OpenCV in an R package, using Rcpp to build the package. When I compile the c code on my machine, it works fine.
For example, I am using the the following syntax locally to compile the facedetect.cpp code:
g++ `pkg-config --cflags opencv` facedetect.cpp -o facedetect `pkg-config --libs opencv`
However, when I try to include it in my package using the following command:
R CMD SHLIB facedetect.cpp -o facedetect
with the following defined in my makevars file:
PKG_CPPFLAGS= `$(R_HOME)/bin/Rscript -e 'Rcpp:::CxxFlags()'`
PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"`
PKG_CXXFLAGS= `pkg-config --cflags opencv` `pkg-config --libs opencv`
R executes the following:
g++ -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/x86_64 `pkg-config --cflags opencv` `pkg-config --libs opencv` `/Library/Frameworks/R.framework/Resources/bin/Rscript -e 'Rcpp:::CxxFlags()'` -I/usr/local/include -fPIC -g -O2 -c facedetect.cpp -o facedetect.o
which gives me the following error messages:
i686-apple-darwin10-g++-4.2.1: -lopencv_core: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_imgproc: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_highgui: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_ml: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_video: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_features2d: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_calib3d: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_objdetect: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_contrib: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_legacy: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_flann: linker input file unused because linking not done
g++ -arch x86_64 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -o facedetect facedetect.o -I/opt/local/include/opencv -I/opt/local/include -L/opt/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
I do not understand these error messages, because I do not have enough experience with C++. Does anyone know how to get R to compile the C++ code as my local g++ compiler does? I'm not sure if the "-c" flag is the problem... Unfortunately I could not find the answer via google or the Writing R Extensions manual. Thanks!
Thanks to previous responders who helped me figure out the initial problems I was having with the flags.
You need to put a Makevars-File into your src directory and specify PKG_CPPFLAGS (preprocessor & includes) and PKG_CXXFLAGS (compiler flags). Details are in sections 1.2.1 and 5.5 in "Writing R Extensions".
This is a Makevars file that actually worked for me:
PKG_LIBS = `pkg-config --libs opencv`
PKG_CFLAGS = `pkg-config --cflags opencv`
PKG_CXXFLAGS = `pkg-config --cflags opencv` `Rscript -e 'Rcpp:::CxxFlags()'`
PKG_CFLAGS = `pkg-config --cflags opencv`
Hope this helps.