欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

开发工具: vscode + lldb + clang + cmake c++ 开发环境配置

最编程 2024-07-18 19:05:30
...

文章目录

  • 准备工作
  • 编译配置--Clang
    • 相关链接
  • 调试配置--LLDB
    • 断点调试
    • 相关链接
  • 指定不同操作系统下的配置属性
  • 指定全局配置属性
  • 额外的
  • 项目编译--CMake
    • 相关链接


准备工作

需要安装的可执行文件:llvm,clang,clangd,lldb,cmake;

这几个文件的关系可以简单理解为,llvm 作为基本的编译框架,clang,clangd,lldb 为基于该框架发展的扩展功能,其中 clang 用于编译源代码;lldb 用于 debug 编译文件,两者的功能类似 gcc 和 gdb;额外的 clangd 则提供了十分强大的代码检测功能;

编译配置–Clang

选择 终端–>运行生成任务,此时如果项目中没有配置文件,vscode 会自动创建 task.json 文件(文件位于项目文件夹下的 .vscode 文件夹内),task.json 用于将某些脚本和启动进程配置为 vscode 的 task 选项,从而无须重复在命令行中输入某些指令或执行某些代码。
在这里插入图片描述

默认的 task.json 文件如下:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "msbuild",
            "args": [
                // Ask msbuild to generate full paths for file names.
                "/property:GenerateFullPaths=true",
                "/t:build",
                // Do not generate summary otherwise it leads to duplicate errors in Problems panel
                "/consoleloggerparameters:NoSummary"
            ],
            "group": "build",
            "presentation": {
                // Reveal the output only if unrecognized errors occur.
                "reveal": "silent"
            },
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": "$msCompile"
        }
    ]
}

task.json 文件的属性说明:

  • label: 该任务的标签,可与任意指定;

  • type: “shell” 将任务指定为 shell 指令;“process” 将任务指定为要执行的进程;

  • command: 要执行的命令,如 “clang++”;

  • group: 指定该任务所属的组;

  • presentation: 指定任务的输出行为;

  • runOptions: 指定任务的运行时行为;

相关链接

  • Integrate with External Tools via Tasks
  • Schema for tasks.json

调试配置–LLDB

安装 CodeLLDB 插件,CodeLLDB 会自动下载 platform-specific native binaries,且无需对该插件进行额外的配置,只需要配置 vscode 调试相关的 launch.json 文件。

选择 运行–>启动调试,此时如果项目中没有配置文件,vscode 会自动创建 launch.json 文件(文件位于项目文件夹下的 .vscode 文件夹内),该文件用于告诉 vscode 如何调试项目文件,如指定调试器,调试文件,调试指令,以及调试前或调试后的动作等。

创建 launch.json 文件

默认创建的 launch.json 如下:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "${workspaceFolder}/<your program>",
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

launch.json 文件的属性说明:不同调试器的配置属性不同,以下为 codelldb 的 launch.json 属性。

参数 类型 说明 默认值
name string 调试操作的名称,可以任意设置;
type string 用于 debug 的调试器类型,设置为 “lldb”;
request string debug 的请求类型,设置为 “launch”;
program string 调试对象的可执行文件路径;
args string ❘ [string] 传递给程序进行调试的参数;
cwd string 工作目录; ${workspaceFolder}
env dictionary 环境变量;
stdio string ❘ [string] ❘ dictionary Stdio Configuration. “null”
terminal string 指定调试对象的输出目标,可选参数 “console”(default), “external” 和 “integrated” “console”
stopOnEntry boolean 是否在启动后立即停止调试对象;
initCommands [string] LLDB commands executed upon debugger startup.
preRunCommands [string] LLDB commands executed just before launching the debuggee.
postRunCommands [string] LLDB commands executed just after launching the debuggee.
exitCommands [string] LLDB commands executed at the end of the debugging session;
expressions string 表达式类型: “simple”,“python” 和 “nativ”,See Expressions “simple”
sourceMap dictionary See Source Path Remapping.
relativePathBase string 指定相对路径的基路径, “${workspaceFolder}”(default),
sourceLanguages [string] 程序中所使用的语言列表; [“cpp”,“rust”]

调试的执行顺序:

  1. 创建调试会话;
  2. 执行 initCommands;
  3. 根据配置参数(program,args,env,cwd,stdio)创建 debug target;
  4. 设置断点;
  5. 执行 preRunCommands;
  6. 启动调试对象;
  7. 执行 postRunCommands;
  8. 在调试结束时执行 exitCommands;

断点调试

  • 条件断点:有两种指定条件的方式(表达式条件,命中计数),当条件满足时,程序才会中断;
  • 函数断点:可以在断点视图下指定函数名来创建断点,而不是直接在源代码上放置断点;
  • 数据断点:可以在变量视图种设置数据断点,当指定变量的值更改时中断程序;
  • 内联断点:可以使用 Shift+F9 或在调试会话期间通过上下文菜单设置内联断点;
  • 日志点:它不会“中断”到调试器中,而是将消息记录到控制台。

相关链接

  • LLDB Homepage:LLDB;
  • Debugging in VS Code:vscode debug 配置;
  • CodeLLDB User’s Manual:CodeLLDB 使用手册;
  • Wiki pages - troubleshooting:CodeLLDB 常见问题的解决;
  • Discussions:CodeLLDB 话题讨论;

指定不同操作系统下的配置属性

只需在 launch.json/task.json 的 configurations 中指定操作系统属性,如 “windows”,“linux”,“osx”;在操作系统属性栏下配置的属性会替换全局配置下的属性。

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "program": "${workspaceFolder}/node_modules/gulp/bin/gulpfile.js",
      "args": ["myFolder/path/app.js"],
      "windows": {
        "args": ["myFolder\\path\\app.js"]
      }
    }
  ]
}

指定全局配置属性

对于 task.json:键入 “ctrl+shift+p” 进入任务面板,键入 “task:open user tasks” ,可以在打开的文件下创建未绑定到特定工作区或文件夹的用户级任务。
在这里插入图片描述

对于 lanuch.json:通过在 settings.json 文件下配置 “lanuch” 属性,可以在所有工作空间下应用该配置。

//settings.json 文件下
"launch": {
    "version": "0.2.0",
    "configurations": [{
        "type": "node",
        "request": "launch",
        "name": "Launch Program",
        "program": "${file}"
    }]
}

额外的

launch.json/tasks.json 文件的配置参数支持 vscode 预定义的变量,各变量的含义如下所示。

  • ${userHome} - 用户 home 文件夹路径;
  • ${workspaceFolder} - 在 vscode 作为工作空间的文件夹路径;
  • ${workspaceFolderBasename} - 工作空间的名称(就是工作空间的文件名);
  • ${file} - 当前打开的文件路径;
  • ${fileWorkspaceFolder} - 当前打开的文件所在工作空间的路径;
  • ${relativeFile} - 当前打开的文件相对于 workspaceFolder 的路径;
  • ${relativeFileDirname} - 当前打开的文件夹相对于 workspaceFolder 的路径;
  • ${fileBasename} - 当前打开文件的文件名;
  • ${fileBasenameNoExtension} - 当前打开文件的文件名,不带扩展名;
  • ${fileDirname} - 当前打开的文件所在文件夹路径;
  • ${fileExtname} - 当前打开的文件的扩展名;
  • ${cwd} - vscode 启动时任务运行程序的当前工作目录;
  • ${lineNumber} - 光标所在行号;
  • ${selectedText} - 活动文件中当前选定的文本;
  • ${execPath} - 正在运行的 vscode 可执行文件的路径;
  • ${defaultBuildTask} - 默认 build task 的名称;
  • ${pathSeparator} - 操作系统用于分隔文件路径的字符;

参考:Predefined variables

项目编译–CMake

安装 vscode 插件 CMake Tools;

创建一个 CMkake 项目:

  • 键入 “ctrl+shift+p” 进入任务面板,键入 “cmake:quick” ,创建一个基本的 CMake 项目;
  • 选择相应的编译套件,如 Clang,所列出的编译套件是 vscode 在本地搜索到的;
  • 选择相应的构建类型,如 Debug,Release;

随后,CMake Tools 会在工作区自动创建 main.cpp,CMakeLists.txt 文件,以及 build 文件夹。默认创建的 CMakeLists.txt 如下:

cmake_minimum_required(VERSION 3.0.0)
project(tsetCmake VERSION 0.1.0)

include(CTest)
enable_testing()

add_executable(tsetCmake main.cpp)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

可以从状态栏中重新设置 CMake 项目或进行编译:
在这里插入图片描述

  • 编号 1 处用于重新选的编译属性,如 Debug,Release 等;
  • 编号 2 处用于重新选定编译套件;
  • 编号 3 处用于编译项目文件;

注意:

  • 可以使用 CMake Tools 的设置参数 cmake.generator 来指定 generator,如,Ninja,Makefile,Visual Studio;
  • CMake Tools 的快速 debug(指令 “cmake: debug”, ctrl+F5) 不需要创建 launch.json,但是必须使用 vscode 插件 cpptool,并且该 debug 方式不支持指定任何调试参数。一般的,可以在 launch.json 文件中配置相关的 debug 参数,并指定相应 debug 工具进行 debug。

几个常用的指令

  • “cmake: quick”:构建 CMake 项目;
  • “cmake : clean”:清除 CMake 生成文件;
  • “cmake: run without debugging”:运行可执行文件但不 debug;
  • “cmake: show configure command”;
  • “cmake: show build command”;

相关链接

  • 开始在 Linux 上使用 CMake Tools (visualstudio.com)
  • vscode-cmake-tools/README.md at main · microsoft/vscode-cmake-tools (github.com)
  • vscode-cmake-tools/configure.md at main · microsoft/vscode-cmake-tools (github.com)