微软的Visual Studio代码编辑器非常不错,但它对构建c++项目没有默认支持。
我如何配置它来做到这一点?
微软的Visual Studio代码编辑器非常不错,但它对构建c++项目没有默认支持。
我如何配置它来做到这一点?
当前回答
要在VS代码中构建/运行c++项目,您需要手动配置任务。Json文件,在工作空间文件夹的。vscode文件夹中。 打开任务。json,按ctrl + shift + P,并键入Configure tasks,并按enter,它将带你到tasks.json
在这里,我提供我的任务。json文件,并添加了一些注释,使文件更容易理解,可以作为配置任务的参考。Json,希望对大家有用
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build & run", //It's name of the task , you can have several tasks
"type": "shell", //type can be either 'shell' or 'process' , more details will be given below
"command": "g++",
"args": [
"-g", //gnu debugging flag , only necessary if you want to perform debugging on file
"${file}", //${file} gives full path of the file
"-o",
"${workspaceFolder}\\build\\${fileBasenameNoExtension}", //output file name
"&&", //to join building and running of the file
"${workspaceFolder}\\build\\${fileBasenameNoExtension}"
],
"group": {
"kind": "build", //defines to which group the task belongs
"isDefault": true
},
"presentation": { //Explained in detail below
"echo": false,
"reveal": "always",
"focus": true,
"panel": "shared",
"clear": false,
"showReuseMessage": false
},
"problemMatcher": "$gcc"
},
]
}
现在,直接从VS code任务文档中声明
type属性说明:
type:任务类型。对于自定义任务,这可以是shell或process。如果指定了shell,则对命令进行解释 作为shell命令(例如:bash、cmd或PowerShell)。如果 如果指定了进程,则该命令将被解释为进程到 执行。
控件可以控制终端的行为 任务中的表示属性。json。它提供以下属性:
reveal: Controls whether the Integrated Terminal panel is brought to front. Valid values are: - always - The panel is always brought to front. This is the default - never - The user must explicitly bring the terminal panel to the front using the View > Terminal command (Ctrl+`). - silent - The terminal panel is brought to front only if the output is not scanned for errors and warnings. focus: Controls whether the terminal is taking input focus or not. Default is false. echo: Controls whether the executed command is echoed in the terminal. Default is true. showReuseMessage: Controls whether to show the "Terminal will be reused by tasks, press any key to close it" message. panel: Controls whether the terminal instance is shared between task runs. Possible values are: - shared: The terminal is shared and the output of other task runs are added to the same terminal. - dedicated: The terminal is dedicated to a specific task. If that task is executed again, the terminal is reused. However, the output of a different task is presented in a different terminal. - new: Every execution of that task is using a new clean terminal. clear: Controls whether the terminal is cleared before this task is run. Default is false.
其他回答
要在VS代码中构建/运行c++项目,您需要手动配置任务。Json文件,在工作空间文件夹的。vscode文件夹中。 打开任务。json,按ctrl + shift + P,并键入Configure tasks,并按enter,它将带你到tasks.json
在这里,我提供我的任务。json文件,并添加了一些注释,使文件更容易理解,可以作为配置任务的参考。Json,希望对大家有用
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build & run", //It's name of the task , you can have several tasks
"type": "shell", //type can be either 'shell' or 'process' , more details will be given below
"command": "g++",
"args": [
"-g", //gnu debugging flag , only necessary if you want to perform debugging on file
"${file}", //${file} gives full path of the file
"-o",
"${workspaceFolder}\\build\\${fileBasenameNoExtension}", //output file name
"&&", //to join building and running of the file
"${workspaceFolder}\\build\\${fileBasenameNoExtension}"
],
"group": {
"kind": "build", //defines to which group the task belongs
"isDefault": true
},
"presentation": { //Explained in detail below
"echo": false,
"reveal": "always",
"focus": true,
"panel": "shared",
"clear": false,
"showReuseMessage": false
},
"problemMatcher": "$gcc"
},
]
}
现在,直接从VS code任务文档中声明
type属性说明:
type:任务类型。对于自定义任务,这可以是shell或process。如果指定了shell,则对命令进行解释 作为shell命令(例如:bash、cmd或PowerShell)。如果 如果指定了进程,则该命令将被解释为进程到 执行。
控件可以控制终端的行为 任务中的表示属性。json。它提供以下属性:
reveal: Controls whether the Integrated Terminal panel is brought to front. Valid values are: - always - The panel is always brought to front. This is the default - never - The user must explicitly bring the terminal panel to the front using the View > Terminal command (Ctrl+`). - silent - The terminal panel is brought to front only if the output is not scanned for errors and warnings. focus: Controls whether the terminal is taking input focus or not. Default is false. echo: Controls whether the executed command is echoed in the terminal. Default is true. showReuseMessage: Controls whether to show the "Terminal will be reused by tasks, press any key to close it" message. panel: Controls whether the terminal instance is shared between task runs. Possible values are: - shared: The terminal is shared and the output of other task runs are added to the same terminal. - dedicated: The terminal is dedicated to a specific task. If that task is executed again, the terminal is reused. However, the output of a different task is presented in a different terminal. - new: Every execution of that task is using a new clean terminal. clear: Controls whether the terminal is cleared before this task is run. Default is false.
有了更新的VS Code,你可以用以下方式做到这一点:
点击(Ctrl+P)并输入:
ext install cpptools
打开一个文件夹(Ctrl+K & Ctrl+O),并在文件夹中创建一个扩展名为.cpp的新文件(例如:hello.cpp): 输入代码并点击保存。 点击(Ctrl+Shift+P和类型,配置任务运行器,然后选择在列表底部的其他。 在同一个文件夹中创建一个名为build.bat的批处理文件,并在文件主体中包含以下代码:
@echo off
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64
set compilerflags=/Od /Zi /EHsc
set linkerflags=/OUT:hello.exe
cl.exe %compilerflags% hello.cpp /link %linkerflags%
编辑任务。Json文件,保存如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "build.bat",
"isShellCommand": true,
//"args": ["Hello World"],
"showOutput": "always"
}
点击(Ctrl+Shift+B运行构建任务。这将为项目创建.obj和.exe文件。 要调试项目,请按F5并选择c++ (Windows)。 在发射。Json文件,编辑以下行并保存文件:
"program": "${workspaceRoot}/hello.exe",
按F5。
一个新的2.0.0任务的makefile任务示例。json版本。
在下面的代码片段中,我希望它们会有用。
{
"version": "2.0.0",
"tasks": [
{
"label": "<TASK_NAME>",
"type": "shell",
"command": "make",
// use options.cwd property if the Makefile is not in the project root ${workspaceRoot} dir
"options": {
"cwd": "${workspaceRoot}/<DIR_WITH_MAKEFILE>"
},
// start the build without prompting for task selection, use "group": "build" otherwise
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
// arg passing example: in this case is executed make QUIET=0
"args": ["QUIET=0"],
// Use the standard less compilation problem matcher.
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["absolute"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]
}
有一个更简单的方法来编译和运行c++代码,不需要配置:
安装代码运行器扩展 在文本编辑器中打开您的c++代码文件,使用快捷键Ctrl+Alt+N,或者按F1,然后选择/键入运行代码,或者右键单击文本编辑器,然后在上下文菜单中单击运行代码,代码将被编译并运行,输出将显示在输出窗口中。
此外,您可以在设置中更新配置。如果你想使用不同的c++编译器,c++的默认配置如下:
"code-runner.executorMap": {
"cpp": "g++ $fullFileName && ./a.out"
}
出于对缺乏清晰文件的失望, 我在github上创建了一个Mac项目,应该可以工作(构建和调试):
vscode-mac-c-example
注意,它需要XCode和VSCode Microsoft cpptools扩展。
我计划在Windows和linux上做同样的事情(除非微软先写了像样的文档…)