OpenCV hello world not compiling like it should

10,071

g++ doesn't consider %PATH% ($PATH on Unix) when looking for include files.

Add the following to the compilation command: -IC:\OpenCV-2.3.1\install\include:

g++ -IC:\OpenCV-2.3.1\install\include -o main main.cpp -lopencv_core ...
Share:
10,071
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I am setting up a new machine with OpenCV 2.3.1. The machine is a Windows 7 box, and I followed the installation instructions given by the OpenCV website (used CMake with MinGW to build).

    Here is my code:

    #include <iostream>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    int main() {
        char var;
        cv::Mat img;
        img = cv::imread("C:/test/img.jpg");
        cv::namedWindow("Image");
        cv::imshow("Image", img);
        std::cin >> var;
        return 1;
    }
    

    Here is my make command:

    g++ -o main main.cpp -lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video -lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect -lopencv_contrib -lopencv_legacy
    

    Here is my path:

    C:\OpenCV-2.3.1\install\bin;C:\OpenCV-2.3.1\install\include;C:\QtSDK\QtCreator\bin;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\CMake 2.8\bin;C:\QtSDK\mingw\bin;
    

    Here is my error:

    main.cpp:2:33: error: opencv2/core/core.hpp: No such file or directory
    main.cpp:3:39: error: opencv2/highgui/highgui.hpp: No such file or directory
    main.cpp: In function 'int main()':
    main.cpp:8: error: 'cv' has not been declared
    main.cpp:8: error: expected ';' before 'img'
    main.cpp:9: error: 'img' was not declared in this scope
    main.cpp:9: error: 'cv' has not been declared
    main.cpp:10: error: 'cv' has not been declared
    main.cpp:11: error: 'cv' has not been declared
    

    This is not making sense. Why won't this compile? Why can't it find opencv2/core/core.hpp?