Issues compiling C (Not C++) OpenCV code

2019-09-19 23:19发布

问题:

I recently installed OpenCV from (http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip/download). Given what I'm trying to do at this point, I don't think it would make a difference using the Ubuntu (12.0.4) repository. That is its very basic functionality.

I'm able to run the C++ code located at http://docs.opencv.org/trunk/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html with no problems.

However my problems begin when I'm trying to use the pure C aspects of OpenCV.

I'm trying to compile the following C code:

#include <stdio.h>
#include <cv.h>
#include <highgui.h>

int main(int argc, char** argv){

    if ( argc != 2 ){
        printf("usage: DisplayImage.out <Image_Path>\n");
        return -1;
    }

    IplImage* img = cvLoadImage( argv[1], CV_LOAD_IMAGE_COLOR );
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage("Example1", img);
    cvWaitKey(0);
    cvReleaseImage( &img );
    cvDestroyWindow( "Example1" );
    return 0;
}

using the following command line

gcc DisplayImage.c -o DisplayImage.out -I /usr/local/include/opencv -L /usr/local/lib -lm -lcv -lhighgui -lcvaux

But am getting the following errors

/usr/bin/ld: cannot find -lcv
/usr/bin/ld: cannot find -lhighgui
/usr/bin/ld: cannot find -lcvaux
collect2: error: ld returned 1 exit status

After taking a look at a similar post (see https://stackoverflow.com/questions/18029712/cannot-find-opencv-libraries-in-ubuntu-12-04), I ran the command

$ pkg-config --cflags opencv

and got

-I/usr/local/include/opencv
-I/usr/local/include  

I also ran

$ pkg-config --libs opencv

and got

/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_ocl.so 
/usr/local/lib/libopencv_photo.so 
/usr/local/lib/libopencv_stitching.so 
/usr/local/lib/libopencv_superres.so 
/usr/local/lib/libopencv_ts.a 
/usr/local/lib/libopencv_video.so 
/usr/local/lib/libopencv_videostab.so 
-lrt 
-lpthread 
-lm
-ldl 

Somehow this last part looks alot different than I would expect (different from the output in the previous post). I could be wrong, so please be patient with me as I'm still a Linux novice.

Would using the downloaded ZIP instead of the repository packages cause this, or is there something else I'm not understanding.

回答1:

Compile using pkg-config (which is a helper tool to build applications) on the command line:

 gcc -Wall -g $(pkg-config  --cflags opencv) DisplayImage.c \
     $(pkg-config --libs opencv) -o DisplayImage

The $( ... ) notation is like backquotes for shell. It inserts the output of some command (here pkg-config ones).

Better yet, write your own Makefile inspired by this example and compile with just the make command.

Order of arguments to gcc matters a big lot!