一般的cmake命令行测试方法:
cmake -S . -B build
cmake --build build
./build/cmake_debug
在vscode中使用图形化界面操作的方法
main.cpp
#include <iostream>
int main()
{
int num_a, num_b;
num_a = 10;
num_b = 20;
std::cout << "num_a = " << num_a << std::endl;
std::cout << "num_b = " << num_b << std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(cmake_debug)
add_executable(cmake_debug main.cpp)
task.json配置
{
"version": "2.0.0",
"tasks": [
{
// cmake配置
"type": "cppbuild",
"label": "CMake配置",
"command": "cmake", // cmake命令
"args": [
"-S", "${workspaceFolder}", // 源码目录
"-B", "${workspaceFolder}/Build" // 编译目录
],
"options": {
"cwd": "${workspaceFolder}" // 工作目录
},
"problemMatcher": [
// "$gcc"
"$msCompile"
],
// "group": "build",
"group": {
"kind":"build",
"isDefault": true
},
},
// 2. 构建
{
"type": "cppbuild",
"label": "CMake 构建",
"command": "cmake",
"args": [
"--build",
"Build"
],
"problemMatcher" :"$msCompile",
"group": {
"kind": "build",
"isDefault": true
},
"options": {
"cwd": "${workspaceFolder}"
},
"dependsOn":[
"CMake配置",
]
},
{
// 删除build目录
"type": "shell",
"label": "删除build目录",
"command": "rm -rf Build",
"options": {
"cwd": "${workspaceFolder}" // 工作目录
},
"problemMatcher": [
//"$gcc"
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
},
// 4 运行可执行文件
{
"type" : "shell",
"label" : "运行可执行文件",
"command": "./Build/cmake_debug",
"problemMatcher": [
//"$gcc"
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn":[
"CMake 构建"
]
},
]
}
launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "CMake调试",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/Build/cmake_debug", // 编译后的程序,需要结合CMakeLists.txt中的add_executable()函数
"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": "CMake 构建"
}
]
}
setting.json
{
"files.associations": {
"iostream": "cpp"
}
}
目录结构:
按照上述配置完成也可以正常debug
初次学习,请多指教!!!