0

I am trying to compile this C file to test if OpenCV will work correctly so I can do the OpenCV lab in Head First C book

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

int main(int argc, char const *argv[])
{
    cvNamedWindow("detector", CV_WINDOW_AUTOSIZE);

    CvCapture* capture = cvCreateCameraCapture(0);

    IplImage* frame;

    while(1)
    {
        frame = cvQueryFrame(capture);

        if (frame == NULL)
        {
            printf("error: frame is null\n\n");
            break;
        }

        cvShowImage("detector", frame);

        char c = cvWaitKey(50);

        if (c == 27)
            break;
    }

    cvReleaseCapture(&capture);

    cvDestroyWindow("detector");

    return 0;
}

When typing the command and adding the library

gcc libcv200.dll.a -I C:\OpenCV2.0\include\opencv\ .\intruder_detector.o -o .\intruder_detector

libcv200.dll.a and intruder_detector.o is in the same directory

I get this output

.\intruder_detector.o:intruder_detector.c:(.text+0xa97): undefined reference to `cvFree_'
.\intruder_detector.o:intruder_detector.c:(.text+0xb20): undefined reference to `cvFree_'
.\intruder_detector.o:intruder_detector.c:(.text+0xc4d): undefined reference to `cvGetRows'
.\intruder_detector.o:intruder_detector.c:(.text+0xc86): undefined reference to `cvGetCols'
.\intruder_detector.o:intruder_detector.c:(.text+0xca1): undefined reference to `cvReleaseMat'
.\intruder_detector.o:intruder_detector.c:(.text+0xe1f): undefined reference to `cvAddS'
.\intruder_detector.o:intruder_detector.c:(.text+0xe5f): undefined reference to `cvSeqSlice'
.\intruder_detector.o:intruder_detector.c:(.text+0xed0): undefined reference to `cvSetAdd'
.\intruder_detector.o:intruder_detector.c:(.text+0xf7e): undefined reference to `cvGetSeqElem'
.\intruder_detector.o:intruder_detector.c:(.text+0x107d): undefined reference to `cvEllipse'
.\intruder_detector.o:intruder_detector.c:(.text+0x10de): undefined reference to `cvInitFont'
.\intruder_detector.o:intruder_detector.c:(.text+0x11b1): undefined reference to `cvGetFileNodeByName'
.\intruder_detector.o:intruder_detector.c:(.text+0x1257): undefined reference to `cvGetFileNodeByName'
.\intruder_detector.o:intruder_detector.c:(.text+0x12df): undefined reference to `cvGetFileNodeByName'
.\intruder_detector.o:intruder_detector.c:(.text+0x1323): undefined reference to `cvGetFileNodeByName'
.\intruder_detector.o:intruder_detector.c:(.text+0x1336): undefined reference to `cvRead'
.\intruder_detector.o:intruder_detector.c:(.text+0x145b): undefined reference to `cvCreateSubdiv2D'
.\intruder_detector.o:intruder_detector.c:(.text+0x147f): undefined reference to `cvInitSubdivDelaunay2D'
.\intruder_detector.o:intruder_detector.c:(.text+0x1654): undefined reference to `cvCalcArrHist'
.\intruder_detector.o:intruder_detector.c:(.text+0x17d1): undefined reference to `cvAvg'
.\intruder_detector.o:intruder_detector.c:(.text+0x1802): undefined reference to `cvSum'
.\intruder_detector.o:intruder_detector.c:(.text+0x184d): undefined reference to `cvAvgSdv'
.\intruder_detector.o:intruder_detector.c:(.text+0x18ac): undefined reference to `cvReshape'
.\intruder_detector.o:intruder_detector.c:(.text+0x18c8): undefined reference to `cvReshape'
.\intruder_detector.o:intruder_detector.c:(.text+0x18dc): undefined reference to `cvPerspectiveTransform'
.\intruder_detector.o:intruder_detector.c:(.text+0x18fd): undefined reference to `cvGetElemType'
.\intruder_detector.o:intruder_detector.c:(.text+0x1917): undefined reference to `cvColorToScalar'
.\intruder_detector.o:intruder_detector.c:(.text+0x194d): undefined reference to `cvSet'
.\intruder_detector.o:intruder_detector.c:(.text+0x199c): undefined reference to `cvError'
.\intruder_detector.o:intruder_detector.c:(.text+0x19d3): undefined reference to `cvError'
.\intruder_detector.o:intruder_detector.c:(.text+0x1aea): undefined reference to `cvError'
.\intruder_detector.o:intruder_detector.c:(.text+0x1b1f): undefined reference to `cvError'
.\intruder_detector.o:intruder_detector.c:(.text+0x1bac): undefined reference to `cvError'
.\intruder_detector.o:intruder_detector.c:(.text+0x1c1e): undefined reference to `cvRandArr'
.\intruder_detector.o:intruder_detector.c:(.text+0x1d96): undefined reference to `cvCartToPolar'
.\intruder_detector.o:intruder_detector.c:(.text+0x1e6f): undefined reference to `cvCartToPolar'
.\intruder_detector.o:intruder_detector.c:(.text+0x1f02): undefined reference to `cvPow'
.\intruder_detector.o:intruder_detector.c:(.text+0x1f95): undefined reference to `cvPow'
.\intruder_detector.o:intruder_detector.c:(.text+0x2028): undefined reference to `cvPow'
.\intruder_detector.o:intruder_detector.c:(.text+0x20a5): undefined reference to `cvExp'
.\intruder_detector.o:intruder_detector.c:(.text+0x2122): undefined reference to `cvLog'
.\intruder_detector.o:intruder_detector.c:(.text+0x2156): undefined reference to `cvBoundingRect'
.\intruder_detector.o:intruder_detector.c:(.text+0x2186): undefined reference to `cvInvert'
.\intruder_detector.o:intruder_detector.c:(.text+0x2218): undefined reference to `cvConvexHull2'
.\intruder_detector.o:intruder_detector.c:(.text+0x2281): undefined reference to `cvMinAreaRect2'
.\intruder_detector.o:intruder_detector.c:(.text+0x22b0): undefined reference to `cvBoxPoints'
.\intruder_detector.o:intruder_detector.c:(.text+0x23ca): undefined reference to `cvFitLine'
.\intruder_detector.o:intruder_detector.c:(.text+0x247c): undefined reference to `cvFitLine'
.\intruder_detector.o:intruder_detector.c:(.text+0x24d9): undefined reference to `cvFitEllipse2'
.\intruder_detector.o:intruder_detector.c:(.text+0x2634): undefined reference to `cvTransform'
.\intruder_detector.o:intruder_detector.c:(.text+0x26c3): undefined reference to `cvHoughLines2'
.\intruder_detector.o:intruder_detector.c:(.text+0x2753): undefined reference to `cvHoughLines2'
.\intruder_detector.o:intruder_detector.c:(.text+0x27e3): undefined reference to `cvHoughLines2'
.\intruder_detector.o:intruder_detector.c:(.text+0x2846): undefined reference to `cvCreateMat'
.\intruder_detector.o:intruder_detector.c:(.text+0x285f): undefined reference to `cvCreateMat'
.\intruder_detector.o:intruder_detector.c:(.text+0x29e7): undefined reference to `cvFindFundamentalMat'
.\intruder_detector.o:intruder_detector.c:(.text+0x29f3): undefined reference to `cvReleaseMat'
.\intruder_detector.o:intruder_detector.c:(.text+0x29ff): undefined reference to `cvReleaseMat'
.\intruder_detector.o:intruder_detector.c:(.text+0x2a44): undefined reference to `cvFindChessboardCorners'
.\intruder_detector.o:intruder_detector.c:(.text+0x2cad): undefined reference to `cvCalibrateCamera2'
.\intruder_detector.o:intruder_detector.c:(.text+0x2f1a): undefined reference to `cvCalibrateCamera2'
.\intruder_detector.o:intruder_detector.c:(.text+0x30f7): undefined reference to `cvFindExtrinsicCameraParams2'
.\intruder_detector.o:intruder_detector.c:(.text+0x32f8): undefined reference to `cvFindExtrinsicCameraParams2'
.\intruder_detector.o:intruder_detector.c:(.text+0x3338): undefined reference to `cvRodrigues2'
.\intruder_detector.o:intruder_detector.c:(.text+0x3351): undefined reference to `cvRodrigues2'
.\intruder_detector.o:intruder_detector.c:(.text+0x3691): undefined reference to `cvProjectPoints2'
.\intruder_detector.o:intruder_detector.c:(.text+0x382c): undefined reference to `cvProjectPoints2'
.\intruder_detector.o:intruder_detector.c:(.text+0x38bb): undefined reference to `cvUndistort2'
.\intruder_detector.o:intruder_detector.c:(.text+0x38fa): undefined reference to `cvGetRawData'
.\intruder_detector.o:intruder_detector.c:(.text+0x3a2d): undefined reference to `cvGetRawData'
.\intruder_detector.o:intruder_detector.c:(.text+0x3b56): undefined reference to `cvCalcEMD2'
.\intruder_detector.o:intruder_detector.c:(.text+0x3ba5): undefined reference to `cvCreateMat'
.\intruder_detector.o:intruder_detector.c:(.text+0x3c7e): undefined reference to `cvKMeans2'
.\intruder_detector.o:intruder_detector.c:(.text+0x3c8a): undefined reference to `cvReleaseMat'
.\intruder_detector.o:intruder_detector.c:(.text+0x3cdb): undefined reference to `cvError'
.\intruder_detector.o:intruder_detector.c:(.text+0x3cf1): undefined reference to `cvCreateGraphScanner'
.\intruder_detector.o:intruder_detector.c:(.text+0x3d37): undefined reference to `cvFree_'
.\intruder_detector.o:intruder_detector.c:(.text+0x3d80): undefined reference to `cvError'
.\intruder_detector.o:intruder_detector.c:(.text+0x3d97): undefined reference to `cvAlloc'
.\intruder_detector.o:intruder_detector.c:(.text+0x3ddd): undefined reference to `cvReleaseGraphScanner'
.\intruder_detector.o:intruder_detector.c:(.text+0x3e1a): undefined reference to `cvGetElemType'
.\intruder_detector.o:intruder_detector.c:(.text+0x3e34): undefined reference to `cvColorToScalar'
.\intruder_detector.o:intruder_detector.c:(.text+0x3e86): undefined reference to `cvLine'
.\intruder_detector.o:intruder_detector.c:(.text+0x3eaf): undefined reference to `cvGetElemType'
.\intruder_detector.o:intruder_detector.c:(.text+0x3ec9): undefined reference to `cvColorToScalar'
.\intruder_detector.o:intruder_detector.c:(.text+0x3f1b): undefined reference to `cvCircle'
.\intruder_detector.o:intruder_detector.c:(.text+0x3f47): undefined reference to `cvGetElemType'
.\intruder_detector.o:intruder_detector.c:(.text+0x3f61): undefined reference to `cvColorToScalar'
.\intruder_detector.o:intruder_detector.c:(.text+0x3fd4): undefined reference to `cvEllipse'
.\intruder_detector.o:intruder_detector.c:(.text+0x4002): undefined reference to `cvGetElemType'
.\intruder_detector.o:intruder_detector.c:(.text+0x401c): undefined reference to `cvColorToScalar'
.\intruder_detector.o:intruder_detector.c:(.text+0x407e): undefined reference to `cvPolyLine'
.\intruder_detector.o:intruder_detector.c:(.text+0x430d): undefined reference to `cvNamedWindow'
.\intruder_detector.o:intruder_detector.c:(.text+0x4317): undefined reference to `cvCreateCameraCapture'
.\intruder_detector.o:intruder_detector.c:(.text+0x4327): undefined reference to `cvQueryFrame'
.\intruder_detector.o:intruder_detector.c:(.text+0x4353): undefined reference to `cvShowImage'
.\intruder_detector.o:intruder_detector.c:(.text+0x435d): undefined reference to `cvWaitKey'
.\intruder_detector.o:intruder_detector.c:(.text+0x4375): undefined reference to `cvReleaseCapture'
.\intruder_detector.o:intruder_detector.c:(.text+0x4381): undefined reference to `cvDestroyWindow'
collect2.exe: error: ld returned 1 exit status

So I tried all the other libraries in C:\OpenCV2.0\lib folder which contains the following libraries

-a----         10/1/2009   6:02 AM         435896 libcv200.dll.a
-a----         10/1/2009   6:10 AM         465136 libcvaux200.dll.a
-a----         10/1/2009   5:58 AM         526158 libcxcore200.dll.a
-a----         10/1/2009   6:16 AM         150786 libcxts200.dll.a
-a----         10/1/2009   6:06 AM         138242 libhighgui200.dll.a
-a----         10/1/2009   6:07 AM         375380 libml200.dll.a

I don't know if I'm compiling the code correctly or I need some other libraries. I tried searching if someone got the same issue, but I didn't find any solution. Can anyone help me?

Yousef Ali
  • 25
  • 1
  • 7
  • 2
    Does this answer your question? [Why does the order in which libraries are linked sometimes cause errors in GCC?](https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc) – kaylum Oct 21 '21 at 19:46
  • 1
    In the link command, move the library to be after the `.o` file that uses it. See the duplicate post for more details. – kaylum Oct 21 '21 at 19:46
  • I moved it and didn't work @kaylum – Yousef Ali Oct 21 '21 at 19:49
  • I also found the include flag is not necessary – Yousef Ali Oct 21 '21 at 19:50
  • 2
    youv'e been fooled. opencv 2.0 is DEAD since more that 10 years. (dont waste anyone's time with this) – berak Oct 21 '21 at 20:14
  • Do you mean dead like I can't compile it, or no one is using opencv2.0 anymore. I chose opencv 2.0 because newer versions doesn't provide C libraries anymore. – Yousef Ali Oct 21 '21 at 20:21
  • `gcc -I C:\OpenCV2.0\include\opencv\ .\intruder_detector.o -lcv200.dll.a -o .\intruder_detector` – 0___________ Oct 21 '21 at 20:21
  • Didn't work :( @0___________ – Yousef Ali Oct 21 '21 at 20:28
  • 1
    have you included all the libs in your build line? Even that will require a specific order. I've used CMake with OpenCV in the past, the cmake files should take care of the building minutiae for you. Does it not come with instructions on how to build? – yano Oct 21 '21 at 21:08
  • I searched for cmake files and instructions but unfortunately I found nothing @yano – Yousef Ali Oct 22 '21 at 12:10
  • 1
    hmm, I dunno. In my mind your options now are 1) guess and check at the order of the libs, 2) guide your approach to #1 by disassembling them to see which functions are in which libraries, 3) upgrade to a more modern version of OpenCV and use C++ (you're saying OpenCV dropped support for C after 2.0??). IMO #3 is your best option. – yano Oct 22 '21 at 14:37
  • 1
    Thank you for your help. Your advice is taken into account. @yano – Yousef Ali Oct 22 '21 at 15:58

0 Answers0