如何设置 Visual Studio Code 来编译 C++ 代码? [英] How do I set up Visual Studio Code to compile C++ code?
问题描述
Microsoft 的 Visual Studio Code 编辑器相当不错,但它没有对构建 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
}
}
}
]
}
现在转到菜单File → Preferences → Keyboard Shortcuts,并为构建任务添加以下键绑定:
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屋!