【问题标题】:Using libclang 6.0.1: stddef.h not found使用 libclang 6.0.1:找不到 stddef.h
【发布时间】:2019-02-24 15:29:22
【问题描述】:

为了开始使用 libclang,我构建了一个非常简单的程序,它尝试加载一个非常简单的源文件。它因找不到“stddef.h”文件而失败。

这是使用 libclang 的程序:

#include <clang-c/Index.h>
int main() {
    CXIndex index = clang_createIndex(1,1);
    const char *source_path = "foo.cpp";
    clang_createTranslationUnitFromSourceFile(index,"foo.cpp",0,0,0,0);
}

(为简洁起见,我省略了与重现问题无关的代码)。

这是我要加载的文件 foo.cpp:

#include <stddef.h>
int main() {}

我使用的是 LLVM 和 Clang 6.0.1,从源代码编译如下:

cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/local -DCMAKE_PREFIX_PATH=$HOME/local -DCMAKE_BUILD_TYPE=Release
make
make install

快速搜索得到这个有希望的帖子:Clang Error - stddef file not found? 不幸的是,这是关于 llvm 3.5,而我使用的是 llvm 6.0.1。另外,我安装 LLVM 和 Clang 的目录 $HOME/local 没有 /usr/lib 目录,所以那里提出的解决方案在这里不起作用。

stddef.h 标头位于 $HOME/lib/clang/6.0.1/include/stddef.h。将此路径作为 -isystem 选项显式添加到 clang_createTranslationUnitFromSourceFile 调用可以解决问题。

另外,clang_createTranslationUnitFromSourceFile 使用的 include 搜索路径与 clang++ 使用的不一样; clang++ foo.cpp 可以正常工作。

是否有关于 clang_createTranslationUnitFromSourceFile 使用的包含搜索路径和 libclang 中类似函数的文档,以便我可以确定需要添加哪些包含路径?

关于如何使用正确的包含搜索路径(相当于 clang++ 使用的路径)调用 clang_createTranslationUnitFromSourceFile 的任何其他建议?

【问题讨论】:

  • clang_parseTranslationUnit 的包含路径也有问题。使用 clang 构建是可以的,但与 clang_parseTranslationUnit 相同的 -I 参数不能完全正常工作。我注意到 clang 和 libclang 都在使用 HeaderSearch.cpp。

标签: c++ clang libclang


【解决方案1】:

libclang 就像您尝试编译文件一样工作,即:解析文件需要知道在哪里查找标题,以及可能的其他信息,例如宏定义、编译标志等。

clang_createTranslationUnitFromSourceFile 有两个参数:num_clang_command_line_argsclang_command_line_args。在那里你需要传递实际的标题搜索路径,例如:

const char *cli_args[] = { "-I", "~/local" };
clang_createTranslationUnitFromSourceFile(index, "foo.cpp", 2, cli_args, 0, 0);

【讨论】:

  • 好的,但是当我将 '#include ' 替换为 '#include ' 时,我仍然收到关于 stddef.h 的相同错误,而关于 string.h 没有错误. string.h 和 stddef.h 有什么区别?看起来“/usr/include”自动成为包含搜索路径的一部分,而“~/local/lib/clang/6.0.1/include”(包含stddef.h)不是。
  • 如果我在 clang_createTranslationUnitFromSourceFile 调用中添加“-I”“~/local/lib/clang/6.0.1/include”,它就可以工作。剩下的问题是:哪些包含路径是隐式添加的,哪些需要手动添加? clang_createTranslationUnitFromSourceFile 的答案似乎与 clang 命令的答案不同。
  • 而且libclang文档中没有记录
  • 这有助于找到 clang 命令的包含搜索路径,但不适用于 clang_createTranslationUnitFromSourceFile 调用。这些搜索路径似乎不同。我编辑了我的问题以澄清这一点。感谢您到现在为止的建议,它们确实帮助我弄清楚了问题。
猜你喜欢
  • 1970-01-01
  • 2017-03-07
  • 1970-01-01
  • 2018-07-27
  • 1970-01-01
  • 2016-05-08
  • 1970-01-01
  • 2014-05-08
  • 1970-01-01
相关资源
最近更新 更多