本人是VS Code的忠实粉丝,之前Python、Golang等开发都采用VS Code进行,也想把这个习惯带到C++开发之上,但是之前一直搞不定如何编译多个文件,只能将所有程序编写到一个cpp文件中,这显然很蠢,因此我开始使用Visual Studio,但是VS太过于heavy,实在是用不习惯。
最近在迁移到WSL进行C++开发之后重新开始学习简单的g++使用,因而学会了如何通过修改launch.json
和tasks.json
两个文件编译多个文件,并使其支持调试。
1 采用g++指令手动进行多个文件的编译
首先创建一个文件夹include_test
,并利用VS Code打开该文件夹,新建三个文件,分别为hello.h
,hello.cpp
,main.cpp
,各自的内容如下所示:
// hello.h
#ifndef _HELLO_H
#define _HELLO_H
void hello_world();
#endif
// hello.cpp
#include <iostream>
using namespace std;
void hello_world() {
cout << "hello world 1" << endl;
cout << "hello world 2" << endl;
}
// main.cpp
#include "hello.h"
int main() {
hello_world();
}
此时目录include_test
中的文件如图所示:
main.cpp
的功能也十分简单,就是用来打印我们熟悉的“hello word”两次,此时我们可以在目录include_test
下执行一条g++的编译指令,如下所示:
g++ -ggdb -o include_test main.cpp hello.cpp
# 通过g++进行编译 -ggdb:尽量生成gdb可以使用的调试信息 -o:输出文件路径与名称 main.cpp hello.cpp:所需要编译文件
此时目录include_test
中会出现编译所生成的include_test
文件,此时执行:
./include_test
则会出现:
程序可以成功运行,自此我们可以利用g++手动进行多个文件编译,我们在以上过程中执行了非常简单的两步:
- 编译
- 运行
而利用VS Code进行编译的关键就是利用json文件控制这两个步骤
2 让VS Code帮助我们执行手动编译
我们用VS Code打开include_test
文件夹,并在打开main.cpp
页面后,点击“运行->启动调试->C++(GDB/LLDB)->g++ 生成和调试活动文件”,此时会报错,点击”取消“,此时include_test
目录中会产生.vscode
文件夹及其内部的两个json格式文件tasks.json
和launch.json
。
我们前面提到,我们实际上在手动编译与运行中,只有两步:编译和运行,而文件tasks.json
和launch.json
恰好可以理解为分管这两个步骤,首先观察tasks.json
的内容,其中command
和args
两项较为重要,尤其是args
需要我们修改,我们重点看一下注释:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++", // 所运行的程序,即运行g++
"args": [ // g++编译时的参数
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
再观察一下launch.json
,其中program
需要我们修改,重点看一下注释:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}", // 可理解为编译后执行的程序
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ 生成活动文件",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
然而,以上文件并不能让我们进行正常的调试,因此我们进行如下修改,tasks.json
和launch.json
的修改分别如下:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
// "-fdiagnostics-color=always",
// "-g",
// "${file}",
// "-o",
// "${fileDirname}/${fileBasenameNoExtension}"
"-ggdb", // 尽量生成gdb可以使用的调试信息
"-o", // 指定输出路径和文件名为...
"${wordspaceFolder}/include_test", // ...工作目录下的include_test
"main.cpp", // 待编译文件1
"hello.cpp" // 待编译文件2
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
// "program": "${fileDirname}/${fileBasenameNoExtension}",
"program": "${fileDirname}/include_test", // 修改需要运行的程序为工作目录下的include_test
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ 生成活动文件",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
在保存以上修改后,我们重新切换到main.cpp,按下F5,即可出现程序运行结果:
并且,我们可以快乐的进行单步调试:
3 总结
其实用VS Code进行多个文件的编译原理很简单,首先要熟悉g++的使用,再去想象如何令VS Code代替我们执行g++命令。