此外,Visual Studio Code带有集成终端,因此如果程序输出可以重定向到终端会很好。同时,我们如何将键盘快捷方式映射到运行此任务上。
我正在Windows 10上使用Visual Studio Code 2019和MinGW G++编译器。
编辑:
我尝试了Escape0707下面的答案,并尝试使用默认键绑定
Ctrl + Alt + N
执行'Run Code',但我得到了这个错误。
Ctrl + Alt + N
执行'Run Code',但我得到了这个错误。
make
和vscode-cpptools
调试组合使用:如果您不关心VSCode集成调试工具,这些工具将为您提供设置断点、在运行时更改变量值、检查变量值等功能,而且您希望以某种更简单、更快速、更透明的方式调用老旧的命令行工具,则可以跳过本节并查看下面的Code Runner
。
默认配置的VSCode C++扩展对于低端机器来说有些慢。最糟糕的部分是它们总是会重新构建您的可执行文件,并且不支持“开始调试前启动”。以下是Linux(当然还有远程-WSL)的解决方法。
为了解决第一个问题,您需要设置make
(对于简单的单个源文件编译,您只需要安装make)来构建您的源代码,并在tasks.json
中设置构建任务。为了解决第二个问题,您需要创建另一个任务,仅在第一个任务完成后运行构建的可执行文件:
使用Intellisense了解每个配置属性。
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"presentation": {
"clear": true,
"focus": true,
"panel": "shared"
},
"tasks": [
{
"label": "make active file",
"type": "shell",
"command": "make",
"args": ["${fileBasenameNoExtension}.out"],
"problemMatcher": "$gcc",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "run active file executable without debuging",
"type": "shell",
"command": "${fileDirname}/${fileBasenameNoExtension}.out",
"presentation": {
"clear": false
}
},
{
"label": "make and run active file without debuging",
"group": {
"kind": "test",
"isDefault": true
},
"dependsOn": [
"make active file",
"run active file executable without debuging"
],
"dependsOrder": "sequence"
}
]
}
-g
编译标志添加到Makefile
的CXXFLAGS
中。
然后,创建以下
launch.json
:
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "make and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.out",
"cwd": "${workspaceFolder}",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "${defaultBuildTask}"
}
]
}
任务:运行构建任务
、任务:运行测试任务
、调试:开始调试
。
请考虑使用Code Runner
,因为对于我来说,它似乎比VSCode内置的调试程序更快,适用于练习许多小型C++代码文件。我将描述如何使用该扩展满足类似要求。
PATH
to include clang++
so you can invoke it from the integrated terminal.
You can also use
g++
by substituteclang++
below withg++
. I preferclang++
as it provides stricter checks for C++ beginners like me.
settings.json
, consider adding the following entries:
"code-runner.clearPreviousOutput": true,
"code-runner.preserveFocus": false,
"code-runner.runInTerminal": true,
"code-runner.saveFileBeforeRun": true
code-runner.executorMap
to user/workspace setting that describes which command you would like the extension to send to the terminal when current filename's extension meets the specified ones. For example:
"code-runner.executorMap": {
"cpp": "\b\b\b\b\b\b\b\b\b\bclang++ -std=c++17 $fileName -o a.out && ./a.out"
},
The above setting tells the extension, "When see a .cpp
file, send 10 Backspace to terminal (to delete any mistyped characters) and call clang++ -std=c++17 *filename* -o a.out && ./a.out
.
I use this command on my Linux machine, for Windows, try change the filename extension of the output file to
.exe
and invoke it with.\a.exe
or simplya.exe
.
Run Code
command to your preferred keybinding in VSCode's Keyboard Shortcuts settings. Mine is to bind it to F5 which is originally bound to Debug: Continue
.快乐编程!
make
的更新继续阅读,了解如何通过利用GNU make
避免冗余编译过程并加速案例测试。我将在Linux上进行此操作,并仅限于C++,因为我没有在Windows或OS X上使用make
,而C++是ACM最好的语言。
make
is installed and in your PATH
Makefile
(or makefile
) under the same directory you invoke make
. (Or in another directory and make -f /path/to/Makefile
).Makefile
, e.g.:
CXX = clang++
CXXFLAGS = -std=c++17 -g -Weverything -Werror
*.out
in the Makefile
, i.e.:
%.out: %.cpp
$(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@
Attention: must use Tab to indent the second line, not Spaces.
code-runner.executorMap
to :
"code-runner.executorMap": {
"cpp": "\b\b\b\b\b\b\b\b\b\bmake $fileNameWithoutExt.out && ./$fileNameWithoutExt.out"
*.out
for git
:
echo "*.out" >> .gitignore
*.out
in current directory:
rm *.out
Run Code
命令将调用 make
,当相应的 .cpp
文件比 .out
文件更新时,make
将只重新生成 .out
文件,使我们能够更加顺畅地跳过编译并进行测试。
CXXFLAGS
用于 C++ 编译器选项,CFLAGS
用于 C 编译器选项。您可以使用make -p
、Google 和GNU make
手册#自动变量 找到其他语言编译器选项及其变量名。
Ctrl+,
打开的 设置
选项卡的右上角,有一个名为 打开设置(JSON)
的按钮。对于第三步中的 settings.json,您也可以按 Ctrl+Shift+P
并搜索 首选项:打开设置(JSON)
。 - Escape0707./filename
运行。然而,在 Windows 上,你应该给出输出文件名 a.exe
,等等。并通过调用 .\a.exe
或 a.exe
来运行它。我会编辑我的答案来注意我使用的操作系统。 - Escape0707GNU make
。希望这能更好地帮助你。 - Escape0707{
"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}\\${fileBasenameNoExtension}", //output file name
"&&", //to join building and running of the file
"${workspaceFolder}\\${fileBasenameNoExtension}"
],
"group": {
"kind": "build", //defines to which group the task belongs
"isDefault": true
},
"presentation": {
"echo": false,
"reveal": "always",
"focus": true,
"panel": "shared",
"clear": false,
"showReuseMessage": false
},
"problemMatcher": "$gcc"
},
]
}
tasks.json
中的所有属性都是用于根据您的需求定制构建任务的,可以随意更改为最喜欢的内容。您可以在VSCode任务文档中了解演示属性(以及其他内容)。