如何设置Visual Studio Code来编译C ++代码? [英] How do I set up Visual Studio Code to compile C++ code?
问题描述
Microsoft的 Visual Studio代码编辑器非常好,但是它不默认支持构建C ++项目.
Microsoft's Visual Studio Code editor is quite nice, but it has no default support for building C++ projects.
我如何配置它来做到这一点?
How do I configure it to do this?
推荐答案
构建任务是特定于项目的.要创建一个新项目,请在Visual Studio Code中打开一个目录.
The build tasks are project specific. To create a new project, open a directory in Visual Studio Code.
按照说明此处,按 Ctrl + Shift + P ,键入Configure Tasks
,选择它并按 Enter .
Following the instructions here, press Ctrl + Shift + P, type Configure Tasks
, select it and press Enter.
tasks.json文件将打开.将以下构建脚本粘贴到文件中,然后将其保存:
The tasks.json file will be opened. Paste the following build script into the file, and save it:
{
"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
}
}
}
]
}
现在转到菜单文件→首选项→键盘快捷键,并为构建任务添加以下按键绑定:
Now go to menu File → Preferences → Keyboard Shortcuts, and add the following key binding for the build task:
// Place your key bindings in this file to overwrite the defaults
[
{ "key": "f8", "command": "workbench.action.tasks.build" }
]
现在,当您按 F8 时,将执行Makefile,并且在编辑器中将在下划线显示错误.
Now when you press F8 the Makefile will be executed, and errors will be underlined in the editor.
这篇关于如何设置Visual Studio Code来编译C ++代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!