[原创]轻松玩转vscode launch.json tasks.json linux启动c程序编译c程序

本文介绍如何在VSCode中设置C++项目的自动编译与调试,包括自定义快捷键、编译命令及配置launch.json文件,使开发过程更加高效便捷。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编译c++网上的确有现成的插件,但是一点都不好用,比如需要传递个参数就蛋疼了。
技巧1: 自定义快捷键编译
技巧2:自定义编译命令
launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch",
            "cwd": "${workspaceFolder}",
            "type": "cppdbg",
            "request": "launch",
            "preLaunchTask": "compile",
            // "program3": "${workspaceRoot}/a.out",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            // "program": "${fileDirname}/${fileBasenameNoExtension}",
            "stopAtEntry": false,
            // "setupCommands": [
            //     {
            //         "description": "Enable pretty-printing for gdb",
            //         "text": "-enable-pretty-printing",
            //         "ignoreFailures": true
            //     }
            // ],
            // "customLaunchSetupCommands": [
            //     {
            //         "text": "target-run",
            //         "description": "run target",
            //         "ignoreFailures": false
            //     }
            // ],
            "launchCompleteCommand": "exec-run",
            "linux": {
                "MIMode": "gdb",
                "miDebuggerPath": "/usr/bin/gdb"
            },
            "osx": {
                "MIMode": "lldb"
            },
            "windows": {
                "MIMode": "gdb",
                "miDebuggerPath": "C:\\MinGw\\bin\\gdb.exe"
            }
        }
    ],
    "configurations1": [
        {
            "name": " Launch C",
            "type": "cppdbg",
            "request": "launch",
            "program": "{fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "preLaunchTask1": "compile",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gcc",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    //ctrl shift p choose task
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "compile",
            "type": "shell",
            "command": "echo compile exeover&g++ -g  ${file} -o ${fileDirname}/${fileBasenameNoExtension}",
            // "command": "g++ ${file} -o ${fileDirname}/${fileBasenameNoExtension}&echo compile exeover&${fileDirname}/${fileBasenameNoExtension}",
            "cwd": "${workspaceFolder}",
            "presentation": {
                "reveal": "always",
                "panel": "new"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "compileRun",
            "type": "shell",
            // "command": "echo compile exeover&g++ -g  ${file} -o ${fileDirname}/${fileBasenameNoExtension}",
            "command": "g++ -g -g -Wall -std=c++11 ${file} -o ${fileDirname}/${fileBasenameNoExtension} -lpthread&&echo [-----compileSucc----]&&${fileDirname}/${fileBasenameNoExtension}&&echo [----executeSucc----]",
            "args": [ //传递给命令的参数
                {
                //    "value": "",
                     "quoting": "escape" //指定对内容的escape
                }
            ],
            "problemMatcher": [
            ],

            
            "options": {
                "cwd": "${fileDirname}",
                "env": { //wsl的环境变量支持有问题,设置不生效
                    "MY_NAME": "aaaaaaa"
                },
                "shell": { //shell执行的一些配置
                    "executable": "bash",
                    "args": [
                        "-c"
                    ]
                },
            },

            "presentation": {
                "echo": false,//如果 为false,怎么执行命令是不会提示的,但是 程序里面的控制信息是会执行的.
                "reveal": "always",
                // "reveal": "never",//揭露 总是 显示信息不然搞毛.
                "focus": false,
                "panel": "shared",//面板就是每次启动的时候是否新开 dedicated专用 ,shared保留之前
                "clear": true,//每次变异 是否清屏.
                "showReuseMessage": false// 不是,true就提示enter,否则 还是需要按enter 就是提示那个英文而已但是并不会自动关闭
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

cpp_properties这个配置需要微软的插件。

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++14",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

快捷键启动指定的task实现编译操作

{
        "key": "ctrl+b",
        "command": "workbench.action.tasks.runTask",
        "args": "compileRun"
    }

我配置的所有快捷键

// 使用当前快捷键设置覆盖默认设置
[
    {
        "key": "alt+d",
        "command": "editor.action.deleteLines",
        "when": "editorFocus"
    },
    {
        "key": "alt+w",
        "command": "editor.action.moveLinesUpAction",
        "when": "editorTextFocus"
    },
    {
        "key": "alt+s",
        "command": "editor.action.moveLinesDownAction",
        "when": "editorTextFocus"
    },
    {
        "key": "ctrl+shift+f",
        "command": "editor.action.format",
        "when": "editorTextFocus"
    },
    {
        "key": "f7",
        "command": "workbench.action.debug.stepOut",
        "when": "inDebugMode"
    },
    {
        "key": "alt+u",
        "command": "cursorHome",
        "when": "editorTextFocus"
    },
    {
        "key": "alt+o",
        "command": "cursorEnd",
        "when": "editorTextFocus"
    },
    {
        "key": "ctrl+j",
        "command": "workbench.action.gotoLine"
    },
    {
        "key": "f6",
        "command": "workbench.action.debug.stepOver",
        "when": "inDebugMode"
    },
    {
        "key": "shift+s",
        "command": "editor.action.insertLineAfter",
        "when": "editorTextFocus"
    },
    {
        "key": "shift+w",
        "command": "editor.action.insertLineBefore",
        "when": "editorTextFocus"
    },
    {
        "key": "alt+n",
        "command": "editor.action.copyLinesDownAction",
        "when": "editorTextFocus"
    },
    {
        "key": "f5",
        "command": "workbench.action.debug.stepInto",
        "when": "inDebugMode"
    },
    {
        "key": "alt+i",
        "command": "cursorUp",
        "when": "textInputFocus&& !parameterHintsVisible &&!suggestWidgetVisible"
    },
    {
        "key": "alt+k",
        "command": "cursorDown",
        "when": "textInputFocus"
    },
    {
        "key": "alt+j",
        "command": "cursorLeft",
        "when": "textInputFocus"
    },
    {
        "key": "alt+l",
        "command": "cursorRight",
        "when": "textInputFocus"
    },
    {
        "key": "alt+k",
        "command": "showNextParameterHint",
        "when": "editorTextFocus && parameterHintsMultipleSignatures && parameterHintsVisible"
    },
    {
        "key": "alt+k",
        "command": "showPrevParameterHint",
        "when": "editorTextFocus && parameterHintsMultipleSignatures && parameterHintsVisible"
    },
    {
        "key": "alt+k",
        "command": "selectNextSuggestion",
        "when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
    },
    {
        "key": "alt+i",
        "command": "selectPrevSuggestion",
        "when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus"
    },
    {
        "key": "alt+/",
        "command": "editor.action.triggerSuggest",
        "when": "editorHasCompletionItemProvider && textInputFocus && !editorReadonly"
    },
    {
        "key": "shift+alt+r",
        "command": "renameFile",
        "when": "explorerViewletVisible && filesExplorerFocus && !explorerResourceIsRoot && !explorerResourceReadonly && !inputFocus"
    },
    {
        "key": "alt+shift+r",
        "command": "editor.action.codeAction",
        "args": {
            "kind": "refactor.extract.function"
        },
        "when": "editorTextFocus"
    },
    {
        "key": "f2",
        "command": "-renameFile",
        "when": "explorerViewletVisible && filesExplorerFocus && !explorerResourceIsRoot && !explorerResourceReadonly && !inputFocus"
    },
    {
        "key": "alt+shift+r",
        "command": "editor.action.changeAll",
        "when": "editorTextFocus && !editorReadonly"
    },
    {
        "key": "ctrl+b",
        "command": "workbench.action.tasks.runTask",
        "args": "compileRun"
    }
]

我的项目源码https://github.com/qssq/linuxcpp/tree/master/.vscode

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值