You haven't specified:
- the include path (header search path) using
-I"/path/to/your/include"
- the path to the libraries using -L"/path/to/libraries"
- which libraries to link against, in this case core and highgui: -lopencv_core -lopencv_highgui
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…