vscode cmake工程launch和task文件设置

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-debug", 
            "type":"cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/${workspaceFolderBasename}",// 调试的程序全路径
            "args": ["app.conf"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/build",
            "preLaunchTask": "build", // 前置任务,前提是在 tasks.json 文件中定义了这个任务比如名为build的任务
            "environment": [],
            "externalConsole": false,
            "MIMode": "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": "vs-debug", 
            "type":"cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/${workspaceFolderBasename}",
            "args": ["app.conf"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/build",
            "preLaunchTask": "build",
            "environment": [],
            "externalConsole": false
        }
    ]
}

2.task.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {   
            // 自定义任务1: 创建build目录(命令:`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"
                ],
            }
        },
        {  
            // 自定义任务2:cmake config
            "label": "cmake",
            "group": "build",
            "type": "shell",
            "command": "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": "D:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Auxiliary\\Build\\vcvarsall.bat",
                        "args": [
                            "${input:PLATFORM}",
                            "-vcvars_ver=${input:vcvars_ver}",
                            "&&"
                        ]
                    }
                },
            },
            "dependsOn": [
                "mkbuild" 
            ]
        },
        { 
            // 自定义任务3:在build目录执行编译
            "label": "build",
            "group": "build",
            "type": "shell",
            "command": "cmake",
            "args": [
                "--build",
                "./",
                "--target",
                "all",
                "--"
            ],
            "options": {
                "cwd": "${workspaceFolder}/build",
            },
            "problemMatcher": "$gcc",
            "windows": {
                "options": {
                    "shell": {
                        "executable": "D:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Auxiliary\\Build\\vcvarsall.bat",
                        "args": [
                            "${input:PLATFORM}",
                            "-vcvars_ver=${input:vcvars_ver}",
                            "&&"
                        ]
                    }
                },
                "problemMatcher": "$msCompile"
            },
            "dependsOn": [
                "cmake"
            ]
        },
        {
            // 自定义任务4:运行程序
            "label": "run",
            "type": "shell",
            "command": "./${workspaceFolderBasename}",
            "group": "build",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": true
            },
            "options": {
                "cwd": "${workspaceFolder}/build" 
            },
            "problemMatcher": "$gcc",
            "windows":{
                "options": {
                    "cwd": "${workspaceFolder}/build"
                },
                // Use the standard MS compiler pattern to detect errors, warnings and infos
                "problemMatcher": "$msCompile"
            },
            "dependsOn":["build"]
        }        
    ],
    // 输入参数
    "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",
                "amd64",
                "arm",
                "x86_arm",
                "x86_amd64",
                "amd64_x86",
                "amd64_arm",
            ],
            "default": "amd64"
        },
        {
            "id": "vcvars_ver",
            "type": "pickString",
            "description": "What vcvars_ver do you want to create?",
            "options": [
                "14.2", // vs2019
                "14.1", // vs2017
                "14.0", // vs2015
            ],
            "default": "14.2"
        }
    ]
}

 

posted @ 2022-12-13 22:49  碎银三二两  阅读(684)  评论(0编辑  收藏  举报