ilkinimanov
ilkinimanov

Reputation: 43

How to compile C++ code that contains OpenCV library without Makefile?

I just started learning OpenCV with C++. I built OpenCV from source on Linux machine. I use Visual Studio Code text editor. I want to compile C++ code that contains OpenCV library without Makefile. I added /usr/local/opencv4/** to includePath variable. But it didn't work. I'm receiving opencv2/opencv.hpp error. Is it possible build C++ code that contains OpenCV includes without Makefile? How can I do it?

My Code

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
  
int main(int argc, char** argv)
{
    Mat image = imread("Enter the Address"
                       "of Input Image",
                       IMREAD_GRAYSCALE);
  
    if (image.empty()) {
        cout << "Image File "
             << "Not Found" << endl;
  
        cin.get();
        return -1;
    }
  
    imshow("Window Name", image);
  
    waitKey(0);
    return 0;
}

Upvotes: 0

Views: 1051

Answers (1)

guivi
guivi

Reputation: 402

You will have to tell the compiler the location of the header files and library files. As mentioned by kotatsuyaki you can use pkg-config to do this for your.

Example with gcc and pkg-config:

g++ *.cpp -o program_name `pkg-config --cflags --libs opencv4`

Example with gcc and without pkg-config:

g++ *.cpp -o program_name -I/PATH/TO/OPENCV/INCLUDE/FOLDER -L/PATH/TO/OPENCV/LIB/FOLDER -lopencv_something -lopencv_somethingelse ... etc

In Windows with cl you can do the same as the g++ without pkg-config:

cl main.cpp /I"PATH TO OPENCV ICNLUDE FOLDER" /link /LIBPATH:"PATH TO OPENCV LIB FOLDER" opencv_library1.lib opencv_libarary2.lib ... etc. 

Upvotes: 1

Related Questions