Visual Studio 2022使用CMake+MinGW+Clang+LLDB作为开发环境
笔者前面写了两篇关于Visual Studio 2022使用MinGW的博文:《 Visual Studio 2022使用MinGW来编译调试C/C++程序》、《 Visual Studio 2022 CMake+MinGW+GDB 调试目标程序》,这两篇博文都是介绍的是GCC+GDB的编译与调试,本文笔者介绍的则是Clang+LLDB的编译与调试。读完本文,读者可以在GCC、Clang与GDB、LLDB之间进行随意组合。
GCC与GDB的编译调试组合在Linux下是默认组合,像Linux内核以及Linux的绝大多数软件都是使用GCC来编译,使用GDB来调试的。Clang+LLDB是作为新秀存在的,在近年来的MacOS中却是作为默认的编译调试组合。Clang作为编译器的新秀,是有许多优点的,比如Clang的编译速度比GCC快许多,编译出的错误提示非常友好,模块化比GCC做得好等等;LLDB调试器也有许多优点,比如下面一段代码,引用笔者前面的博文《 对C++变长参数中的字符串进行转义》
1template<typename ... Args>
2string FormatSQL(const char* fmt, Args&& ... args)
3{
4 vector<char*> vct;
5 size_t len = strlen(fmt);
6 string str;
7 Concat(str, len, fmt, EscapeArg(len, vct, args)...);
8 for (auto iter : vct)
9 delete[] iter;
10 return str;
11}
在调试到Concat(str, len, fmt, EscapeArg(len, vct, args)...);
时,默认情况下,使用GDB进入EscapeArg
函数执行到最后一句时,在VS中按F10
执行逐过程
,就会跳过Concat
函数,即不会进入Concat
函数,需要按F11
执行逐语句
,才会进入Concat
函数,这个行为与VC的调试器
不一致,而LLDB
就与VC的调试器
的行为一致,按F10
执行逐过程
可以进入Concat
函数。GDB的这种行为不知道是否可以配置,有知道的读者可以在评论区留言或者讨论。
一、在MinGW中安装Clang和LLDB
MinGW的安装这里就不再赘述了,直接去 MSYS2官网就可以下载最新的MinGW安装包安装就可以了。
安装好MinGW后,在MinGW终端使用下面的命令安装Clang和LLDB:
1pacman -S mingw-w64-x86_64-clang
2pacman -S mingw-w64-x86_64-clang-tools-extra
3pacman -S mingw-w64-x86_64-clang-analyzer
4pacman -S mingw-w64-x86_64-lldb
5pacman -S mingw-w64-x86_64-lldb-mi
二、Visual Studio 2022配置
Visual Studio 2022有一系列的CMake模板配置,有本机VC的,有WSL的、有MinGW的,有ARM的,也有Linux远程的:
这些模板配置位于Visual Studio 2022的安装目录C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\CMake\CMakeSettingsTemplates
,该目录下有许多json文件:
MinGW的相关配置就在MinGW.json
中,内容如下:
1{
2 "configurations": [
3 {
4 // Test Comment
5 "environments": [
6 {
7 "MINGW64_ROOT": "C:/msys64/mingw64",
8 "BIN_ROOT": "${env.MINGW64_ROOT}/bin",
9 "FLAVOR": "x86_64-w64-mingw32",
10 "TOOLSET_VERSION": "9.1.0",
11 "PATH": "${env.MINGW64_ROOT}/bin;${env.MINGW64_ROOT}/../usr/local/bin;${env.MINGW64_ROOT}/../usr/bin;${env.MINGW64_ROOT}/../bin;${env.PATH}",
12 "INCLUDE": "${env.INCLUDE};${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION};${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/tr1;${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/${env.FLAVOR}",
13 "environment": "mingw_64"
14 }
15 ],
16 "name": "Mingw64-Debug",
17 "description": "TemplateDescription_Localize_Mingw64Debug",
18 "generator": "Ninja",
19 "configurationType": "Debug",
20 "inheritEnvironments": [
21 "mingw_64"
22 ],
23 "buildRoot": "${projectDir}\\out\\build\\${name}",
24 "installRoot": "${projectDir}\\out\\install\\${name}",
25 "cmakeCommandArgs": "",
26 "buildCommandArgs": "-v",
27 "ctestCommandArgs": "",
28 "intelliSenseMode": "linux-gcc-x64",
29 "variables": [
30 {
31 "name": "CMAKE_C_COMPILER",
32 "value": "${env.BIN_ROOT}/gcc.exe"
33 },
34 {
35 "name": "CMAKE_CXX_COMPILER",
36 "value": "${env.BIN_ROOT}/g++.exe"
37 }
38 ]
39 },
40 {
41 "environments": [
42 {
43 "MINGW64_ROOT": "C:/msys64/mingw64",
44 "BIN_ROOT": "${env.MINGW64_ROOT}/bin",
45 "FLAVOR": "x86_64-w64-mingw32",
46 "TOOLSET_VERSION": "9.1.0",
47 "PATH": "${env.MINGW64_ROOT}/bin;${env.MINGW64_ROOT}/../usr/local/bin;${env.MINGW64_ROOT}/../usr/bin;${env.MINGW64_ROOT}/../bin;${env.PATH}",
48 "INCLUDE": "${env.INCLUDE};${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION};${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/tr1;${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/${env.FLAVOR}",
49 "environment": "mingw_64"
50 }
51 ],
52 "name": "Mingw64-Release",
53 "description": "TemplateDescription_Localize_Mingw64Release",
54 "generator": "Ninja",
55 "configurationType": "RelWithDebInfo",
56 "inheritEnvironments": [
57 "mingw_64"
58 ],
59 "buildRoot": "${projectDir}\\out\\build\\${name}",
60 "installRoot": "${projectDir}\\out\\install\\${name}",
61 "cmakeCommandArgs": "",
62 "buildCommandArgs": "-v",
63 "ctestCommandArgs": "",
64 "intelliSenseMode": "linux-gcc-x64",
65 "variables": [
66 {
67 "name": "CMAKE_C_COMPILER",
68 "value": "${env.BIN_ROOT}/gcc.exe"
69 },
70 {
71 "name": "CMAKE_CXX_COMPILER",
72 "value": "${env.BIN_ROOT}/g++.exe"
73 }
74 ]
75 }
76 ]
77}
(一)、GCC配置
1.配置MinGW根目录和TOOLSET_VERSION
可以看到MinGW中分别配置了Mingw64-Debug
和Mingw64-Release
,里面配置了MinGW的环境变量,使用的编译器,编译目录、安装目录等等。MinGW的安装目录默认为C:/msys64/mingw64
,可以根据自己的实际情况更改安装目录以及TOOLSET_VERSION
,比如笔者的GCC目前已经更新到13.1.0
了,所以需要把TOOLSET_VERSION
修改为13.1.0
,否则可能会在VS中无法打开头文件。
2.配置调试格式
GCC与Clang在编译时添加调试信息的参数默认都是-g
,但是各自生成的调试格式是不一定相同的。
GCC的
官方文档解释-g
:
-g Produce debugging information in the operating system’s native format (stabs, COFF, XCOFF, or DWARF). GDB can work with this debugging information. On most systems that use stabs format, -g enables use of extra debugging information that only GDB can use; this extra information makes debugging work better in GDB but probably makes other debuggers crash or refuse to read the program. If you want to control for certain whether to generate the extra information, use -gvms (see below).
即使用的操作系统本地的调试格式,可能是stabs
或者COFF
或者XCOFF
或者 DWARF
,GDB是可以识别这些调试格式的。
Clang的 官方文档:
-g Generate complete debug info.
Controlling Debugger “Tuning” While Clang generally emits standard DWARF debug info ( http://dwarfstd.org), different debuggers may know how to take advantage of different specific DWARF features. You can “tune” the debug info for one of several different debuggers. -ggdb, -glldb, -gsce, -gdbx Tune the debug info for the gdb, lldb, Sony PlayStation® debugger, or dbx, respectively. Each of these options implies -g. (Therefore, if you want both -gline-tables-only and debugger tuning, the tuning option must come first.)
即默认是使用标准的DWARF
格式,GDB是可以识别的,所以GCC与Clang使用参数-g
生成的程序,都可以使用GDB来调试。
而LLDB
官方文档中提到的功能说明:
Debug symbol file parsers to incrementally extract debug information from object files. Support currently includes DWARF & Mach-O symbol tables.
即目前LLDB只支持DWARF
和Mach-O
符号表,那MinGW中就只能是DWARF
了。
DWARF
格式目前已经发展到第5个版本了,LLDB目前只支持到第4个版本。
所以为了让GDB与LLDB都能进行调试,在使用GCC编译时需要指定调试格式为DWARF
,即需要使用参数-gdwarf-4
。
前面的json文件中variables
部分添加CMAKE_C_FLAGS_DEBUG
和CMAKE_CXX_FLAGS_DEBUG
变量,如下配置:
1"variables": [
2 {
3 "name": "CMAKE_C_COMPILER",
4 "value": "${env.BIN_ROOT}/gcc.exe"
5 },
6 {
7 "name": "CMAKE_CXX_COMPILER",
8 "value": "${env.BIN_ROOT}/g++.exe"
9 },
10 {
11 "name": "CMAKE_C_FLAGS_DEBUG",
12 "value": "-gdwarf-4",
13 "type": "STRING"
14 },
15 {
16 "name": "CMAKE_CXX_FLAGS_DEBUG",
17 "value": "-gdwarf-4",
18 "type": "STRING"
19 }
20 ]
(二)、Clang配置
Clang编译器可以使用GCC的库libstdc++
,也可以使用Clang自己的C++库libc++
(参见笔者之前的博文《
CentOS 6.X安装GCC 9.1和LLVM/Clang 8.0》中的CLANG_DEFAULT_CXX_STDLIB
说明),Clang的libc++
库头文件是在v1
目录中,所以配置时需要让Clang可以查找到GCC的头文件以及Clang的头文件目录。TOOLSET_VERSION
还是填写MinGW中当前安装的GCC的版本号,然后在environments
的INCLUDE
中添加${env.MINGW64_ROOT}/include/c++/v1
。variables
设置编译器为clang.exe
以及clang++.exe
:
1"variables": [
2 {
3 "name": "CMAKE_C_COMPILER",
4 "value": "${env.BIN_ROOT}/clang.exe"
5 },
6 {
7 "name": "CMAKE_CXX_COMPILER",
8 "value": "${env.BIN_ROOT}/clang++.exe"
9 }
10 ]
由于Clang在MinGW默认就是使用的dwarf
格式,所以可以不用指定,也可以指定。为了不与原来的文件混用,我们在原Mingw.json
的基础上复制一个MingwClang.json
文件来修改,并保存在相同的目录,下面列出完整配置:
1{
2 "configurations": [
3 {
4 // Test Comment
5 "environments": [
6 {
7 "MINGW64_ROOT": "G:/msys64/mingw64",
8 "BIN_ROOT": "${env.MINGW64_ROOT}/bin",
9 "FLAVOR": "x86_64-w64-mingw32",
10 "TOOLSET_VERSION": "13.1.0",
11 "PATH": "${env.MINGW64_ROOT}/bin;${env.MINGW64_ROOT}/../usr/local/bin;${env.MINGW64_ROOT}/../usr/bin;${env.MINGW64_ROOT}/../bin;${env.PATH}",
12 "INCLUDE": "${env.INCLUDE};${env.MINGW64_ROOT}/include/c++/v1;${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION};${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/tr1;${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/${env.FLAVOR}",
13 "environment": "mingw_64"
14 }
15 ],
16 "name": "Mingw64-Clang-Debug",
17 "description": "TemplateDescription_Localize_Mingw64Debug",
18 "generator": "Ninja",
19 "configurationType": "Debug",
20 "inheritEnvironments": [
21 "mingw_64"
22 ],
23 "buildRoot": "${projectDir}\\out\\build\\${name}",
24 "installRoot": "${projectDir}\\out\\install\\${name}",
25 "cmakeCommandArgs": "",
26 "buildCommandArgs": "-v",
27 "ctestCommandArgs": "",
28 "intelliSenseMode": "windows-clang-x64",
29 "variables": [
30 {
31 "name": "CMAKE_C_COMPILER",
32 "value": "${env.BIN_ROOT}/clang.exe"
33 },
34 {
35 "name": "CMAKE_CXX_COMPILER",
36 "value": "${env.BIN_ROOT}/clang++.exe"
37 },
38 {
39 "name": "CMAKE_C_FLAGS_DEBUG",
40 "value": "-gdwarf-4",
41 "type": "STRING"
42 },
43 {
44 "name": "CMAKE_CXX_FLAGS_DEBUG",
45 "value": "-gdwarf-4",
46 "type": "STRING"
47 }
48 ]
49 },
50 {
51 "environments": [
52 {
53 "MINGW64_ROOT": "G:/msys64/mingw64",
54 "BIN_ROOT": "${env.MINGW64_ROOT}/bin",
55 "FLAVOR": "x86_64-w64-mingw32",
56 "TOOLSET_VERSION": "13.1.0",
57 "PATH": "${env.MINGW64_ROOT}/bin;${env.MINGW64_ROOT}/../usr/local/bin;${env.MINGW64_ROOT}/../usr/bin;${env.MINGW64_ROOT}/../bin;${env.PATH}",
58 "INCLUDE": "${env.INCLUDE};${env.MINGW64_ROOT}/include/c++/v1;${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION};${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/tr1;${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/${env.FLAVOR}",
59 "environment": "mingw_64"
60 }
61 ],
62 "name": "Mingw64-Clang-Release",
63 "description": "TemplateDescription_Localize_Mingw64Release",
64 "generator": "Ninja",
65 "configurationType": "RelWithDebInfo",
66 "inheritEnvironments": [
67 "mingw_64"
68 ],
69 "buildRoot": "${projectDir}\\out\\build\\${name}",
70 "installRoot": "${projectDir}\\out\\install\\${name}",
71 "cmakeCommandArgs": "",
72 "buildCommandArgs": "-v",
73 "ctestCommandArgs": "",
74 "intelliSenseMode": "windows-clang-x64",
75 "variables": [
76 {
77 "name": "CMAKE_C_COMPILER",
78 "value": "${env.BIN_ROOT}/clang.exe"
79 },
80 {
81 "name": "CMAKE_CXX_COMPILER",
82 "value": "${env.BIN_ROOT}/clang++.exe"
83 }
84 ]
85 }
86 ]
87}
配置好后可以在CMakeSettings对话框中看到:
如果要使用Clang的libc++
库,CMakeLists.txt
中添加下面两句:
1add_compile_options("-stdlib=libc++")
2link_libraries(c++)
(三)、GDB调试配置
下面直接列出一个简单的配置:
1{
2 "type": "cppdbg",
3 "name": "CMakeLists.gdb",
4 "project": "CMakeLists.txt",
5 "projectTarget": "t.exe",
6 "cwd": "${workspaceRoot}",
7 "program": "${debugInfo.target}",
8 "MIMode": "gdb",
9 "miDebuggerPath": "${env.MINGW_PREFIX}\\bin\\gdb.exe",
10 "externalConsole": true,
11 "setupCommands": [
12 {
13 "description": "为 gdb 启用整齐打印",
14 "text": "-enable-pretty-printing",
15 "ignoreFailures": true
16 },
17 {
18 "description": "将反汇编风格设置为 Intel",
19 "text": "set disassembly-flavor intel",
20 "ignoreFailures": true
21 }
22 ]
23 }
可以参见:《 Visual Studio 2022使用MinGW来编译调试C/C++程序》、《 Visual Studio 2022 CMake+MinGW+GDB 调试目标程序》
如果每次都添加setupCommands
命令,也比较烦,我们可以在USERPROFILE
环境变量所指目录中添加一个.gdbinit
文件,将上面的命令放在里面,每次GDB启动时都会去执行里面的命令:
1set disassembly-flavor intel
(四)、LLDB配置
LLDB配置与GDB配置类似:
1{
2 "type": "cppdbg",
3 "name": "CMakeLists.lldb",
4 "project": "CMakeLists.txt",
5 "projectTarget": "t.exe",
6 "cwd": "${workspaceRoot}",
7 "program": "${debugInfo.target}",
8 "MIMode": "lldb",
9 "miDebuggerPath": "${env.MINGW_PREFIX}\\bin\\lldb-mi.exe",
10 "externalConsole": true,
11 "setupCommands": [
12 {
13 "description": "为 gdb 启用整齐打印",
14 "text": "-enable-pretty-printing",
15 "ignoreFailures": true
16 },
17 {
18 "description": "将反汇编风格设置为 Intel",
19 "text": "setting set target.x86-disassembly-flavor intel",
20 "ignoreFailures": true
21 }
22 ]
23 }
只需要把MIMode
改为lldb
,miDebuggerPath
设置为lldb-mi.exe
,如果反汇编格式使用Intel格式的命令为setting set target.x86-disassembly-flavor intel
。同样地,为了方便省事,可以在USERPROFILE
环境变量所指目录中添加一个.lldbinit
文件,将命令放在里面,每次LLDB启动时都会去执行里面的命令:
1setting set target.x86-disassembly-flavor intel
Btw:目前Visual Studio 2022使用LLDB调试器对源码反汇编还有Bug,期待新版本能快速修复。VSCode目前也可以使用LLDB进行调试,但是反汇编也是有Bug的。
若对你有所帮助,欢迎点赞收藏!
- 原文作者:Witton
- 原文链接:https://wittonbell.github.io/posts/2023/2023-05-26-Visual-Studio-2022使用CMake+MinGW+Clang+LLDB作为开发环境/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. 进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。