Visual Studio 2022使用CMake+MinGW+Clang+LLDB作为开发环境

这篇具有很好参考价值的文章主要介绍了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++变长参数中的字符串进行转义》

template<typename ... Args>
string FormatSQL(const char* fmt, Args&& ... args)
{
	vector<char*> vct;
	size_t len = strlen(fmt);
	string str;
	Concat(str, len, fmt, EscapeArg(len, vct, args)...);
	for (auto iter : vct)
		delete[] iter;
	return str;
}

在调试到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:

pacman -S mingw-w64-x86_64-clang
pacman -S mingw-w64-x86_64-clang-tools-extra
pacman -S mingw-w64-x86_64-clang-analyzer
pacman -S mingw-w64-x86_64-lldb
pacman -S mingw-w64-x86_64-lldb-mi

二、Visual Studio 2022配置

Visual Studio 2022有一系列的CMake模板配置,有本机VC的,有WSL的、有MinGW的,有ARM的,也有Linux远程的:

Visual Studio 2022使用CMake+MinGW+Clang+LLDB作为开发环境

这些模板配置位于Visual Studio 2022的安装目录C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\CMake\CMakeSettingsTemplates,该目录下有许多json文件:

Visual Studio 2022使用CMake+MinGW+Clang+LLDB作为开发环境

MinGW的相关配置就在MinGW.json中,内容如下:

{
  "configurations": [
    {
      // Test Comment
      "environments": [
        {
          "MINGW64_ROOT": "C:/msys64/mingw64",
          "BIN_ROOT": "${env.MINGW64_ROOT}/bin",
          "FLAVOR": "x86_64-w64-mingw32",
          "TOOLSET_VERSION": "9.1.0",
          "PATH": "${env.MINGW64_ROOT}/bin;${env.MINGW64_ROOT}/../usr/local/bin;${env.MINGW64_ROOT}/../usr/bin;${env.MINGW64_ROOT}/../bin;${env.PATH}",
          "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}",
          "environment": "mingw_64"
        }
      ],
      "name": "Mingw64-Debug",
      "description": "TemplateDescription_Localize_Mingw64Debug",
      "generator": "Ninja",
      "configurationType": "Debug",
      "inheritEnvironments": [
        "mingw_64"
      ],
      "buildRoot": "${projectDir}\\out\\build\\${name}",
      "installRoot": "${projectDir}\\out\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": "",
      "intelliSenseMode": "linux-gcc-x64",
      "variables": [
        {
          "name": "CMAKE_C_COMPILER",
          "value": "${env.BIN_ROOT}/gcc.exe"
        },
        {
          "name": "CMAKE_CXX_COMPILER",
          "value": "${env.BIN_ROOT}/g++.exe"
        }
      ]
    },
    {
      "environments": [
        {
          "MINGW64_ROOT": "C:/msys64/mingw64",
          "BIN_ROOT": "${env.MINGW64_ROOT}/bin",
          "FLAVOR": "x86_64-w64-mingw32",
          "TOOLSET_VERSION": "9.1.0",
          "PATH": "${env.MINGW64_ROOT}/bin;${env.MINGW64_ROOT}/../usr/local/bin;${env.MINGW64_ROOT}/../usr/bin;${env.MINGW64_ROOT}/../bin;${env.PATH}",
          "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}",
          "environment": "mingw_64"
        }
      ],
      "name": "Mingw64-Release",
      "description": "TemplateDescription_Localize_Mingw64Release",
      "generator": "Ninja",
      "configurationType": "RelWithDebInfo",
      "inheritEnvironments": [
        "mingw_64"
      ],
      "buildRoot": "${projectDir}\\out\\build\\${name}",
      "installRoot": "${projectDir}\\out\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": "",
      "intelliSenseMode": "linux-gcc-x64",
      "variables": [
        {
          "name": "CMAKE_C_COMPILER",
          "value": "${env.BIN_ROOT}/gcc.exe"
        },
        {
          "name": "CMAKE_CXX_COMPILER",
          "value": "${env.BIN_ROOT}/g++.exe"
        }
      ]
    }
  ]
}

(一)、GCC配置

1.配置MinGW根目录和TOOLSET_VERSION

可以看到MinGW中分别配置了Mingw64-DebugMingw64-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只支持DWARFMach-O符号表,那MinGW中就只能是DWARF了。DWARF格式目前已经发展到第5个版本了,LLDB目前只支持到第4个版本。

所以为了让GDB与LLDB都能进行调试,在使用GCC编译时需要指定调试格式为DWARF,即需要使用参数-gdwarf-4

前面的json文件中variables部分添加CMAKE_C_FLAGS_DEBUGCMAKE_CXX_FLAGS_DEBUG变量,如下配置:

"variables": [
        {
          "name": "CMAKE_C_COMPILER",
          "value": "${env.BIN_ROOT}/gcc.exe"
        },
        {
          "name": "CMAKE_CXX_COMPILER",
          "value": "${env.BIN_ROOT}/g++.exe"
        },
		{
          "name": "CMAKE_C_FLAGS_DEBUG",
          "value": "-gdwarf-4",
          "type": "STRING"
        },
		{
          "name": "CMAKE_CXX_FLAGS_DEBUG",
          "value": "-gdwarf-4",
          "type": "STRING"
        }
      ]

(二)、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的版本号,然后在environmentsINCLUDE中添加${env.MINGW64_ROOT}/include/c++/v1variables设置编译器为clang.exe以及clang++.exe

"variables": [
        {
          "name": "CMAKE_C_COMPILER",
          "value": "${env.BIN_ROOT}/clang.exe"
        },
        {
          "name": "CMAKE_CXX_COMPILER",
          "value": "${env.BIN_ROOT}/clang++.exe"
        }
      ]

由于Clang在MinGW默认就是使用的dwarf格式,所以可以不用指定,也可以指定。为了不与原来的文件混用,我们在原Mingw.json的基础上复制一个MingwClang.json文件来修改,并保存在相同的目录,下面列出完整配置:

{
  "configurations": [
    {
      // Test Comment
      "environments": [
        {
          "MINGW64_ROOT": "G:/msys64/mingw64",
          "BIN_ROOT": "${env.MINGW64_ROOT}/bin",
          "FLAVOR": "x86_64-w64-mingw32",
          "TOOLSET_VERSION": "13.1.0",
          "PATH": "${env.MINGW64_ROOT}/bin;${env.MINGW64_ROOT}/../usr/local/bin;${env.MINGW64_ROOT}/../usr/bin;${env.MINGW64_ROOT}/../bin;${env.PATH}",
          "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}",
          "environment": "mingw_64"
        }
      ],
      "name": "Mingw64-Clang-Debug",
      "description": "TemplateDescription_Localize_Mingw64Debug",
      "generator": "Ninja",
      "configurationType": "Debug",
      "inheritEnvironments": [
        "mingw_64"
      ],
      "buildRoot": "${projectDir}\\out\\build\\${name}",
      "installRoot": "${projectDir}\\out\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": "",
      "intelliSenseMode": "windows-clang-x64",
      "variables": [
        {
          "name": "CMAKE_C_COMPILER",
          "value": "${env.BIN_ROOT}/clang.exe"
        },
        {
          "name": "CMAKE_CXX_COMPILER",
          "value": "${env.BIN_ROOT}/clang++.exe"
        },
        {
          "name": "CMAKE_C_FLAGS_DEBUG",
          "value": "-gdwarf-4",
          "type": "STRING"
        },
        {
          "name": "CMAKE_CXX_FLAGS_DEBUG",
          "value": "-gdwarf-4",
          "type": "STRING"
        }
      ]
    },
    {
      "environments": [
        {
          "MINGW64_ROOT": "G:/msys64/mingw64",
          "BIN_ROOT": "${env.MINGW64_ROOT}/bin",
          "FLAVOR": "x86_64-w64-mingw32",
          "TOOLSET_VERSION": "13.1.0",
          "PATH": "${env.MINGW64_ROOT}/bin;${env.MINGW64_ROOT}/../usr/local/bin;${env.MINGW64_ROOT}/../usr/bin;${env.MINGW64_ROOT}/../bin;${env.PATH}",
          "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}",
          "environment": "mingw_64"
        }
      ],
      "name": "Mingw64-Clang-Release",
      "description": "TemplateDescription_Localize_Mingw64Release",
      "generator": "Ninja",
      "configurationType": "RelWithDebInfo",
      "inheritEnvironments": [
        "mingw_64"
      ],
      "buildRoot": "${projectDir}\\out\\build\\${name}",
      "installRoot": "${projectDir}\\out\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "-v",
      "ctestCommandArgs": "",
      "intelliSenseMode": "windows-clang-x64",
      "variables": [
        {
          "name": "CMAKE_C_COMPILER",
          "value": "${env.BIN_ROOT}/clang.exe"
        },
        {
          "name": "CMAKE_CXX_COMPILER",
          "value": "${env.BIN_ROOT}/clang++.exe"
        }
      ]
    }
  ]
}

配置好后可以在CMakeSettings对话框中看到:
Visual Studio 2022使用CMake+MinGW+Clang+LLDB作为开发环境

如果要使用Clang的libc++库,CMakeLists.txt中添加下面两句:

add_compile_options("-stdlib=libc++")
link_libraries(c++)

(三)、GDB调试配置

下面直接列出一个简单的配置:

{
      "type": "cppdbg",
      "name": "CMakeLists.gdb",
      "project": "CMakeLists.txt",
      "projectTarget": "t.exe",
      "cwd": "${workspaceRoot}",
      "program": "${debugInfo.target}",
      "MIMode": "gdb",
      "miDebuggerPath": "${env.MINGW_PREFIX}\\bin\\gdb.exe",
      "externalConsole": true,
      "setupCommands": [
        {
          "description": "为 gdb 启用整齐打印",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        },
        {
          "description": "将反汇编风格设置为 Intel",
          "text": "set disassembly-flavor intel",
          "ignoreFailures": true
        }
      ]
    }

可以参见:《Visual Studio 2022使用MinGW来编译调试C/C++程序》、《Visual Studio 2022 CMake+MinGW+GDB 调试目标程序》

如果每次都添加setupCommands命令,也比较烦,我们可以在USERPROFILE环境变量所指目录中添加一个.gdbinit文件,将上面的命令放在里面,每次GDB启动时都会去执行里面的命令:

set disassembly-flavor intel

(四)、LLDB配置

LLDB配置与GDB配置类似:

{
      "type": "cppdbg",
      "name": "CMakeLists.lldb",
      "project": "CMakeLists.txt",
      "projectTarget": "t.exe",
      "cwd": "${workspaceRoot}",
      "program": "${debugInfo.target}",
      "MIMode": "lldb",
      "miDebuggerPath": "${env.MINGW_PREFIX}\\bin\\lldb-mi.exe",
      "externalConsole": true,
      "setupCommands": [
        {
          "description": "为 gdb 启用整齐打印",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        },
        {
          "description": "将反汇编风格设置为 Intel",
          "text": "setting set target.x86-disassembly-flavor intel",
          "ignoreFailures": true
        }
      ]
    }

只需要把MIMode改为lldbmiDebuggerPath设置为lldb-mi.exe,如果反汇编格式使用Intel格式的命令为setting set target.x86-disassembly-flavor intel。同样地,为了方便省事,可以在USERPROFILE环境变量所指目录中添加一个.lldbinit文件,将命令放在里面,每次LLDB启动时都会去执行里面的命令:

setting set target.x86-disassembly-flavor intel

Btw:目前Visual Studio 2022使用LLDB调试器对源码反汇编还有Bug,期待新版本能快速修复。VSCode目前也可以使用LLDB进行调试,但是反汇编也是有Bug的。

若对你有所帮助,欢迎点赞收藏!文章来源地址https://www.toymoban.com/news/detail-463008.html

到了这里,关于Visual Studio 2022使用CMake+MinGW+Clang+LLDB作为开发环境的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Window中,Visual Studio 2022(C++)环境下安装OpenCV教程(不用Cmake版本)

    本教程主要为了方便小白安装C++版本的OpenCV。 1. 第一步:下载官方OpenCV 下载后,在本地安装即可,注意记住安装路径,后续需要! 2. 配置系统环境变量,Path中,新增变量。即opencv安装的路径,选到opencv中build/x64/vc15/bin 3. 安装visual studio 2022,官网 直接,按照C++配置安装即可

    2024年02月11日
    浏览(24)
  • 使用 Visual Studio 2022 开发 Linux C++ 应用程序

    前置条件: Windows上需要先安装 WSL2,方法见: Install WSL | Microsoft Docs 在 WSL2 中依次执行如下命令,进行安装如下必需软件: Visual Studio 2022 引入了用于 Linux C++ 开发的本机 WSL2 工具集,可以构建和调试 Linux C++ 代码,并提供了非常好的 Linux 文件系统性能、GUI 支持和完整的系统

    2024年02月05日
    浏览(75)
  • VS Code 使用 clang++ 编译,使用 cppvsdbg 或 lldb 调试的配置方法

    VS Code LLVM C/C++(用来配置 c_cpp_properties.json) CodeLLDB(如果你要用 lldb 调试,那么这个插件就需要安装,用来连接到 lldb 调试器) 我们都知道配置编译器要设置三个 json,task, launch, c_cpp_properties.json task.json 直接通过 terminal - configure default build task - C/C++: clang++.exe build active file

    2024年02月09日
    浏览(20)
  • win下使用MinGW-w64+cmake搭建c++开发环境

    关于MinGW与MSVC MSVC: 即Microsoft Visual C++ Compiler,即微软自己的编译器 我们下载Windows下的OpenCV时,会带两个文件夹VC14,VC15(分别与Visual Studio的版本有对应关系),这两个文件夹下的库可以直接运行不需要编译 将VS作为Qt的开发环境也是使用这个编译器的缘故 MinGW: 我们都知道

    2024年02月11日
    浏览(16)
  • 解决Unity游戏开发使用Visual Studio Enterprise 2022提示未找到目标框架.net framework4.7.1问题

    采用Visual Studio Installer安装Visual Studio Enterprise 2022,安装中勾选游戏下Unity游戏开发。 单个组件中确保勾选.NET Framework 4.7.1目标包 安装位置自定义修改至F盘后使用Visual Studio Enterprise 2022登录,在Unity设置使用Visual Studio Enterprise 2022并打开对应项目文件,提示未找到目标框架.net

    2024年02月10日
    浏览(23)
  • 使用 Visual Studio Code 调试 CMake 脚本

    之前被引入到 Visual Studio 中的 CMake 调试器,现已在 Visual Studio Code 中可用。 也就是说,现在你可以通过在 VS Code 中安装 CMake 工具扩展,来调试你的 CMakeLists.txt 脚本了。是不是很棒? Visual C++ 开发团队和 CMake 的维护者 Kitware 一直密切合作,目的是将我们的 CMake 调试器实现集成

    2024年02月13日
    浏览(18)
  • Visual Studio 2022 搭建GLFW OpenGL开发环境

    最近工作需要 需要写一个全景的视频播放器 网上搜了下大概解决方案是 ffmpeg+opengl b站有很多视频  按照视频 搭建了OpenGL的开发环境 先去GLFW的网站下载 windows平台的库文件 为什么使用GLFW  因为GLFW是跨平台的   我下的是64位版本解压后有目录如下    包含了动态库和静态库

    2024年02月03日
    浏览(23)
  • FBX SDK 开发环境配置 visual studio 2022

    FBX | Adaptable File Formats for 3D Animation Software | Autodesk. 下载windows的sdk并安装. 创建一个c++ console 工程 设置include目录 添加预处理宏 FBX_SHARED=1 添加fbx sdk lib 目录 添加依赖lib :  libfbxsdk-md.lib libxml2-md.lib zlib-md.lib 配置完毕.

    2024年02月10日
    浏览(17)
  • Visual Studio 2022 正式支持 .NET MAUI 开发

    我们很高兴地宣布 Visual Studio 2022 正式支持 .NET MAUI 开发。现在,您可以使用 .NET 更快地构建跨平台原生客户端应用程序,并将它们从单个代码库发布到 Android、iOS、macOS 和 Windows。 此版本还提供了 .NET MAUI SDK 的最新稳定性改进,这是自 2022 年 5 月 正式发布(GA)发布以来的第

    2024年02月11日
    浏览(17)
  • Windows搭建C++开发环境(visual studio 2022)

    开发环境的搭建 开发工具:vscode、visual studio 2022、visual studio 2019、2015、2010 .. 安装步骤(以Windows下visual studio2022为例): 打开官网地址  Visual Studio 2022 IDE - 适用于软件开发人员的编程工具 (microsoft.com) 下载社区版2022   下载完成后打开 步骤如下 安装完成后会自动打开出现

    2024年02月06日
    浏览(23)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包