小编典典

如何设置 Visual Studio Code 来编译 C++ 代码?

all

Microsoft 的Visual Studio
Code
编辑器相当不错,但它没有默认支持构建 C++ 项目。

我如何配置它来做到这一点?


阅读 22

收藏
2022-05-13

共1个答案

小编典典

构建任务是项目特定的。要创建新项目,请在 Visual Studio Code 中打开一个目录。

按照此处的说明,按Ctrl+ Shift+
P,键入Configure Tasks,选择它并按Enter

tasks.json 文件将被打开。将以下构建脚本粘贴到文件中,然后保存:

{
    "version": "0.1.0",
    "command": "make",
    "isShellCommand": true,
    "tasks": [
        {
            "taskName": "Makefile",

            // Make this the default build command.
            "isBuildCommand": true,

            // Show the output window only if unrecognized errors occur.
            "showOutput": "always",

            // Pass 'all' as the build target
            "args": ["all"],

            // Use the standard less compilation problem matcher.
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": ["relative", "${workspaceRoot}"],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}

现在转到菜单 File - Preferences - Keyboard Shortcuts ,并为构建任务添加以下键绑定:

// Place your key bindings in this file to overwrite the defaults
[
    { "key": "f8",          "command": "workbench.action.tasks.build" }
]

现在,当您按下F8Makefile 时,将执行,错误将在编辑器中加下划线。

2022-05-13