微软的Visual Studio代码编辑器非常不错,但它对构建c++项目没有默认支持。
我如何配置它来做到这一点?
微软的Visual Studio代码编辑器非常不错,但它对构建c++项目没有默认支持。
我如何配置它来做到这一点?
当前回答
出于对缺乏清晰文件的失望, 我在github上创建了一个Mac项目,应该可以工作(构建和调试):
vscode-mac-c-example
注意,它需要XCode和VSCode Microsoft cpptools扩展。
我计划在Windows和linux上做同样的事情(除非微软先写了像样的文档…)
其他回答
下面是我如何为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
}
参考:
C/ c++ VS Code c_cpp_properties。json模板
构建任务是特定于项目的。要创建一个新项目,请在Visual Studio Code中打开一个目录。
按照这里的说明,按Ctrl + Shift + P,输入Configure Tasks,选择它并按Enter。
的任务。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" }
]
现在,当您按下F8时,Makefile将被执行,错误将在编辑器中下划线显示。
要在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.
微软现在有一个C/ c++语言扩展。你可以通过“快速打开”(Ctrl+p)来安装它,然后输入:
ext install cpptools
你可以在这里阅读:
https://blogs.msdn.microsoft.com/vcblog/2016/03/31/cc-extension-for-visual-studio-code/
截至2016年5月,这是非常基本的。
如果你的项目有一个CMake配置,那么设置VSCode就很简单了,比如设置任务。Json如下:
{
"version": "0.1.0",
"command": "sh",
"isShellCommand": true,
"args": ["-c"],
"showOutput": "always",
"suppressTaskName": true,
"options": {
"cwd": "${workspaceRoot}/build"
},
"tasks": [
{
"taskName": "cmake",
"args": ["cmake ."]
},
{
"taskName": "make",
"args" : ["make"],
"isBuildCommand": true,
"problemMatcher": {
"owner": "cpp",
"fileLocation": "absolute",
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]
}
这假设在工作区的根目录中构建了一个带有CMake配置的文件夹。
还有一个CMake集成扩展,它在VScode中添加了一个“CMake build”命令。
PS !问题matcher是为clang-builds设置的。要使用GCC,我相信你需要将fileLocation更改为relative,但我还没有测试过这个。