2

问题陈述:

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

我的 Makefile 有以下标志:

CFLAGS += -I/usr/local/share/mylib                             
LDFLAGS += -lmylib -L/usr/local/lib/
...
<snip>

但是,clangd 不知道如何找到头文件,因此我的编辑器显示如下错误,但无济于事:

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

mylib_type1_t foo                     unknown type name 'mylib_type1_t'

...
<snip>

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

问题是:

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

4

1 回答 1

2

tl;博士:

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

示例:我的compile_flags.txt包含:

-I/usr/local/share/mylib
-lmylib
-L/usr/local/lib/

我是如何排除故障的?

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

       --check[=<string>]                 - Parse one file in isolation instead of acting as a language server. Useful to investigate/reproduce crashes or configu‐
              ration problems. With --check=<filename>, attempts to parse a particular file.

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

E[13:12:40.787] [pp_file_not_found] Line 10: 'mylibheader.h' file not found

E[13:12:40.820] [unknown_typename] Line 187: unknown type name 'mylib_type1_t'
E[13:12:40.820] [unknown_typename] Line 202: unknown type name 'mylib_type1_t'
E[13:12:40.820] [undeclared_var_use] Line 820: use of undeclared identifier 'mylib_type2_t'
...
<snip>

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

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

  • 请注意,我没有使用 coc-neovim,而是使用 Neovim 的内置 lsp 服务器,所以这个问题没有直接关系,但恰好有我的解决方案。
于 2021-11-27T21:26:30.510 回答