clangd配置指南
本文最后更新于 203 天前,其中的信息可能已经有所发展或是发生改变。

关于clangd

系统官方的介绍可以看网上别的博客,按我个人理解,clangd就是给你提供代码补全,函数跳转之类的功能。

开始配置

我是MacOS,所以先直接brew install llvm,如果你是Debian系Linux,用apt install clang clangd clangd-tidy llvm即可

macOS brew的时候会出现

CLANG_CONFIG_FILE_SYSTEM_DIR: /opt/homebrew/etc/clang
CLANG_CONFIG_FILE_USER_DIR:   ~/.config/clang

LLD is now provided in a separate formula:
  brew install lld

We plan to build LLVM 20 with `LLVM_ENABLE_EH=OFF`. Please see:
  https://github.com/orgs/Homebrew/discussions/5654

Using `clang`, `clang++`, etc., requires a CLT installation at `/Library/Developer/CommandLineTools`.
If you don't want to install the CLT, you can write appropriate configuration files pointing to your
SDK at ~/.config/clang.

To use the bundled libunwind please use the following LDFLAGS:
  LDFLAGS="-L/opt/homebrew/opt/llvm/lib/unwind -lunwind"

To use the bundled libc++ please use the following LDFLAGS:
  LDFLAGS="-L/opt/homebrew/opt/llvm/lib/c++ -L/opt/homebrew/opt/llvm/lib/unwind -lunwind"

NOTE: You probably want to use the libunwind and libc++ provided by macOS unless you know what you're doing.

llvm is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have llvm first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.zshrc

For compilers to find llvm you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/llvm/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/llvm/include"

这是提示你macOS里面的clang和brew下来的clang冲突了,往~/.zshrc中加入如下语句即可。

export PATH="/opt/homebrew/opt/llvm/bin:$PATH"
# homebrew clang, llvm
export LDFLAGS="-L/opt/homebrew/opt/llvm/lib"
export CPPFLAGS="-I/opt/homebrew/opt/llvm/include"

接下来我们去vscode里面下载插件,通常是clangdCodeLLDBCMake Tools这几个一起下。

然后去clangd插件中配置参数,启用一些特性

"clangd.arguments": [
        "--enable-config",
        "--background-index",
        "--compile-commands-dir=build",
        "--clang-tidy",
        "--all-scopes-completion",
        "--completion-style=detailed",
        "--header-insertion=iwyu",
        "--pch-storage=disk",
        "-j=12"
    ],

然后去项目根目录,新增clangd配置,以下内容放在根目录下的.clangd中,要注意,clangd的用户级配置的优先级是大于项目配置的,这一点非常反人类。

用户级配置:

  • Windows: , typically .%LocalAppData%\clangd\config.yaml``C:\Users\Bob\AppData\Local\clangd\config.yaml

  • macOS~/Library/Preferences/clangd/config.yaml

  • Linux and others: , typically .$XDG_CONFIG_HOME/clangd/config.yaml``~/.config/clangd/config.yaml

CompileFlags:
    Add:
      [
        -std=c++17,
        -Wno-documentation,
        -Wno-missing-prototypes,
      ]
Diagnostics:
  ClangTidy:
    Add:
    [
        performance-*,
        bugprone-*,
        modernize-avoid-c-arrays,
				modernize-use-nodiscard,
        clang-analyzer-*,
        readability-identifier*,
    ]
    CheckOptions:
      readability-identifier-naming.VariableCase: lower_case
      readability-identifier-naming.ClassCase: CamelCase
      readability-identifier-naming.FunctionCase: CamelCase
      readability-identifier-naming.Structcase: CamelCase

具体参数可以看一下clang官网clang-tidy配置

然后我们配置lldb

vscode中创建lanuch.json,然后添加配置,选择CodeLLDB: Launch,稍微修改一下

"version": "0.2.0",
    "configurations": [
        {
            "name": "Debug",
            "type": "lldb",
            "request": "launch",
            "program": "${workspaceFolder}/build/${fileBasenameNoExtension}",
            "args": [],
            "cwd": "${workspaceFolder}",
        }
    ]

这样即可。按下F5调试代码,但是前提是你编译的文件在根目录的build文件下

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇