c++ - 使用 RecursiveASTVisitor 解析系统 header 时出错

标签 c++ clang++ libtooling

我正在使用 libtooling 构建一个独立的工具。我有一个 FrontendAction 的基本样板代码, Consumer和一个 Visitor .访客只访问ClassTemplateSpecializationDecl我只是保存一些信息。一切正常,但如果我 #include <string>在我正在解析的文件中出现错误:'stddef.h' file not found .

我以为编译器找不到一些系统头文件,但我正在解析的输入文件使用 clang++ 命令编译时没有任何错误。

最佳答案

对于基于libTooling 的工具来说,这是一个非常典型的错误:https://clang.llvm.org/docs/FAQ.html#id3

Some header files (stddef.h, stdarg.h, and others) are shipped with Clang — these are called builtin includes. Clang searches for them in a directory relative to the location of the clang binary. If you moved the clang binary, you need to move the builtin headers, too.

如果您使用 CMake,您可以添加以下代码来安装 Clang 的 header :

set(CLANG_BUILTIN_HEADERS_DIR "${LLVM_LIBRARY_DIR}/clang")

install(
  DIRECTORY ${CLANG_BUILTIN_HEADERS_DIR}
  DESTINATION lib
  FILES_MATCHING PATTERN "*.h"
  )

关于c++ - 使用 RecursiveASTVisitor 解析系统 header 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55315394/

相关文章:

CMAKE:在 clang/g++ 和 libc++/libstdc++ 之间切换

c++ - g++ 和 clang++ SFINAE 和 SFINAE 失败的不同行为

Clang - 如何将 "Expr"作为字符串检索?

c++ - 模板函数中的多态性

c++ - 自定义 opengl 压缩纹理

c++ - static int 执行了多少次?

c++ - 铛 libTooling : How to find which header an AST item came out of?

c++ - 如何纠正多态性

c++ - std::make_unique 导致大幅减速?

c++ - Clang 使用 LibTooling Rewriter 生成新文件?