I am new to C++ and VS Code. I am trying to use OpenCV in VS Code, and I’m running on Ubuntu.
I followed This tutorial to install and use OpenCV in Ubuntu, and it works. The problem is, when I try to use OpenCV in other projects in VS code, I have the following error when compiling :
cannot open source file "opencv2/opencv.hpp"
Now I suspect that in the tutorial it work because of the command line
g++ test.cpp -o testoutput -std=c++11 `pkg-config --cflags --libs opencv`
Since if I open the tutorial project in VS code and try to compile it normally, I have the same error as my own project.
For information here is my c_cpp_properties.json
for my own project (the tutorial project does not have one):
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/include/opencv4/opencv2/"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
And here is my code for my project :
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(){
cout << "It's ok" << endl;
}
Is it possible to be able to use the ‘compile and run’ functionnality of VS code, and having my project running wit hopenCV ?
When I use the said command line
g++ test.cpp -o testoutput -std=c++11 `pkg-config --cflags --libs opencv`
on my own project, it works, but I would like for it to work by simple compilation
2
Answers
As said HolyBlackCat, "The command used in the tutorial is the correct way to compile code that uses OpenCV. You should figure out how to make VSC run the same command."
I guess I just wanted something simpler, so I'll look into it.
Try putting
"/usr/local/include/opencv4/"
in your c_cpp_properties.json instead of"/usr/local/include/opencv4/opencv2/"
. When you want to#include <foo/bar/baz>
, you need to specify the directory containingfoo/
instead of the path tofoo/
.