OpenCV - C program not compiling

,

Hi, I'm having trouble compiling a C code using OpenCV library, this is my main.c file.

#include <opencv/cv.h>

int main()
{
    CvCapture* webcam = cvCreateCameraCapture(0);

    return 0;
}

This is the error I get when I compile the program with gcc

$ gcc main.c 
main.c: In function ‘main’:
main.c:5:5: error: unknown type name ‘CvCapture’; did you mean ‘CvCmpFunc’?
     CvCapture* webcam = cvCreateCameraCapture(0);
     ^~~~~~~~~
     CvCmpFunc
main.c:5:25: warning: implicit declaration of function ‘cvCreateCameraCapture’; did you mean ‘cvCreateGraph’? [-Wimplicit-function-declaration]
     CvCapture* webcam = cvCreateCameraCapture(0);
                         ^~~~~~~~~~~~~~~~~~~~~
                         cvCreateGraph
main.c:5:25: warning: initialization of ‘int *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]

It doesn't recognize the typedef struct CvCapture even though I included opencv/cv.h.

Here's the documentation I referenced for cvCreateCameraCapture(): https://docs.opencv.org/3.4/dd/d01/group__videoio__c.html

I installed the OpenCV library using Pamac Manager, so the header files are in /usr/include/opencv/ and shared object files are in /usr/lib/.

Any help is appreciated!

CvCapture *webcam = cvCreateCameraCapture(0);

I'm still getting the same error. Both ways of declaring pointer variables should be the same.

hmm, check for the include file if you can find the structure definition and the function prototype. This is odd. I am installing the package from the repos now. There is also a package with samples "opencv-samples". Unfortunately my wifi is slow now (used up my highspeed with phone).
Edit: Better yet, grep for the definitions in the opencv include directory with *.h

Ok, I ran grep -rnw '/usr/include/opencv2/' -e 'CvCapture' to search the folder for the definition and found the structure definition in /usr/include/opencv2/videoio/videoio_c.h. It compiles now but I get a bunch more undefined references

$ gcc main.c 
/usr/bin/ld: /tmp/cc39Gglx.o: in function `cvDecRefData':
main.c:(.text+0xa24): undefined reference to `cvFree_'
/usr/bin/ld: main.c:(.text+0xaad): undefined reference to `cvFree_'
/usr/bin/ld: /tmp/cc39Gglx.o: in function `cvGetRow':
main.c:(.text+0xbc5): undefined reference to `cvGetRows'
/usr/bin/ld: /tmp/cc39Gglx.o: in function `cvGetCol':
main.c:(.text+0xbf3): undefined reference to `cvGetCols'
/usr/bin/ld: /tmp/cc39Gglx.o: in function `cvReleaseMatND':
main.c:(.text+0xc0d): undefined reference to `cvReleaseMat'
/usr/bin/ld: /tmp/cc39Gglx.o: in function `cvSubS':
main.c:(.text+0xd6a): undefined reference to `cvAddS'
/usr/bin/ld: /tmp/cc39Gglx.o: in function `cvCloneSeq':
main.c:(.text+0xdbc): undefined reference to `cvSeqSlice'
/usr/bin/ld: /tmp/cc39Gglx.o: in function `cvSetNew':
main.c:(.text+0xe38): undefined reference to `cvSetAdd'
/usr/bin/ld: /tmp/cc39Gglx.o: in function `cvGetSetElem':
main.c:(.text+0xefb): undefined reference to `cvGetSeqElem'
/usr/bin/ld: /tmp/cc39Gglx.o: in function `cvReadIntByName':
main.c:(.text+0xfa2): undefined reference to `cvGetFileNodeByName'
/usr/bin/ld: /tmp/cc39Gglx.o: in function `cvReadRealByName':
main.c:(.text+0x103d): undefined reference to `cvGetFileNodeByName'
/usr/bin/ld: /tmp/cc39Gglx.o: in function `cvReadStringByName':
main.c:(.text+0x10c1): undefined reference to `cvGetFileNodeByName'
/usr/bin/ld: /tmp/cc39Gglx.o: in function `cvReadByName':
main.c:(.text+0x1104): undefined reference to `cvGetFileNodeByName'
/usr/bin/ld: main.c:(.text+0x111a): undefined reference to `cvRead'
/usr/bin/ld: /tmp/cc39Gglx.o: in function `main':
main.c:(.text+0x1178): undefined reference to `cvCreateCameraCapture'
collect2: error: ld returned 1 exit status

Well, I don't even get this far. According to the cv.h file you should include this:

//CV_WARNING("This is a deprecated opencv header provided for compatibility. Please include a header from a corresponding opencv module")

#include "opencv2/core/core_c.h"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/photo/photo_c.h"
#include "opencv2/video/tracking_c.h"
#include "opencv2/objdetect/objdetect_c.h"

replace the quotes with < and >

this should be your programm:

#include <opencv2/core/core_c.h>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/photo/photo_c.h>
#include <opencv2/video/tracking_c.h>
#include <opencv2/objdetect/objdetect_c.h>
#include <opencv2/videoio/videoio_c.h>

int main()
{
    CvCapture *webcam = cvCreateCameraCapture(0);

    return 0;
}

The error you get are linker errors. You need to bind the correct Library like such with the -L option of gcc. I do not know which of this libs its.

This is how far i came:

$ gcc -lopencv_videoio opencv.c 
/usr/bin/ld: /tmp/ccHPAbQm.o: undefined reference to symbol 'cvArcLength'
/usr/bin/ld: /usr/lib/libopencv_imgproc.so.3.4: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

I got it to compile with no errors, I referenced this site http://answers.opencv.org/question/67705/undefined-reference-error-while-compiling-a-c-program-on-raspberry-pi/.

The command I used to compile it:

gcc main.c -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_videoio

Just wanted to tell you. This is my command:

$ gcc -l opencv_videoio -l opencv_imgproc -l opencv_core opencv.c

Oh that works too. Yours has less library bindings. :slight_smile:

Thanks for the help Calvous. This is my first post on the forums. I appreciate that you took your time on this issue!

Just a little tip: This libraries are stripped an therefore cannot be scanned with the nm command:

$ nm /usr/lib/libopencv_videoio.so
nm: /usr/lib/libopencv_videoio.so: no symbols

In this case I am using brute force:

$ grep CvCapture /usr/lib/libopencv_*.so
Binary file /usr/lib/libopencv_videoio.so matches

This way you can find undefined references in shared librarys. Not nice but saves some time to search for it in documentations :wink:

1 Like

Forum kindly sponsored by