OpenCV application (not library) linking errors

15,262

Solution 1

gcc `pkg-config --cflags --libs opencv` -o ipcamcap ipcamcap.c

The order of flags sometimes matter. Try:

gcc -o ipcamcap ipcamcap.c `pkg-config --cflags --libs opencv` 

You can get more information here.

Solution 2

Be sure you are correctly including and linking all library files.

I specified library's search path with the following:

-L/usr/local/lib
-lopencv_core -lopencv_highgui

and obviously included libraries for the compiler:

-I/usr/local/include/opencv

and compiled correctly your code snippet.

Share:
15,262
bdutta74
Author by

bdutta74

Updated on June 05, 2022

Comments

  • bdutta74
    bdutta74 almost 2 years

    After significant bit of searching around, I am still feeling quite clueless as to what is going on.

    I have written a OpenCV simple application (more-or-less based on a tutorial), but getting it build has been a challenge. Here's the code:-

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <cv.h>
    #include <highgui.h>
    
    
    int main()
    {
        CvCapture *cam;
    
        cam = cvCaptureFromFile("http://[email protected]:2002/videostream.cgi");
        if (cam == NULL) {
            printf("camera is null\n");
        }
        else {
            printf("camera is non-null\n");
        }
    
        cvNamedWindow("Mycam", 0);
    
        while (cvWaitKey(10) != atoi("q")) {
            double t1 = (double) cvGetTickCount();
            IplImage *img = cvQueryFrame(cam);
            double t2 = (double) cvGetTickCount();
            printf("time: %gms  fps: %.2g\n", (t2-t1)/(cvGetTickFrequency() * 1000.), 1000./((t2-t1)/(cvGetTickFrequency() * 1000.)));
            cvShowImage("Mycam", img);
        }
    
        cvReleaseCapture(&cam);
    }
    

    And here's what my Makefile looks like:

    ipcamcap: ipcamcap.c
        g++ `pkg-config --cflags --libs opencv` -o ipcamcap ipcamcap.c
    

    Finally, here are the errors I get --

    /home/icarus/Projects/ipcamcap$ make
    g++ `pkg-config --cflags --libs opencv` -o ipcamcap ipcamcap.c
    /tmp/ccV5YLqQ.o: In function `main':
    ipcamcap.c:(.text+0xf): undefined reference to `cvCreateFileCapture'
    ipcamcap.c:(.text+0x41): undefined reference to `cvNamedWindow'
    ipcamcap.c:(.text+0x4b): undefined reference to `cvGetTickCount'
    ipcamcap.c:(.text+0x61): undefined reference to `cvQueryFrame'
    ipcamcap.c:(.text+0x6a): undefined reference to `cvGetTickCount'
    ipcamcap.c:(.text+0x8c): undefined reference to `cvGetTickFrequency'
    ipcamcap.c:(.text+0xd6): undefined reference to `cvGetTickFrequency'
    ipcamcap.c:(.text+0x118): undefined reference to `cvShowImage'
    ipcamcap.c:(.text+0x122): undefined reference to `cvWaitKey'
    ipcamcap.c:(.text+0x147): undefined reference to `cvReleaseCapture'
    collect2: ld returned 1 exit status
    make: *** [ipcamcap] Error 1
    /home/icarus/Projects/ipcamcap$ 
    

    On the host, I can see that --

    /home/icarus/Projects/ipcamcap$ echo `pkg-config --cflags --libs opencv`
    

    -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 /home/icarus/Projects/ipcamcap$

    And, I do have the libs on this host as well --

    /home/icarus/Projects/ipcamcap$ ls -l /usr/local/lib/libopencv_core.so*
    lrwxrwxrwx 1 root root      21 2012-02-28 11:44 /usr/local/lib/libopencv_core.so -> libopencv_core.so.2.3
    lrwxrwxrwx 1 root root      23 2012-02-28 11:44 /usr/local/lib/libopencv_core.so.2.3 -> libopencv_core.so.2.3.1
    -rw-r--r-- 1 root root 2169691 2012-02-28 11:16 /usr/local/lib/libopencv_core.so.2.3.1
    

    of course, apart from all the other OpenCV libraries.

    Edited:

    Note that I started with compiling the program with "gcc" compiler, but the errors were lot more, like -

    gcc `pkg-config --cflags --libs opencv` -o ipcamcap ipcamcap.c
    /tmp/cc9zt8fA.o: In function `cvDecRefData':
    ipcamcap.c:(.text+0x9f2): undefined reference to `cvFree_'
    ipcamcap.c:(.text+0xa7a): undefined reference to `cvFree_'
    /tmp/cc9zt8fA.o: In function `cvGetRow':
    ipcamcap.c:(.text+0xb91): undefined reference to `cvGetRows'
    /tmp/cc9zt8fA.o: In function `cvGetCol':
    ipcamcap.c:(.text+0xbbf): undefined reference to `cvGetCols'
    /tmp/cc9zt8fA.o: In function `cvReleaseMatND':
    ipcamcap.c:(.text+0xbd9): undefined reference to `cvReleaseMat'
    /tmp/cc9zt8fA.o: In function `cvSubS':
    ipcamcap.c:(.text+0xd35): undefined reference to `cvAddS'
    /tmp/cc9zt8fA.o: In function `cvCloneSeq':
    ipcamcap.c:(.text+0xd6e): undefined reference to `cvSeqSlice'
    /tmp/cc9zt8fA.o: In function `cvSetNew':
    ipcamcap.c:(.text+0xddb): undefined reference to `cvSetAdd'
    /tmp/cc9zt8fA.o: In function `cvGetSetElem':
    ipcamcap.c:(.text+0xe84): undefined reference to `cvGetSeqElem'
    /tmp/cc9zt8fA.o: In function `cvEllipseBox':
    ipcamcap.c:(.text+0xf69): undefined reference to `cvEllipse'
    /tmp/cc9zt8fA.o: In function `cvFont':
    ipcamcap.c:(.text+0xfa6): undefined reference to `cvInitFont'
    /tmp/cc9zt8fA.o: In function `cvReadIntByName':
    ipcamcap.c:(.text+0x1094): undefined reference to `cvGetFileNodeByName'
    /tmp/cc9zt8fA.o: In function `cvReadRealByName':
    ipcamcap.c:(.text+0x1147): undefined reference to `cvGetFileNodeByName'
    /tmp/cc9zt8fA.o: In function `cvReadStringByName':
    ipcamcap.c:(.text+0x11bd): undefined reference to `cvGetFileNodeByName'
    /tmp/cc9zt8fA.o: In function `cvReadByName':
    ipcamcap.c:(.text+0x11fd): undefined reference to `cvGetFileNodeByName'
    ipcamcap.c:(.text+0x1213): undefined reference to `cvRead'
    /tmp/cc9zt8fA.o: In function `cvCreateSubdivDelaunay2D':
    ipcamcap.c:(.text+0x124f): undefined reference to `cvCreateSubdiv2D'
    ipcamcap.c:(.text+0x126a): undefined reference to `cvInitSubdivDelaunay2D'
    /tmp/cc9zt8fA.o: In function `cvContourPerimeter':
    ipcamcap.c:(.text+0x1442): undefined reference to `cvArcLength'
    /tmp/cc9zt8fA.o: In function `cvCalcHist':
    ipcamcap.c:(.text+0x1472): undefined reference to `cvCalcArrHist'
    /tmp/cc9zt8fA.o: In function `main':
    ipcamcap.c:(.text+0x15bb): undefined reference to `cvCreateFileCapture'
    ipcamcap.c:(.text+0x15ed): undefined reference to `cvNamedWindow'
    ipcamcap.c:(.text+0x15f7): undefined reference to `cvGetTickCount'
    ipcamcap.c:(.text+0x160d): undefined reference to `cvQueryFrame'
    ipcamcap.c:(.text+0x1616): undefined reference to `cvGetTickCount'
    ipcamcap.c:(.text+0x1638): undefined reference to `cvGetTickFrequency'
    ipcamcap.c:(.text+0x167e): undefined reference to `cvGetTickFrequency'
    ipcamcap.c:(.text+0x16bf): undefined reference to `cvShowImage'
    ipcamcap.c:(.text+0x16c9): undefined reference to `cvWaitKey'
    ipcamcap.c:(.text+0x16e9): undefined reference to `cvReleaseCapture'
    collect2: ld returned 1 exit status
    make: *** [ipcamcap] Error 1