Clangd 与 MinGW 的问题

问题描述 投票:0回答:1

我一直将 Neovim 与 clangd 用于 C,并且在处理包含

windows.h
的 C++ 文件时遇到配置问题。虽然我已经设置了配置文件
config.yaml
以使用 MinGW 编译器,对于 C 文件没有任何问题,但在处理 C++ 文件时,clangd 似乎找不到
windows.h

这些是我的旧

config.yaml
(与c配合良好):

CompileFlags:
  Compiler: x86_64-w64-mingw32-gcc
  Add: ["-I../headers", "-I$INCLUDE_PATH"]

但现在我想使用 c++,所以 这是我的新

config.yaml

If:
  PathMatch: .*\.c .*\.h

CompileFlags:
  Compiler: x86_64-w64-mingw32-gcc
  Add: ["-I../headers", "-I$INCLUDE_PATH"]
---

If:
  PathMatch: .*\.cpp .*\.hpp

CompileFlags:
  Compiler: x86_64-w64-mingw32-g++
  Add: ["-I../headers", "-I$INCLUDE_PATH"]
---

这些事情发生了:

enter image description here

即使它编译没有任何问题。

这些是 neovim LSP 日志(如果有帮助的话)。

[START][2024-05-01 19:22:24] LSP logging initiated
[ERROR][2024-05-01 19:22:24] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:24.429] clangd version 17.0.3 (https://github.com/llvm/llvm-project 888437e1b60011b8a375dd30928ec925b448da57)\nI[19:22:24.429] Features: linux+grpc\nI[19:22:24.429] PID: 1238686\nI[19:22:24.429] Working directory: /home/panda/path/to\nI[19:22:24.429] argv[0]: /home/panda/.local/share/nvim/mason/bin/clangd\nI[19:22:24.429] Starting LSP over stdin/stdout\nI[19:22:24.429] <-- initialize(1)\n"
[ERROR][2024-05-01 19:22:24] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:24.430] --> reply:initialize(1) 0 ms\n"
[ERROR][2024-05-01 19:22:24] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:24.431] <-- initialized\nI[19:22:24.432] <-- textDocument/didOpen\nE[19:22:24.432] config error at /home/panda/.config/clangd/config.yaml:24:0: Config should be a dictionary\nI[19:22:24.432] --> textDocument/publishDiagnostics\nI[19:22:24.432] Failed to find compilation database for /home/panda/path/to/main.cpp\nI[19:22:24.432] ASTWorker building file /home/panda/path/to/main.cpp version 0 with command clangd fallback\n[/home/panda/path/to]\n/usr/lib/llvm-16/bin/clang -resource-dir=/home/panda/.local/share/nvim/mason/packages/clangd/clangd_17.0.3/lib/clang/17 -- /home/panda/path/to/main.cpp\n"
[ERROR][2024-05-01 19:22:24] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:24.678] Built preamble of size 3580972 for file /home/panda/path/to/main.cpp version 0 in 0.25 seconds\n"
[ERROR][2024-05-01 19:22:24] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:24.678] --> workspace/semanticTokens/refresh(0)\n"
[ERROR][2024-05-01 19:22:24] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:24.679] Indexing c++17 standard library in the context of /home/panda/path/to/main.cpp\n"
[ERROR][2024-05-01 19:22:24] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:24.679] <-- reply(0)\n"
[ERROR][2024-05-01 19:22:24] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "E[19:22:24.685] IncludeCleaner: Failed to get an entry for resolved path : No such file or directory\n"
[ERROR][2024-05-01 19:22:24] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:24.685] --> textDocument/publishDiagnostics\n"
[ERROR][2024-05-01 19:22:24] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:24.689] <-- textDocument/signatureHelp(2)\n"
[ERROR][2024-05-01 19:22:24] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:24.691] --> reply:textDocument/signatureHelp(2) 2 ms\n"
[ERROR][2024-05-01 19:22:25] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:25.962] Indexed c++17 standard library (incomplete due to errors): 13604 symbols, 1918 filtered\n"
[ERROR][2024-05-01 19:22:26] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:26.750] <-- textDocument/didSave\n"
[ERROR][2024-05-01 19:22:26] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:26.750] File version went from 0 to 0\n"
[ERROR][2024-05-01 19:22:26] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:26.800] Failed to find compilation database for /home/panda/path/to/main.cpp\n"
[ERROR][2024-05-01 19:22:26] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:26.800] ASTWorker building file /home/panda/path/to/main.cpp version 0 with command clangd fallback\n[/home/panda/path/to]\n/usr/lib/llvm-16/bin/clang -resource-dir=/home/panda/.local/share/nvim/mason/packages/clangd/clangd_17.0.3/lib/clang/17 -- /home/panda/path/to/main.cpp\n"
[ERROR][2024-05-01 19:22:27] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:27.008] <-- textDocument/signatureHelp(3)\n"
[ERROR][2024-05-01 19:22:27] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:27.016] --> reply:textDocument/signatureHelp(3) 7 ms\n"
[ERROR][2024-05-01 19:22:34] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:34.880] <-- textDocument/signatureHelp(4)\n"
[ERROR][2024-05-01 19:22:34] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:34.888] --> reply:textDocument/signatureHelp(4) 7 ms\n"
[ERROR][2024-05-01 19:22:35] .../vim/lsp/rpc.lua:734    "rpc"   "/home/panda/.local/share/nvim/mason/bin/clangd"        "stderr"        "I[19:22:35.437] <-- shutdown(5)\nI[19:22:35.437] --> reply:shutdown(5) 0 ms\n"

这个问题与我的新配置有关吗?如果是这样,我如何配置 clangd 以使用 Mingw 编译器来处理 C 和 C++?

谢谢!

我尝试过调整路径和配置,但到目前为止还没有任何运气。任何有关如何解决此问题的建议将不胜感激。

c++ mingw neovim clangd
1个回答
0
投票

经过几次尝试,我发现问题出在 clangd 没有识别

x86_64-w64-mingw32-g++
的包含目录。

我不太明白为什么它适用于

x86_64-w64-mingw32-gcc
而不是
x86_64-w64-mingw32-g++

但是,为了解决这个问题,我使用以下命令找到了 g++ 的所有包含目录:

x86_64-w64-mingw32-g++ -print-search-dirs

这就是输出:

install: /usr/lib/gcc/x86_64-w64-mingw32/12-win32/
programs: =/usr/lib/gcc/x86_64-w64-mingw32/12-win32/:/usr/lib/gcc/x86_64-w64-mingw32/12-win32/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/12
-win32/:/usr/lib/gcc/x86_64-w64-mingw32/:/usr/lib/gcc/x86_64-w64-mingw32/12-win32/../../../../x86_64-w64-mingw32/bin/x86_64-w64-mingw32/12-win32/:/usr/lib/gcc/x86
_64-w64-mingw32/12-win32/../../../../x86_64-w64-mingw32/bin/
libraries: =/usr/lib/gcc/x86_64-w64-mingw32/12-win32/:/usr/lib/gcc/x86_64-w64-mingw32/12-win32/../../../../x86_64-w64-mingw32/lib/x86_64-w64-mingw32/12-win32/:/us
r/lib/gcc/x86_64-w64-mingw32/12-win32/../../../../x86_64-w64-mingw32/lib/

从这些目录中,我选择了这三个:

  • /usr/lib/gcc/x86_64-w64-mingw32/12-win32/include/c++
    :该目录包含 C++ 标准库的主要头文件。它包括容器(如向量、映射等)、算法、迭代器和其他标准 C++ 组件的标头。

  • /usr/lib/gcc/x86_64-w64-mingw32/12-win32/include/c++/x86_64-w64-mingw32
    :该子目录包含 C++ 标准库的特定于体系结构的标头。它可能包括优化或特定于 MinGW-w64 使用的 x86_64 架构的标头。

  • /usr/lib/gcc/x86_64-w64-mingw32/12-win32/include/c++/backward
    :此子目录包含旧版本 C++ 标准库的兼容性标头。它可能包含与旧代码库保持兼容性或提供已弃用功能的标头。

所以我确实将

.config/clangd/config.yaml
更改为:

If:
  PathMatch: .*\.c
CompileFlags:
  Compiler: x86_64-w64-mingw32-gcc
  Add: ["-I../headers", "-I$INCLUDE_PATH"] # These two arguments are not affecting anything; they just match my needs.
---
If:
  PathMatch: .*\.cpp
CompileFlags:
  Compiler: x86_64-w64-mingw32-g++
  Add: ["-I../headers", "-I$INCLUDE_PATH", "-I/usr/lib/gcc/x86_64-w64-mingw32/12-win32/include/c++", "-I/usr/lib/gcc/x86_64-w64-mingw32/12-win32/include/c++/x86_64-w64-mingw32", "-I/usr/lib/gcc/x86_64-w64-mingw32/12-win32/include/c++/backward"]
---

小提示:在问题帖子中,我没有使用命名空间 std。

© www.soinside.com 2019 - 2024. All rights reserved.