微软的Visual Studio Code编辑器非常不错,但它没有默认支持构建C++项目的功能。
我该如何配置才能实现此功能?
微软的Visual Studio Code编辑器非常不错,但它没有默认支持构建C++项目的功能。
我该如何配置才能实现此功能?
编译和运行C++代码的方法更简单,无需配置:
Ctrl+Alt+N
或按F1
并选择/输入Run Code
,或右键单击文本编辑器,然后在上下文菜单中点击Run Code
,代码将被编译和运行,并在输出窗口中显示输出。此外,您可以使用不同的C++编译器更新settings.json中的配置,C++的默认配置如下:
"code-runner.executorMap": {
"cpp": "g++ $fullFileName && ./a.out"
}
Ctrl+Alt+M
。要使用标准输入读取数据,您可以转到文件
->首选项
->设置
来设置 "code-runner.runInTerminal": true
。有关更多详细信息,请参考 https://github.com/formulahendry/vscode-code-runner/issues/91。 - Jun Han构建任务是针对项目的。要创建一个新项目,请在 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
}
}
}
]
}
// Place your key bindings in this file to overwrite the defaults
[
{ "key": "f8", "command": "workbench.action.tasks.build" }
]
ctrl+alt+b
。 - Melroy van den Berg一个适用于新的2.0.0 tasks.json版本的makefile任务示例。
下面的代码片段中有一些注释,我希望它们会有用。
{
"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
}
}
}
]
}
.vscode
。对于Git版本控制,一种可能性是在.gitignore
中使用模式,如!.vscode/tasks.json
。 - attdona以下是我如何为C++配置VS:
确保将MinGW安装的路径更改为适当的路径。
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (GDB)",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x86",
"program": "${workspaceRoot}\\${fileBasename}.exe",
"miDebuggerPath":"C:\\mingw-w64\\bin\\gdb.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"externalConsole": true,
"preLaunchTask": "g++"
}
]
}
tasks.json
{
"version": "0.1.0",
"command": "g++",
"args": ["-g","-std=c++11","${file}","-o","${workspaceRoot}\\${fileBasename}.exe"],
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++",
"C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/x86_64-w64-mingw32",
"C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/backward",
"C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include",
"C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/tr1",
"C:/mingw-w64/x86_64-w64-mingw32/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=6",
"__cdecl=__attribute__((__cdecl__))"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"${workspaceRoot}",
"C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++",
"C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/x86_64-w64-mingw32",
"C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/backward",
"C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include",
"C:/mingw-w64/lib/gcc/x86_64-w64-mingw32/7.2.0/include/c++/tr1",
"C:/mingw-w64/x86_64-w64-mingw32/include"
]
},
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
],
"version": 3
}
参考资料:
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)。如果指定了process,则命令将被解释为要执行的进程。
通过在tasks.json中使用presentation属性可以控制终端的行为, 它提供以下属性:
reveal: 控制是否将集成终端面板置于前台。有效值为: - always - 面板始终置于前台。这是默认值。 - never - 用户必须显式使用查看>终端命令(Ctrl+`)将终端面板置于前台。 - silent - 仅当输出未被扫描以查找错误和警告时,终端面板才会置于前台。
focus: 控制终端是否正在接受输入焦点。默认值为false。
echo: 控制是否在终端中回显执行的命令。默认值为true。
showReuseMessage: 控制是否显示“终端将被任务重用,请按任意键关闭”消息。
panel: 控制终端实例是否在任务运行之间共享。可能的值为: - shared: 终端是共享的,其他任务运行的输出添加到同一个终端中。 - dedicated: 终端专用于特定任务。如果再次执行该任务,则会重复使用该终端。但是,不同任务的输出会呈现在不同的终端中。 - new: 每次执行该任务都使用新的干净终端。
clear: 控制在运行此任务之前是否清除终端。默认值为false。
由于缺乏清晰的文档,我在Github上创建了一个Mac项目,应该可以正常工作(构建和调试):
请注意,它需要XCode和VSCode Microsoft C++工具扩展。
我计划为Windows和Linux做同样的事情(除非Microsoft先撰写出优秀的文档......)。
https://code.visualstudio.com/docs/editor/tasks
...但请注意与以下内容的冲突:
https://code.visualstudio.com/docs/languages/cpp
警告:以下所有答案中,任何以版本标签2.0.0以下开头的内容都已过时。
目前我有最接近的东西。请注意,我将大部分重活交给脚本处理,这并没有给我任何我可以使用的菜单条目,并且没有什么好的方法可以在调试和发布之间进行选择,除非只是在这里再创建另外三个明确的条目。总之,这就是我目前可以容忍的.vscode/tasks.json文件:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build project",
"type": "shell",
"command": "buildscripts/build-debug.sh",
"args": [],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
// Reveal the output only if unrecognized errors occur.
"echo": true,
"focus": false,
"reveal": "always",
"panel": "shared"
},
// Use the standard MS compiler pattern to detect errors, warnings and infos
"options": {
"cwd": "${workspaceRoot}"
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}/DEBUG"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
{
"label": "rebuild project",
"type": "shell",
"command": "buildscripts/rebuild-debug.sh",
"args": [],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
// Reveal the output only if unrecognized errors occur.
"echo": true,
"focus": false,
"reveal": "always",
"panel": "shared"
},
// Use the standard MS compiler pattern to detect errors, warnings and infos
"options": {
"cwd": "${workspaceRoot}"
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}/DEBUG"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
{
"label": "clean project",
"type": "shell",
"command": "buildscripts/clean-debug.sh",
"args": [],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
// Reveal the output only if unrecognized errors occur.
"echo": true,
"focus": false,
"reveal": "always",
"panel": "shared"
},
// Use the standard MS compiler pattern to detect errors, warnings and infos
"options": {
"cwd": "${workspaceRoot}"
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}/DEBUG"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]
}
以下是我使用g++编译器为C++配置VS的方法,包括调试选项,非常有效:
tasks.json文件
{
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
// compiles and links with debugger information
"args": ["-g", "-o", "hello.exe", "hello.cpp"],
// without debugger information
// "args": ["-o", "hello.exe", "hello.cpp"],
"showOutput": "always"
}
launch.json文件
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (Windows)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/hello.exe",
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGw\\bin\\gdb.exe",
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"externalConsole": false,
"visualizerFile": "${workspaceRoot}/my.natvis"
}
]
}
我在VS Code中安装了'C/C++ for Visual Studio Code'扩展
使用更新的VS Code,您可以按照以下方式完成操作:
ext install cpptools
打开一个文件夹(Ctrl+K & Ctrl+O),并在文件夹内创建一个扩展名为.cpp的新文件(例如:hello.cpp):
输入代码并保存。
按下 (Ctrl+Shift+P),然后键入 Configure task runner
,然后在列表底部选择 other
。
在相同的文件夹中创建一个名为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%
{
// 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)。
在launch.json文件中,编辑以下行并保存该文件:
"program": "${workspaceRoot}/hello.exe",