VSCode 使用CMake调试
时间: 2024-06-13 16:06:41 浏览: 299
以下是使用CMake在VSCode中进行调试的步骤:
1. 确保已经安装了CMake和调试器(例如GDB)。
2. 在项目根目录下创建一个名为`CMakeLists.txt`的文件,并编写CMake配置。
3. 在VSCode中打开项目文件夹,并安装CMake Tools和C/C++插件。
4. 在VSCode的设置中,添加CMake的路径,并在CMake Tools的设置中配置CMake的路径和生成目录。
5. 在VSCode的调试选项卡中,创建一个名为`(gdb) Launch`的配置,并设置`program`为可执行文件的路径。
6. 在源代码中设置断点,并使用调试器来跟踪程序的执行。
下面是一个示例`CMakeLists.txt`文件:
```cmake
cmake_minimum_required(VERSION 3.10)
project(MyProject)
set(CMAKE_CXX_STANDARD 11)
add_executable(MyProject main.cpp)
```
下面是一个示例的VSCode配置:
```json
{
"cmake.configureSettings": {
"CMAKE_BUILD_TYPE": "Debug"
},
"cmake.buildDirectory": "${workspaceFolder}/build",
"cmake.configureOnOpen": true,
"cmake.generator": "Unix Makefiles",
"cmake.path": "/usr/bin/cmake",
"cmake.sourceDirectory": "${workspaceFolder}",
"cmake.toolsPath": "/usr/bin",
"cmake.useCMakeServer": true,
"cmake.configureArgs": [
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON"
],
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
"C_Cpp.default.compilerPath": "/usr/bin/g++",
"C_Cpp.default.includePath": [
"${workspaceFolder}/**"
],
"C_Cpp.default.intelliSenseMode": "gcc-x64",
"C_Cpp.default.browse.path": [
"${workspaceFolder}"
],
"C_Cpp.default.browse.limitSymbolsToIncludedHeaders": true,
"C_Cpp.default.browse.databaseFilename": ""
}
```
下面是一个示例的`launch.json`文件:
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/MyProject",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
```
阅读全文