步骤一: c++ 工程含有正确的CMakeLists.txt , 使用vscode 打开工程
在.vscode要建立三个json文件才能对Cmake工程进行编译和调试,分别是c_cpp_properties.json,launch.json和tasks.json
步骤2: Ctrl+Shift+P,输入C/C++,选择C/C++: Edit Configurations(JSON) ,创建并修改c_cpp_properties.json文件,修改为如下:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
],
"defines": [],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++98",
"intelliSenseMode": "linux-gcc-x64"
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
步骤3: choose Run > Add Configuration... and then choose C++ (GDB/LLDB),配置修改launch.json文件,配置如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
// Resolved by CMake Tools:
"program": "${workspaceFolder}/build/Hello",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
步骤4: choose Terminal > Configure Default Build Task. A dropdown appears showing various predefined build tasks for C++ compilers. Choose C/C++: g++ build active file. 修改编辑
tasks.json是编译任务的文件(或者用默认的tasks)
{
"version": "2.0.0",
"tasks": [
{
"label": "make build",//编译的项目名,build
"type": "shell",
"command": "cd ./build ;cmake .. ;make",//编译命令
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "clean",
"type": "shell",
"command": "make clean",
}
]
}
步骤5: ctrl+shift+B运行代码,成功
步骤6:F5 debug 调试