更新了结合make和vscode-cpptools调试的方法:
如果您不关心 VSCode 集成调试工具,它可以让您设置断点、在运行时更改变量值、检查变量值等,并且您想要更简单、更简单、更快、更透明调用旧命令行工具的方法,跳过本节并在下面查看Code Runner。
VSCode C++ 扩展的默认配置对于低端机器来说有点慢。最糟糕的是,他们总是会重建您的可执行文件和don't support 'Start Without Debugging'。以下是适用于 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"
}
]
}
要以这种方式使用 VSCode 进行调试,首先确保在 Makefile 中将 -g 编译标志添加到 CXXFLAGS。
有关如何编写Makefile 的快速信息,请参阅this、this 或this。或查看此答案的最后一部分。
然后,创建以下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}"
}
]
}
现在您可以使用命令面板来尝试Task: Run Build Task、Task: Run Test Task、Debug: Start Debugging。
原答案
请考虑Code Runner,因为它似乎比 VSCode 的内置调试程序更快(对我来说),用于练习许多小型 C++ 代码文件。我将描述我如何使用该扩展来满足类似的要求。
- 确保您已将
PATH 配置为包含clang++,以便您可以从集成终端调用它。
您也可以使用g++,将下面的clang++ 替换为g++。我更喜欢clang++,因为它为像我这样的 C++ 初学者提供了更严格的检查。
- 安装扩展。
- 在您的 VSCode 的
settings.json 中,考虑添加以下条目:
"code-runner.clearPreviousOutput": true,
"code-runner.preserveFocus": false,
"code-runner.runInTerminal": true,
"code-runner.saveFileBeforeRun": true
- 并将最后一个自定义
code-runner.executorMap 添加到用户/工作区设置,该设置描述了当当前文件名的扩展名满足指定的扩展名时,您希望扩展名发送到终端的命令。例如:
"code-runner.executorMap": {
"cpp": "\b\b\b\b\b\b\b\b\b\bclang++ -std=c++17 $fileName -o a.out && ./a.out"
},
上面的设置告诉扩展,“当看到.cpp 文件时,发送10 Backspace 到终端(删除任何错误输入的字符)并调用clang++ -std=c++17 *filename* -o a.out && ./a.out。
我在我的 Linux 机器上使用此命令,对于 Windows,尝试将输出文件的文件扩展名更改为 .exe 并使用 .\a.exe 或简单地调用它 a.exe。
- 最后,将
Run Code 命令映射到您在 VSCode 的键盘快捷键设置中的首选键绑定。我的是将它绑定到最初绑定到Debug: Continue的F5。
编码愉快!
关于make的更新
继续阅读以了解如何利用GNU make 避免冗余编译过程并加快案例测试。我将在 Linux 上执行此操作,并且仅针对 C++,因为我没有在 Windows 或 OS X 上使用 make,而 C++ 最适合 ACM。
- 确保
make 已安装并在您的PATH 中
- 在调用
make 的同一目录下创建一个名为Makefile(或makefile)的文件。 (或者在另一个目录和make -f /path/to/Makefile)。
- 在
Makefile 中将编译器选项重新定义为您喜欢的任何内容,例如:
CXX = clang++
CXXFLAGS = -std=c++17 -g -Weverything -Werror
- 在
Makefile 中为*.out 创建自动目标规则,即:
%.out: %.cpp
$(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@
注意:必须使用Tab缩进第二行,而不是Spaces。
- 将
code-runner.executorMap 更改为:
"code-runner.executorMap": {
"cpp": "\b\b\b\b\b\b\b\b\b\bmake $fileNameWithoutExt.out && ./$fileNameWithoutExt.out"
- (可选)为
git 忽略 *.out:
echo "*.out" >> .gitignore
- (可选)删除当前目录中的
*.out:
rm *.out
现在Run Code 命令将调用make 并且make 将仅在对应的.cpp 文件比.out 文件更新时重新生成.out 文件,从而允许我们跳过编译并继续测试更流畅。
CXXFLAGS 用于 C++ 编译器选项,CFLAGS 用于 C 编译器选项。您可以使用make -p、Google 和GNU make manual#Automatic-Variables 找到其他语言编译器选项及其变量名。