1. OpenCV installation:
https://opencv.org/get-started/
sudo apt install libopencv-dev
(or install from source:)
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.x/modules -DCMAKE_BUILD_TYPE=Release -DENABLE_CXX11=ON -DWITH_GTK=ON -DWITH_GTK_2_X=ON -DOPENCV_ENABLE_NONFREE=ON -DBUILD_opencv_python2=OFF -DBUILD_opencv_python3=OFF ../opencv-4.x
make -j4
sudo make install
sudo ldconfig
2. VSCode Configuration:
- ctrl+shift+p --> INPUT "c/c++: edit configurations (JSON)" --> Click --> c_cpp_properties.json is generated (in .vscode directory).
- ctrl+shift+p --> INPUT "tasks: configure default build task" --> Click --> Click "C/C++: g++-7 build active file"(g++, gcc also) --> tasks.json is generated (in .vscode directory).
- In tasks.json, add the last two lines below:
"args": [ // "-std=c++17", "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}", "-I", "${workspaceFolder}/", "-I", "/usr/local/include/opencv4/", "-L", "/usr/local/lib/libopencv_*", // "-lstdc++fs" ],
- In c_cpp_properties.json, add the last one line below:
"includePath": [ "${workspaceFolder}/**", "/usr/local/include/opencv4" ],
3. Code Runner Configuration:
- Extensions: install code runner
- Settings --> INPUT: "code runner: executor map" --> Edit in settings.json
- Modify this line:
"code-runner.executorMap": { "cpp": "cd $dir && g++-7 -g $fileName -o $fileNameWithoutExt -I /usr/local/include/opencv4/ -L /usr/local/lib/libopencv_* && $dir$fileNameWithoutExt",
Insert "-I /usr/local/include/opencv4/ -L /usr/local/lib/libopencv_*" before "&&" (the insertion position is significant);
If cppStandard is c++17, insert "-std=c++17" "-lstdc++fs" as above in 2.3
Check
Now we can Run Code, or Debug/Run C/C++ File successfully.