为什么 cland 看不到我的自定义库位置

Bre*_*man 5 c linux makefile language-server-protocol clangd

问题陈述:

我在自定义位置安装了一个库,编译和链接工作正常,但 clangd 报告与自定义库相关的各种错误。

我的 Makefile 有以下标志:

CFLAGS += -I/usr/local/share/mylib                             
LDFLAGS += -lmylib -L/usr/local/lib/
...
<snip>
Run Code Online (Sandbox Code Playgroud)

但是,clangd 不知道如何查找头文件,因此我的编辑器显示如下错误,但没有帮助:

#include <mylibheader.h>    'mylibheader.h' file not found

mylib_type1_t foo                     unknown type name 'mylib_type1_t'

...
<snip>
Run Code Online (Sandbox Code Playgroud)

在互联网上搜索错误和关键字clangd, clangd config,clangd .clang-format未能在 SO 或其他地方产生任何有用的东西(因此这个问题)。

问题是:

如何告诉 clangd 我的非标准标头在哪里,以便它不会显示虚假的“文件未找到”警告?

Bre*_*man 10

长话短说:

\n

将编译标志添加到compile_flags.txt(小项目)或为更复杂的项目生成compile_commands.json请参阅此处的说明。

\n

示例:我的compile_flags.txt包含:

\n
-I/usr/local/share/mylib\n-lmylib\n-L/usr/local/lib/\n
Run Code Online (Sandbox Code Playgroud)\n

我是如何排除故障的?

\n

man 1 clangd给出了检查输出的有用提示

\n
       --check[=<string>]                 - Parse one file in isolation instead of acting as a language server. Useful to investigate/reproduce crashes or configu\xe2\x80\x90\n              ration problems. With --check=<filename>, attempts to parse a particular file.\n
Run Code Online (Sandbox Code Playgroud)\n

一些输出clangd --check=src/main.c

\n
E[13:12:40.787] [pp_file_not_found] Line 10: \'mylibheader.h\' file not found\n\nE[13:12:40.820] [unknown_typename] Line 187: unknown type name \'mylib_type1_t\'\nE[13:12:40.820] [unknown_typename] Line 202: unknown type name \'mylib_type1_t\'\nE[13:12:40.820] [undeclared_var_use] Line 820: use of undeclared identifier \'mylib_type2_t\'\n...\n<snip>\n
Run Code Online (Sandbox Code Playgroud)\n

添加pp_file_not_found到我的搜索词让我发现了这个非常有用的问题*,其中有一条评论说:

\n
\n

看起来您没有设置compile_flags.txt或compilation_database.json。该文件告诉 clangd 在哪里可以找到包含文件,您可以查看项目设置指南以获取详细信息。

\n
\n
    \n
  • 请注意,我没有使用 coc-neovim,而是使用 Neovim 的内置 lsp 服务器,因此这个问题不直接相关,但恰好有我的解决方案。
  • \n
\n

Linux 内核:

\n

编译后生成compile_commands.json使用./scripts/clang-tools/gen_compile_commands.py(使用.cmd文件)。

\n