For the past week, I have been trying to run some simple OpenCV programs using the terminal. I have tried many tutorials and recommendations from various forums with little
You haven't specified:
-I"/path/to/your/include"
I have opencv headers in /opt/local/include
and libraries in /opt/local/lib
,
so to compile a basic program like this:
#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;
}
}
I compiled like so:
g++ main.cpp -I"/opt/local/include/" -L"/opt/local/lib/" -lopencv_core -lopencv_highgui -o main
Then ran ./main
:
Bare in mind you might have opencv installed in the /usr/local
folder not /opt/local
depending how you compiled/installed OpenCV.
Also, you might have pkg-config installed which can come in handy when you need to link against more libraries.
For example, you can run:
pkg-config --libs --cflags opencv
which in my case outputs:
-I/opt/local/include/opencv -I/opt/local/include -L/opt/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_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab
but in your case, it should output your particular OpenCV paths.
This would simplify compiling to this:
g++ main.cpp `pkg-config --libs --cflags opencv` -o main
The guide you linked to uses cmake
which generates Makefiles for you.
That's another nice options. Also, based on the same guide, you should have XCode installed which you can use to create a Command Line Tool and point the Header Search Paths and Library Search Paths.
I create a similar file that maybe can help you.
First I use:
sudo brew install opencv
Then I install the opencv.3.0
according to the hint given by the terminal. Then in the .cpp
file which needs the API from opencv
, I use:
#include "opencv2/opencv.hpp"
As my only include file about opencv
. Actually, at that project I use highgui
, core
, and imgprog
. But no worries here, I will show you how to solve them in linking part.
After you finish your project, you are going to compile your project on the terminal.
Because I also used the JNI interface, so I still need to link the jni.h.
Here we go:
g++ xxxx.cpp xxx.cpp -lstdc++ -fPIC -shared (to create a shared object)
-I/absolute path/ (we can use -I to be followed with the absolute path of the library you need to use )
-I/Users/yuanzhan/Downloads/OpenCV-2.0.0/src/
-I /Users/yuanzhan/Downloads/OpenCV-2.0.0/include/opencv/ -I/usr/local/Cellar/opencv3/3.1.0_3/lib -lopencv_core (open the library for use if you use the API fro here)-lopencv_highgui -lopencv_imgproc -L.(i put the cv2. on local otherwise you can add the absolute path here) -lcv2(use the package) -v -o libopenCvSDK.so(generate my .so package).