环境配置-LLVM

Windows

windows 环境采用的版本:LLVM 下载地址:https://github.com/mstorsjo/llvm-mingw/releases

Linux

Linux 环境采用的版本:LLVM 下载地址:https://github.com/llvm/llvm-project/releases

不管系统如何,使用当前最新版本即可。

代码编辑器 VSCode 配置:

项目使用 VSCode 打开后会产生 .vscode 目录,以下文件放在 .vscode 目录内。

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "LLVM",
            "includePath": [
                "${workspaceFolder}/**",
                "D:\\Dev\\LLVM\\llvm-mingw\\include**" // Windows 示例,修改为你的实际路径
                // "/opt/llvm19/include**" // Linux 示例,修改为你的实际路径
            ],
            "defines": [],
            "compilerPath": "D:\\Dev\\LLVM\\llvm-mingw\\bin\\clang++.exe", // Windows 示例,修改为你的实际路径
            // "compilerPath": "/opt/llvm19/bin/clang++.exe", // Linux 示例,修改为你的实际路径
            "cStandard": "c23",
            "cppStandard": "c++23",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug with LLDB",
            "type": "lldb",
            "request": "launch",
            "program": "${workspaceFolder}\\build\\windows\\x64\\debug\\he.exe", // 可执行文件路径,修改为你的实际生成路径
            "args": [], // 启动时传递给程序的参数
            "cwd": "${workspaceFolder}", // 工作目录
            "stopOnEntry": false, // 是否在入口处停下
            "preLaunchTask": "Debug Build", // 在启动调试前执行的任务(如编译)
            "terminal": "integrated", // 使用 VSCode 内置终端
            "env": {},
            "MIMode": "lldb",
            "miDebuggerPath": "D:\\Dev\\LLVM\\llvm-mingw\\bin\\lldb.exe", // Windows 示例,修改为你的实际路径
            // "miDebuggerPath": "/opt/llvm19/bin/lldb.exe", // Linux 示例,修改为你的实际路径
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

settings.json

{
    "clang-format.executable": "D:\\Dev\\LLVM\\llvm-mingw\\bin\\clang-format.exe", // Windows 示例,修改为你的实际路径
    // "clang-format.executable": "/opt/llvm19/bin/clang-format.exe", // Linux 示例,修改为你的实际路径
    "clang-format.style": "Google", // 设置为 Google 风格
    "editor.formatOnSave": true, // 保存时自动格式化
    "editor.defaultFormatter": "ms-vscode.cpptools", // 设置默认格式化工具
    "C_Cpp.intelliSenseEngine": "disabled", // 关闭默认的 IntelliSense 引擎
    "C_Cpp.autocomplete": "default", // 启用默认的自动补全
    // "C_Cpp.autocomplete": "disabled", // 关闭默认的自动补全
    "clangd.path": "D:\\Dev\\LLVM\\llvm-mingw\\bin\\clangd.exe", // Windows 示例,修改为你的实际路径
    // "clangd.path": "/opt/llvm19/bin/clangd.exe", // Linux 示例,修改为你的实际路径
    "clangd.arguments": [
        "--background-index", // 在后台生成索引
        "--clang-tidy", // 启用 clang-tidy 来分析代码
        "--completion-style=detailed", // 提供详细的补全信息
        "--header-insertion=iwyu", // 自动补全时插入必要的头文件
        "--cross-file-rename=true", // 支持跨文件重命名
        "-std=c++23",
        "--log=verbose"
    ],
    "editor.wordBasedSuggestions": "off", // 关闭词汇级别的建议(避免干扰)
    "editor.suggestSelection": "first", // 提示列表中优先选择第一个候选项
    "files.associations": {
        "*.h": "cpp", // 让 .h 文件也作为 C++ 文件进行处理
        "*.mpp": "cpp",
        "*.hpp": "cpp"
    },
    "[cpp]": {
        "editor.defaultFormatter": "xaver.clang-format",
        "editor.quickSuggestions": {
            "comments": "on",
            "strings": "on",
            "other": "on"
        },
        "editor.wordBasedSuggestions": "matchingDocuments" // C++ 语言的词汇补全
    },
    "[json]": {
        "editor.defaultFormatter": "vscode.json-language-features"
    },
    "[c]": {
        "editor.wordBasedSuggestions": "matchingDocuments", // C 语言的词汇补全
        "editor.quickSuggestions": {
            "comments": "on",
            "strings": "on",
            "other": "on"
        },
    },
    "[cuda-cpp]": {
        "editor.wordBasedSuggestions": "matchingDocuments" // CUDA-C 语言的词汇补全
    },
    "[jsonc]": {
        "editor.defaultFormatter": "vscode.json-language-features"
    }
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Debug Build", // Debug 编译
            "type": "shell",
            "command": "xmake f -m debug --toolchain=llvm --runtimes=c++_shared -cv ; xmake -rv", // Windows 示例(PowerShell)
            "command": "xmake f -m debug --toolchain=llvm --runtimes=c++_shared -cv && xmake -rv", // Linux 示例
            "options": {
                "cwd": "${workspaceFolder}", // 指定工作目录为 "build" 子目录
                "env": {
                    "PATH": "${env:PATH};D:\\Dev\\LLVM\\llvm-mingw\\bin;D:\\Dev\\xmake;" // Windows 示例,以你实际安装路径为准
                    // "PATH": "${env:PATH}:/opt/llvm19/bin:/opt/xmake/bin" // Linux 示例,以你实际安装路径为准
                }
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Release Build", // Release 编译
            "type": "shell",
            "command": "xmake f -m release --toolchain=llvm --runtimes=c++_shared -cv ; xmake -rv", // Windows 示例(PowerShell)
            "command": "xmake f -m release --toolchain=llvm --runtimes=c++_shared -cv && xmake -rv", // Linux 示例
            "options": {
                "cwd": "${workspaceFolder}", // 指定工作目录为 "build" 子目录
                "env": {
                    "PATH": "${env:PATH};D:\\Dev\\LLVM\\llvm-mingw\\bin;D:\\Dev\\xmake;" // Windows 示例,以你实际安装路径为准
                    // "PATH": "${env:PATH}:/opt/llvm19/bin:/opt/xmake/bin" // Linux 示例,以你实际安装路径为准
                }
            },
            "group": {
                "kind": "build",
                "isDefault": false
            }
        },
        {
            "label": "Run", // 运行程序,需要先 Release Build 或者 Debug Build
            "type": "shell",
            "command": "xmake run",
            "options": {
                "cwd": "${workspaceFolder}", // 指定工作目录为 "build" 子目录
                "env": {
                    "PATH": "${env:PATH};D:\\Dev\\LLVM\\llvm-mingw\\bin;D:\\Dev\\xmake;" // Windows 示例,以你实际安装路径为准
                    // "PATH": "${env:PATH}:/opt/llvm19/bin:/opt/xmake/bin" // Linux 示例,以你实际安装路径为准
                }
            },
            "group": {
                "kind": "build",
                "isDefault": false
            }
        }
    ]
}

配置安装后,测试了Linux、Windows 平台,编译、运行、调式 都没有问题,但代码提示!!!

C++23 代码提示

目前只有Windows环境可行,使用 xmake 将项目转换为 VisualStudio 项目,命令如下:

# 配置项目信息
xmake f --toolchain=llvm --runtimes=c++_shared -cv --vs=2022
# 转换为 VisualStudio 项目
xmake project -k vsxmake

# 执行成功后会在项目目录生成 vsxmake2022 文件夹, 进入此目录,打开 “项目名.sln” 即可进行开发

TIPS: 使用 VisualStudio 开发的话无需配置 VSCode。