Article Directory
Recently, I am doing C++ deployment related work for deep learning, so I wrote this document to record the process of environment configuration. Environment configuration is a very tedious job, no matter from doing related homework in university or going to work. It takes skill as well as luck to do the job. Of course, when encountering unsolvable metaphysical problems, there is only one way in the end: restart the device.
This article takes the environment as the setting and divides it into two environments for deployment, one is the part of Linux and Win10 deployment. Linux is partially deployed because Linux is involved in both device-side deployment applications and server-side deployment applications. The Windows deployment is recorded for the convenience of my learning exercises on my notebook. So let's start our environment configuration and deployment now.
Whether it is Linux or Win10 environment, first write a hello world code for testing:
#include<iostream>
using namespace std;
int main(){
cout<<"hello world"<<endl;
}
Linux
My Linux version is: Linux ubuntu 5.15.0-56-generic #62-Ubuntu SMP Tue Nov 22 19:54:14 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
. Connect to the server through VSCode on the Win10 device. To configure the C++ environment, you must first install the C++ extension on vscode, as shown in the figure below:
After configuring the environment of vscode, you need to configure and files in the folder of the current project , .vscode
which are used for compiler configuration. For debugger settings, for compiler paths and IntelliSense settings.task.json
launch.json
c_cpp_properties.json
task.json
launch.json
c_cpp_properties.json
compile
Create a new task.json
file, which tells vscode how to compile the program. The general purpose is to use g++
the compiler to compile the source file into an executable file. task
The content is as follows:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
command
The variable specifies which compiler will be used, here /usr/bin
is g++
the compiler under the directory; args
this parameter list gives g++
the command parameters we need to pass to the compiler, which must conform to g++
the order of the parameters of the command line, ${file}
representing the file currently opened by the editor, ${fileDirname}
Represents the directory of the current active file, ${fileBasenameNoExtension}
which is the generated file name, which is the same as the compiled file name. label
The value of the variable is the name of the terminal task, and you can modify it arbitrarily; group
the in the variable "isDefault":true
means that when pressed Ctrl+Shift+B
, the currently active file will be compiled, just for convenience. For more variable definitions, please refer to: Variables Reference
VS Code
Open the source file with tmp.cpp
, and in the editing interface, press Ctrl+Shift+B
the key (or in the main menu->terminal->run build task), tasks.json will be executed.
Run the file, output hello world
.
Be careful: Make tmp.cpp
(that is, the file you want to compile) active.
debugging
The debug file mainly uses launch.json
the file, which configures the GDB debugger under VS Code. GDB debugger installation command: apt-get install build-essential gdb
.
launch.json
content:
{
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
],
"version": "2.0.0"
}
Among them, the variable specifies the file you want to debug, which is set as the active file under program
the active folder . If it is active, it will be debugged. By default, the C++ extension does not add breakpoints to source files, which is set by default , and if set , the debugger will pause at method entry.${fileDirname}
${fileBasenameNoExtension}
tmp.cpp
tmp
stopAtEntry
flase
true
main
Configure OpenCV
If you need more usage of C/C++ extensions, you can create a c_cpp_properties.json
file, which allows you to change some settings, such as compiler path, C++ standard, etc. You can press Ctrl+Shift+P
the key and enter it on the command line, and you can select configuration C/C++
in the drop-down menu , so that files will appear under the folder, and a setting window will appear on the VS Code interface. You can configure according to your needs Some C/C++ configurations are modified in it.C/C++
.vscode
c_cpp_properties.json
To configure opencv, of course OpenCV must be installed first. You can refer to this article: Notes - Linux installation of OpenCV and VSCode configuration compilation and practice to gain true knowledge - Ubuntu 18.04 VSCODE configures OpenCV4.5 to run the YOLO4 model .
Create a new c_cpp_properties.json
file with the following content:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/opencv2"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
The main thing is inclodePath
to include the path of the opencv header file.
Modify task.json
the file, because the dynamic link library of opencv needs to be added:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"`pkg-config", "--cflags", "--libs", "opencv4`",
// "-I", "/usr/local/include",
// "-I", "/usr/local/include/opencv4",
// "-I", "/usr/local/include/opencv4/opencv2",
// "-L", "/usr/local/lib",
// "-l", "opencv_core",
// "-l", "opencv_imgproc",
// "-l", "opencv_imgcodecs",
// "-l", "opencv_video",
// "-l", "opencv_ml",
// "-l", "opencv_highgui",
// "-l", "opencv_objdetect",
// "-l", "opencv_flann",
// "-l", "opencv_imgcodecs",
// "-l", "opencv_photo",
// "-l", "opencv_videoio"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Finally, just run the following program code like the above compilation and debugging:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc,char** argv){
std::cout<<"111"<< std::endl;
cv::Mat src = cv::imread("./src/model_optimize.jpg");
cv::resize(src,src,cv::Size(500,800));
imwrite("./test.jpg", src);
return 0;
}
Win10
To debug C++ programs on Win10, it must be installed MinGW
and configured first. In this step, it will not be expanded here.
Compile and debug
The principle of compiling and debugging is similar to that of Linux. Just configure the path and path inside. The content of the file is as task.json
follows launch.json
:g++
gdb
task.json
:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "C:/mingw64/bin/g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
// "-I", "/usr/local/include",
// "-I", "/usr/local/include/opencv4",
// "-I", "/usr/local/include/opencv4/opencv2",
// "-L", "/usr/local/lib",
// "-l", "opencv_core",
// "-l", "opencv_imgproc",
// "-l", "opencv_imgcodecs",
// "-l", "opencv_video",
// "-l", "opencv_ml",
// "-l", "opencv_highgui",
// "-l", "opencv_objdetect",
// "-l", "opencv_flann",
// "-l", "opencv_imgcodecs",
// "-l", "opencv_photo",
// "-l", "opencv_videoio"
],
"options": {
"cwd": "C:/mingw64/bin"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
launch.json
The file is as follows:
{
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "C:/mingw64/bin/gdb.exe"
}
],
"version": "2.0.0"
}
Configure OpenCV
opencv version: 4.6.10. Configure the working principle of OpenCV: compile the source code of OpenCV, and then configure it on VSCode.
(1) Source code compilation
Compile and install the environment:
- cmake version 3.9.0-rc3
- mingw 5.3.0
Set the source code path and generate the path, click configure
the button, note: select generate MinGW Makefiules
, select Specify native compilers
, and finally configure your compiler path.
Click Generate
the button.
To set the path, here is: C:\opencv\build\x64\mingw
Next execute:minGW32-make -j8
implementminGW32-make install
bin
Finally , the result is generated C:\opencv\build\x64\mingw\bin
under and configured to the environment path.
Problems encountered:
- Compile and generate the error shown in the figure below, because the OpenCV version is too old. Finally updated to 4.6.10.
- 出现“Unexpected GDB output from command “-exec-run”.”
StackOverFlow上的解决方案:ERROR: Unable to start debugging. Unexpected GDB output from command “-exec-run”. Unable to find Mach task port for process-id 1401
reference
- How to configure C/C++ environment on VS Code in Linux system
- VSCode+Opencv(C++)+Win10
- VScode builds the OpenCV environment ⭐️⭐️⭐️, recommended.
- OpenCV uses CMake and MinGW-w64 to compile and install ⭐️⭐️⭐️, recommended
- OpenCV-MinGW-Build : The compiled OpenCV library