0. Official link YYDS
Let me tell you about my environment in advance, which is ubuntu 20.04, vscode 1.68.1, which is used for the configuration of C and C++ environments. The content of this article is not how to configure files, but the understanding after configuration files.
The following is the official link. Personally, I think the English writing is quite easy to read.
https://code.visualstudio.com/docs/cpp/config-linux
1. Format (json format)
You need to understand the content of the vscode configuration file, and you need to know in advance what the json format is. I saw a link to share below, and I can roughly understand the json format. In my opinion, the json format is very similar to the dictionary in the Python data structure.
- Arrays
[]
are represented by square brackets (" ").- Objects (0bject) are denoted by braces ("
{}
").- Name/value pairs (
name/value
) are combined into arrays and objects.- Names (
name
) are enclosed in double quotes , and values (value
) are strings, numbers, booleans, null, objects, and arrays .- Parallel data are
,
separated by commas (“ ”)
2. Content
file name | effect | configuration method |
---|---|---|
task.json | Tell VS code how to compile .cpp the file. After configuration, the g++ compiler will be called to create an executable file based on the source code . |
Select: Terminal > Configure Default Build Task , select from the drop-down menu: g++.exe build active file , the selection .vscode will create a tasks.json file in the file. |
launch.json | You can use F5 to make VS Code start the GDB debugger to debug the program . | 1. Select Run > Add Configuration… in the main menu , and then select C++ (GDB/LLDB) ; 2. There will be various predefined debugging configuration files in the drop-down menu, select g++.exe build and debug active file . (It is also possible to directly generate a debug file as follows, in short, the content of the configuration file is as follows) |
c_cpp_properties.json | Get more control over C/C++ extensions, change the compiler's path, the C++ standard (defaults to C++17), and more. (compiler path and IntelliSense settings) | Execute the C/C++:Edit Configurations(UI) command in the command panel ( Ctrl+Shift+P ) to view the C/C++ configuration interface C/C++ Configurations . VS Code will write the changed configuration in C/C++ Configurations to the c_cpp_properties.json file under ./vscode . |
settings.json |
The above content is referenced from: VS Code - C++ configuration file
2.0 task.json
If you only need to compile code information, you can configure this file.
source information
{
"tasks": [
{
// The task type to customize
"type": "cppbuild",
// The name of the task.
"label": "C/C++: g++ build active file",
// ***The path to either a compiler or script that performs compilation.
"command": "/usr/bin/g++",
// ***调试命令,即g++ -g ${file} -o ${fileDirname}/${fileBasenameNoExtension}
// -fdiagnostics-color=always 设置彩色错误提示
// -g 添加调试信息
// ${file} - 需要编译的文件,查看下文变量速查表
// -o 后续参数为可执行文件
// ${fileDirname}/${fileBasenameNoExtension} - 输出文件,查看下文变量速查表
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
// *** group 对象的 "isDefault": true 说明这个任务会在按下 Ctrl+Shift+B 时运行。
// 这个属性只是为方便,如果设置为 false 可以通过中断菜单中的 Tasks: Run Build Task 来运行任务。
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Variable Cheat Sheet
variable name | effect |
---|---|
${file} | The name of the currently opened file being edited, including the absolute path, file name, and file extension |
${fileDirname} | The absolute path of the currently opened file, excluding the file name |
${fileBasenameNoExtension} | The file name of the currently opened file, excluding the path and extension |
The above content comes from linking various variables in VS code file, {file},f i l e , {fileBasename}, there are more instructions in the link.