#include "highgui.h"
int main(int argc, char ** argv) {
IplImage *img = cvLoadImage(argv[1]);
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Example1");
}
很简单的一个程序,接下来要做的事情就是编译与链接了。
通过观察OpenCV-2.4.2/samples/c/build_all.sh,可以找到编译链接命令 :
g++ `pkg-config --cflags opencv` -o filename filename.cpp `pkg-config --libs opencv`
这样就可以生成可执行文件。
现在我们可以进一步分析这条命令:
运行pkg-config --cflags opencv,得到结果-I/usr/local/include/opencv -I/usr/local/include
运行pkg-config --libs opencv,得到结果:/usr/local/lib/libopencv_calib3d.so /usr/local/lib/libopencv_contrib.so /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_features2d.so /usr/local/lib/libopencv_flann.so /usr/local/lib/libopencv_gpu.so /usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_imgproc.so /usr/local/lib/libopencv_legacy.so /usr/local/lib/libopencv_ml.so /usr/local/lib/libopencv_nonfree.so /usr/local/lib/libopencv_objdetect.so /usr/local/lib/libopencv_photo.so /usr/local/lib/libopencv_stitching.so /usr/local/lib/libopencv_ts.so /usr/local/lib/libopencv_video.so /usr/local/lib/libopencv_videostab.so
gcc通过-I指定头文件的搜索路径,通过-l+库名指定链接的库,也可以直接将库名加在后面。
这里将所有可能用的的库都加上了,其实本程序仅仅使用了HighGUI和CXCORE两个模块(库)。HighGUI中调用了CXCORE模块(库)。
于是这个程序的编译链接命令可以最终简化为:
g++ -I/usr/local/include/opencv -o showimage showimage.cpp /usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_core.so
或 g++ -I/usr/local/include/opencv -o showimage showimage.cpp -lopencv_highgui -lopencv_core
最后需要说明的是这里库的所在路径已经在/etc/ld.so.conf中指定,为/usr/local/lib。