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.
Related
Apple Mach-O Linker (ld) Error Group , found many existing question with same tag, however none did fix my issue.
I am trying to include OpenCV with my project in order to track hand position. And so far everything works fine, however when I build the project. It gives the error : Apple Mach-O Linker (ld) Error Group .
From the log here is what I obtained :
Ld /Users/shaheenakader/Library/Developer/Xcode/DerivedData/handSeg-fiojpqxgqvrsbxadwvnbdsljpycy/Build/Products/Debug/handSeg normal x86_64
cd /Users/shaheenakader/Documents/tracking
export MACOSX_DEPLOYMENT_TARGET=10.9
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -L/Users/shaheenakader/Library/Developer/Xcode/DerivedData/handSeg-fiojpqxgqvrsbxadwvnbdsljpycy/Build/Products/Debug -L/usr/local/lib -F/Users/shaheenakader/Library/Developer/Xcode/DerivedData/handSeg-fiojpqxgqvrsbxadwvnbdsljpycy/Build/Products/Debug -filelist /Users/shaheenakader/Library/Developer/Xcode/DerivedData/handSeg-fiojpqxgqvrsbxadwvnbdsljpycy/Build/Intermediates/handSeg.build/Debug/handSeg.build/Objects-normal/x86_64/handSeg.LinkFileList -mmacosx-version-min=10.9 -Xlinker -object_path_lto -Xlinker /Users/shaheenakader/Library/Developer/Xcode/DerivedData/handSeg-fiojpqxgqvrsbxadwvnbdsljpycy/Build/Intermediates/handSeg.build/Debug/handSeg.build/Objects-normal/x86_64/handSeg_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -stdlib=libc++ -lopencv_calib3d -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videoio -lopencv_videostab -lopencv_core.2.4.10 -lopencv_objdetect.2.4.10 -lopencv_highgui.2.4.10 -lopencv_imgproc.2.4.10 -Xlinker -dependency_info -Xlinker /Users/shaheenakader/Library/Developer/Xcode/DerivedData/handSeg-fiojpqxgqvrsbxadwvnbdsljpycy/Build/Intermediates/handSeg.build/Debug/handSeg.build/Objects-normal/x86_64/handSeg_dependency_info.dat -o /Users/shaheenakader/Library/Developer/Xcode/DerivedData/handSeg-fiojpqxgqvrsbxadwvnbdsljpycy/Build/Products/Debug/handSeg
Also,
ld: library not found for -lopencv_imgcodecs
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Previously
I manually built the OpenCV library using Cmake .
And inside my Xcode project, I set header search path to : /usr/local/include
and library search path to : /usr/local/lib .
Also, set other linker flags to : -lopencv_calib3d -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videoio -lopencv_videostab
What could possibly be causing this issue, and how to resolve this issue? Any help would be much appreciated.
It’s not enough just to link libraries in Xcode. You need to add the libraries into the projects. To do so, go to /usr/local/libs and drags them to the project explorer (the left panel of Xcode) and choose to create references on the pop up window. You can uncheck the option copy if needed on that window to avoid unnecessary clones of the libraries.
I am using xcode to compile my opencv project, and I have some settings as below:
HEADER_SEARCH_PATHS = /usr/local/include
LIBRARY_SEARCH_PATHS = /usr/local/lib
OTHER_LDFLAGS = -lopencv_calib3d -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videoio -lopencv_videostab
I want to know what shall I write by terminal command rather than setting of xcode.
If you installed OpenCV with homebrew and you also installed the pkgconfig package with homebrew, the package can tell you the settings you need itself - far more accurately than you can guess them.
The easy way is to ask pkgconfig to list all the packages it knows about:
pkg-config --list-all | grep -i opencv
opencv OpenCV - Open Source Computer Vision Library
So, now you know the package name is plain and simple opencv, and you can find the flags you need like this:
pkg-config --cflags --libs opencv
-I/usr/local/Cellar/opencv/2.4.12_2/include/opencv \
-I/usr/local/Cellar/opencv/2.4.12_2/include \
-L/usr/local/Cellar/opencv/2.4.12_2/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
Which means your compilation and linking becomes simply:
g++ $(pkg-config --cflags --libs opencv) program.cpp -o program
If you do that in a Makefile, you will need to double up the $ signs.
If your system is not so well installed, you may need to find the pkgconfig file yourself. So you would do:
find /usr/local -name "opencv*pc"
/usr/local/Cellar/opencv/2.4.12_2/lib/pkgconfig/opencv.pc
Then you can access that file specifically like this:
pkg-config --cflags --libs /usr/local/Cellar/opencv/2.4.12_2/lib/pkgconfig/opencv.pc
You can use:
g++ -I ${HEADER_SEARCH_PATHS} -L ${LIBRARY_SEARCH_PATHS} ${OTHER_LDFLAGS} ${SOURCE_FILES}
I've just installed OpenCV on my Debian machine, and I'm having issues with it. I followed the install guide on the Wiki. Trying to compile the examples gives what appears to be a successful compile, however trying to run them ends up throwing an error:
fagg#hubble:~/src/OpenCV-2.3.1/samples/cpp$ g++ -Wall em.cpp
-lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video
-lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect
-lopencv_contrib -lopencv_legacy
fagg#hubble:~/src/OpenCV-2.3.1/samples/cpp$ ./a.out
./a.out: error while loading shared libraries: libopencv_core.so.2.3:
cannot open shared object file: No such file or directory
fagg#hubble:~/src/OpenCV-2.3.1/samples/cpp$ pkg-config opencv
--libs-L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui
-lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d
-lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
fagg#hubble:~/src/OpenCV-2.3.1/samples/cpp$
But even doing this doesn't work:
fagg#hubble:~/src/OpenCV-2.3.1/samples/cpp$ g++ -Wall em.cpp
`pkg-config opencv --cflags --libs`
fagg#hubble:~/src/OpenCV-2.3.1/samples/cpp$ ./a.out
./a.out: error while loading shared libraries: libopencv_core.so.2.3:
cannot open shared object file: No such file or directory
fagg#hubble:~/src/OpenCV-2.3.1/samples/cpp$
I think there's an issue with the linking, but I'm not quite sure what's going on. This is a completely fresh install of OpenCV - I've never had it on this machine before. Does anyone have any ideas?
Ran
sudo ldconfig
Suggested by anon.coward
I have done all the necessary job like adding "c:/cygwin/bin" to system path, I have also set the include path under project->properties->c/c++ build->setting and also set the linker library search path.
Now when I include file like #include"cv.h", it gives no error, but when I run the program error comes like
undefined reference to '_cvGetSize'
I have included the necessary files, and read lots of tutorials that are available but still I couldn't able to run my code, please help me
I always recommend that before jumping to an IDE you make sure your system is installed and configured correctly by attempting to compile an application on the cmd-line:
g++ flann.cpp -o flann `pkg-config --cflags --libs opencv`
or:
g++ flann.cpp -o flann -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
Apparently you didn't link your project with the opencv libraries, which is being achieved in the cmd above with: -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann.
That missing symbol is defined in one of these libraries.
I'm having a problem with linking my application that uses Qt and OpenCV on Ubuntu.
my qmake .pro file:
SOURCES = ../../../Source/*.cpp ../../../Source/LinuxSpecific/*.cpp
HEADERS = ../../../Source/*.h ../../../Source/LinuxSpecific/*.h
FORMS = ../../../Source/UI/*.ui
CONFIG+=link_pkgconfig
PKGCONFIG+=opencv
QMAKE_CXXFLAGS += -std=c++0x
I intstalled opencv from the package here: https://launchpad.net/~gijzelaar/+archive/opencv2.3
I've verified OpenCV 2.3 is installed on my machine by running the following:
tim#tim-Desktop:~$ pkg-config --cflags --libs opencv
-I/usr/include/opencv -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann
tim#tim-Desktop:~$
The error: /usr/bin/ld: cannot find -lopencv_contrib
G++ version: gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)