vscode搭配cmake配置c++开发环境
1.launch.json设置启动调试
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gdb-build-run",
"type":"cppdbg",
"request": "launch",
// program: 设置可执行程序路径
"program": "${workspaceFolder}/build/${workspaceFolderBasename}",
// args: 设置可执行程序的命令行参数, 如程序配置文件"app.conf"
"args": ["app.conf"],
// stopAtEntry: 设置是否在程序入口暂停
"stopAtEntry": true,
// cwd: 设置当前工作目录,即程序运行目录
"cwd": "${workspaceFolder}/build",
// preLaunchTask: 设置前置任务(即在 tasks.json 文件中定义的任务, 如 label 为 build 的任务)
"preLaunchTask": "build",
// environment:设置程序运行时的环境变量
"environment": [],
"MIMode": "gdb",
// setupCommands: GDB调试器设置如优化打印信息
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
{
"name": "gdb-run",
"type":"cppdbg",
"request": "launch",
// program: 设置可执行程序路径
"program": "${workspaceFolder}/build/${workspaceFolderBasename}",
// args: 设置可执行程序的命令行参数, 如程序配置文件"app.conf"
"args": ["app.conf"],
// stopAtEntry: 设置是否在程序入口暂停
"stopAtEntry": true,
// cwd: 设置当前工作目录,即程序运行目录
"cwd": "${workspaceFolder}/build",
// preLaunchTask: 设置前置任务(即在 tasks.json 文件中定义的任务, 如 label 为 build 的任务)
"preLaunchTask": "",
// environment:设置程序运行时的环境变量
"environment": [],
"MIMode": "gdb",
// setupCommands: GDB调试器设置如:优化打印信息
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
{
"name": "msvc-build-run",
"type":"cppvsdbg",
"request": "launch",
// program: 设置可执行程序路径
"program": "${workspaceFolder}/build/${workspaceFolderBasename}",
// args: 设置可执行程序的命令行参数, 如程序配置文件"app.conf"
"args": ["app.conf"],
// stopAtEntry: 设置是否在程序入口暂停
"stopAtEntry": true,
// cwd: 设置当前工作目录,即程序运行目录
"cwd": "${workspaceFolder}/build",
// preLaunchTask: 设置前置任务(即在 tasks.json 文件中定义的任务, 如 label 为 build 的任务)
"preLaunchTask": "build",
// environment:设置程序运行时的环境变量
"environment": [],
// console: 设置是否需要启动外部终端窗口,默认是false
"console": "externalTerminal"
},
{
"name": "msvc-run",
"type":"cppvsdbg",
"request": "launch",
// program: 设置可执行程序路径
"program": "${workspaceFolder}/build/${workspaceFolderBasename}",
// args: 设置可执行程序的命令行参数, 如程序配置文件"app.conf"
"args": ["app.conf"],
// stopAtEntry: 设置是否在程序入口暂停
"stopAtEntry": true,
// cwd: 设置当前工作目录,即程序运行目录
"cwd": "${workspaceFolder}/build",
// preLaunchTask: 设置前置任务(即在 tasks.json 文件中定义的任务, 如 label 为 build 的任务)
"preLaunchTask": "",
// environment:设置程序运行时的环境变量
"environment": [],
// console: 设置是否需要启动外部终端窗口,默认是false
"console": "externalTerminal"
}
]
}
2.task.json设置编译任务
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
// 自定义任务 mkbuild : 创建build目录(linux命令:`mkdir -p build`,windows在powershell中执行命令`mkdir -Force build`)
"label": "mkbuild",
"group": "build",
"command": "mkdir",
"type": "shell",
"args": [
"-p",
"build"
],
"windows": {
"options": {
"shell": {
"executable": "powershell.exe"
}
},
"args": [
"-Force",
"build"
],
}
},
{
// 自定义任务 cmake :CMake构建
"label": "cmake",
"group": "build",
"type": "shell",
"command": "cmake",
// CMake参数
"args": [
"-DCMAKE_BUILD_TYPE=${input:CMAKE_BUILD_TYPE}",
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
".."
],
"options": {
// 设置当前工作目录
"cwd": "${workspaceFolder}/build",
},
"windows": {
"args": [
"-DCMAKE_BUILD_TYPE=${input:CMAKE_BUILD_TYPE}",
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
"..",
"-G",
"\"NMake Makefiles\""
],
"options": {
"shell": {
"executable": "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Auxiliary/Build/vcvarsall.bat",
"args": [
"${input:PLATFORM}",
"-vcvars_ver=${input:MSVC_VERSION}",
"&&"
]
}
}
},
"dependsOn": [
"mkbuild"
]
},
{
// 自定义任务 build:在build目录执行编译
"label": "build",
"group": "build",
"type": "shell",
"command": "cmake",
"args": [
"--build",
"./",
"--target",
"all",
"--"
],
"options": {
"cwd": "${workspaceFolder}/build",
},
"problemMatcher": "$gcc",
"windows": {
"options": {
"shell": {
"executable": "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Auxiliary/Build/vcvarsall.bat",
"args": [
"${input:PLATFORM}",
"-vcvars_ver=${input:MSVC_VERSION}",
"&&"
]
}
},
"problemMatcher": "$msCompile"
},
"dependsOn": [
"cmake"
]
}
],
// 设置需要用户选择的输入参数
"inputs": [
{
"id": "CMAKE_BUILD_TYPE",
"type": "pickString",
"description": "What CMAKE_BUILD_TYPE do you want to create?",
"options": [
"Debug",
"Release",
"RelWithDebInfo",
"MinSizeRel",
],
"default": "Debug"
},
{
"id": "PLATFORM",
"type": "pickString",
"description": "What PLATFORM do you want to create?",
"options": [
"x86",
"x64",
"amd64",
"amd64_x86"
],
"default": "x64"
},
{
"id": "MSVC_VERSION",
"type": "pickString",
"description": "Which msvc version do you want to create?",
"options": [
"14.3", // vs2022
"14.2", // vs2019
"14.1", // vs2017
"14.0", // vs2015
],
"default": "14.2"
}
]
}