微软的Visual Studio代码编辑器非常不错,但它对构建c++项目没有默认支持。
我如何配置它来做到这一点?
微软的Visual Studio代码编辑器非常不错,但它对构建c++项目没有默认支持。
我如何配置它来做到这一点?
当前回答
你可以在Visual Studio Code的2.0.0版本任务中引用这个最新的要点,https://gist.github.com/akanshgulati/56b4d469523ec0acd9f6f59918a9e454
您可以轻松地编译和运行每个文件,而无需更新任务。它是通用的,并且还为输入项打开终端。
其他回答
如果你的项目有一个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,但我还没有测试过这个。
微软现在有一个C/ c++语言扩展。你可以通过“快速打开”(Ctrl+p)来安装它,然后输入:
ext install cpptools
你可以在这里阅读:
https://blogs.msdn.microsoft.com/vcblog/2016/03/31/cc-extension-for-visual-studio-code/
截至2016年5月,这是非常基本的。
要在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.
下面是我如何使用g++编译器配置我的VS for c++,它工作得很好,包括调试选项:
任务。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"
}
发射。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”扩展
有一个更简单的方法来编译和运行c++代码,不需要配置:
安装代码运行器扩展 在文本编辑器中打开您的c++代码文件,使用快捷键Ctrl+Alt+N,或者按F1,然后选择/键入运行代码,或者右键单击文本编辑器,然后在上下文菜单中单击运行代码,代码将被编译并运行,输出将显示在输出窗口中。
此外,您可以在设置中更新配置。如果你想使用不同的c++编译器,c++的默认配置如下:
"code-runner.executorMap": {
"cpp": "g++ $fullFileName && ./a.out"
}