Linux 下VS Code 的使用
这里以创建helloworld 为例。
Step 0:准备工作:
-
Install Visual Studio Code.
-
Install the C++ extension for VS Code. You can install the C/C++ extension by searching for 'c++' in the Extensions view (Ctrl+Shift+X).
Step 1: 创建工作目录 helloworld,并启动vscode
mkdir helloworld
cd helloworld
code .
Step 2: 工作目录helloworld 下会生成一个.vscode
文件夹,其中需要三个关键的配置文件:
tasks.json
(compiler build settings)launch.json
(debugger settings)c_cpp_properties.json
(compiler path and IntelliSense settings)
(1)tasks.json
stores build configurations.
The first time you run your program, the C++ extension creates tasks.json
例子如下
{"version": "2.0.0","tasks": [{"type": "shell","label": "C/C++: 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},"detail": "Task generated by Debugger."}]
}
(2)launch.json 存储
debug 配置信息
To create launch.json
, choose Add Debug Configuration from the play button drop-down menu.
例子如下:
{"version": "0.2.0","configurations": [{"name": "C/C++: g++ build and debug active file","type": "cppdbg","request": "launch","program": "${fileDirname}/${fileBasenameNoExtension}","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"MIMode": "gdb","miDebuggerPath": "/usr/bin/gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "C/C++: g++ build active file"}]
}
From now on, the play button and F5 will read from your launch.json
file when launching your program for debugging.
(3)c_cpp_properties.json C/C++ configurations
If you want more control over the C/C++ extension, you can create a c_cpp_properties.json
file, which will allow you to change settings such as the path to the compiler, include paths, C++ standard (default is C++17), and more.
You can view the C/C++ configuration UI by running the command C/C++: Edit Configurations (UI) from the Command Palette (Ctrl+Shift+P).
You only need to modify the Include path setting if your program includes header files that are not in your workspace or in the standard library path.例子如下:
{"configurations": [{"name": "Linux","includePath": ["${workspaceFolder}/**"],"defines": [],"compilerPath": "/usr/bin/gcc","cStandard": "c11","cppStandard": "c++17","intelliSenseMode": "clang-x64"}],"version": 4
}
VS Code is now configured to use gcc on Linux. The configuration applies to the current workspace. To reuse the configuration, just copy the JSON files to a .vscode
folder in a new project folder (workspace) and change the names of the source file(s) and executable as needed.
Ubuntu 下使用clang 作为vscode 的编译器,可参考如下链接进行设置:
How To Use clang With vscode On Ubuntu 20.04 | Robs Blog (rob-blackbourn.github.io)