OpenCV program won't compile (Quantal)

5,796

include all of your libraries in the last.

e.g.

g++ -I/usr/include/opencv main.cpp -o main -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann  

It resolves symbols as it find libraries in the same sequence as it is written on commandline.

Share:
5,796

Related videos on Youtube

Paris
Author by

Paris

I am a co-founder at SourceLair, an online IDE for developers that build Python and JavaScript web applications.

Updated on September 18, 2022

Comments

  • Paris
    Paris over 1 year

    I am trying to compile a simple program using OpenCV in Ubuntu Quantal. I have installed all the OpenCV packages available. My code is the following

    #include <stdio.h>
    #include <highgui.h>
    #include <cv.h>
    
    int main(int argc, char *argv[]) {
            IplImage* img=0; /* pointer to an image */
            printf("Hello\n");
            if(argv[1] != 0)
                    img = cvLoadImage(argv[1], 0); // 1 for color
            else
                    printf("Enter filename\n");
            if(img != 0) {
                    cvNamedWindow("Display", CV_WINDOW_AUTOSIZE); // create a window
                    cvShowImage("Display", img); // show image in window
                    cvWaitKey(0); // wait until user hits a key
                    cvDestroyWindow("Display");
            }
            else
                    printf("File not found\n");
            return 0;
    }
    

    And my compile command is g++ -I/usr/include/opencv -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann main.cpp -o main

    And I get the following errors

    /tmp/ccUQ4Tm4.o: In function `main':
    main.cpp:(.text+0x45): undefined reference to `cvLoadImage'
    main.cpp:(.text+0x6b): undefined reference to `cvNamedWindow'
    main.cpp:(.text+0x7c): undefined reference to `cvShowImage'
    main.cpp:(.text+0x86): undefined reference to `cvWaitKey'
    main.cpp:(.text+0x90): undefined reference to `cvDestroyWindow'
    collect2: ld returned 1 exit status
    make: *** [all] Error 1
    

    What I understand from the errors above is that there is no problem finding the library but the compiler cannot find the functions inside it. Can you please help me out with that?