使用vscode调试WSL内的C++程序
主要内容
- WSL配置
- vscode配置
WSL配置
安装C++编译环境
1 | apt install build-essential gdb |
查看build-essential的依赖,可以看出的,build-essential是打包了gcc、g++、make等编译环境的工具。
1 | root@DESKTOP-Q7IC13H:~/face_to_future# apt-cache depends build-essential |
那么gcc与g++是什么关系呢
Several versions of the compiler (C, C++, Objective-C, Ada, Fortran, Java and treelang) are integrated; this is why we use the name “GNU Compiler Collection”. GCC can compile programs written in any of these languages. The Ada, Fortran, Java and treelang compilers are described in separate manuals.
“GCC” is a common shorthand term for the GNU Compiler Collection. This is both the most general name for the compiler, and the name used when the emphasis is on compiling C programs (as the abbreviation formerly stood for “GNU C Compiler”).
When referring to C++ compilation, it is usual to call the compiler “G++”. Since there is only one compiler, it is also accurate to call it “GCC” no matter what the language context; however, the term “G++” is more useful when the emphasis is on compiling C++ programs.
Similarly, when we talk about Ada compilation, we usually call the compiler “GNAT”, for the same reasons.
We use the name “GCC” to refer to the compilation system as a whole, and more specifically to the language-independent part of the compiler. For example, we refer to the optimization options as affecting the behavior of “GCC” or sometimes just “the compiler”.
Front ends for other languages, such as Mercury and Pascal exist but have not yet been integrated into GCC. These front ends, like that for C++, are built in subdirectories of GCC and link to it. The result is an integrated compiler that can compile programs written in C, C++, Objective-C, or any of the languages for which you have installed front ends.
In this manual, we only discuss the options for the C, Objective-C, and C++ compilers and those of the GCC core. Consult the documentation of the other front ends for the options to use when compiling programs written in other languages.
G++ is a compiler, not merely a preprocessor. G++ builds object code directly from your C++ program source. There is no intermediate C version of the program. (By contrast, for example, some other implementations use a program that generates a C program from your C++ source.) Avoiding an intermediate C representation of the program means that you get better object code, and better debugging information. The GNU debugger, GDB, works with this information in the object code to give you comprehensive C++ source-level editing capabilities (see C and C++ (Debugging with GDB)).
用man gcc 和man g++ 出来的页面是一样的,摘抄两段
gcc - GNU project C and C++ compiler.
The usual way to run GCC is to run the executable called gcc, or machine-gcc when cross-compiling, or machine-gcc-version to run a specific version of GCC. When you compile C++ programs, you should invoke GCC as g++ instead.
C++ source files conventionally use one of the suffixes .C, .cc, .cpp, .CPP, .c++, .cp, or .cxx; C++ header files often use .hh, .hpp, .H, or (for shared template code) .tcc; and preprocessed C++ files use the suffix .ii. GCC recognizes files with these names and compiles them as C++
programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).
However, the use of gcc does not add the C++ library. g++ is a program that calls GCC and automatically specifies linking against the C++library. It treats .c, .h and .i files as C++ source files instead of C source files unless -x is used. This program is also useful when precompiling a C header file with a .h extension for use in C++ compilations. On many systems, g++ is also installed with the name c++.
总之c++最好用g++编译,gcc继承了除了c很多语言的编译器
安装gdb
the GNU debugger, c与c++的调试工具
确认环境已配置正确1
2whereis g++
whereis gdb
vscode配置
.vscode 里面有三个文件需要配置
- c_cpp_properties.json (compiler path and IntelliSense settings)
- tasks.json (build instructions)
- launch.json (debugger settings)
c/c++环境配置
按 Ctrl+Shift+P 命令行面板(Command Palette)
找到c/c++ Edit configurations(UI),修改里面配置,会同步到c_cpp_properties.json里面1
2
3complier path: /usr/bin/g++
intelliSense mode: gcc-x64
cStandard: c++17
编译环境配置
在命令行面板内输入task,选择Tasks:Configure Default Build Task,创建一个tasks.json1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28{
"version": "2.0.0",
"windows": {
"options": {
"shell": {
"executable": "bash.exe",
"args": ["-c"]
}
}
},
"tasks": [
{
"label": "build hello world on WSL",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-o",
"/home/<your linux user name>/projects/helloworld/helloworld.out",
"helloworld.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
debug设置配置
1 | { |
参考: