Trying to compile C++using Visual Studio Code or g++ in Ubuntu on Jetson Nano

My basic code is:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    // Read the image file
    Mat image = imread("cv.jpg");
    // ... rest of code

The error I get is:

undefined reference to `cv::imread

And a few more like:

undefined reference to `cv::namedWindow

Just trying to do any C++ code with OpenCV and it fails.

welcome.

you probably didn’t tell the compiler/linker where to find the opencv library files, just the headers.

if you get an error, please always use a search engine on it. it would have given you this result:

I think for that routine you have to link to opencv_imgcodecs. Did you add this library as well on the command line? You might also have to link with opencv_highgui, but try imgcodecs first.

I added -lopencv_imgcodecs -lopencv_core -lopencv_features2d -lopencv_highgui to the command line and it worked.
I needed Visual Studio Code to do this automatically so I added the following to tasks.json
“-lopencv_imgcodecs”,
“-lopencv_core”,
“-lopencv_features2d”,
“-lopencv_highgui”