Clang-Tidy 找不到我的头文件

Mag*_* S. 11 c++ clang clang-tidy

这里是 clang 和 clang-tidy 的新手。

我有一个具有这种结构的项目: project/ - build/ - cmake/ - component1/ - src/ - someFile.cpp - someFile2.cpp - someFile.hpp - someFile2.hpp - component2/ - etc... -

当我使用 clang-tidy 通过project/component1/以下命令查看所有文件时:clang-tidy project/component1/src/* -checks=-*,clang-analyzer-*,-clang-analyzer-alpha*

它最终抛出这样的错误: $HOME/project/component1/src/someFile.cpp:18:10: error: 'project/component1/someFile.hpp' file not found [clang-diagnostic-error] \#include "component1/someFile.hpp"

Kni*_*chi 16

如果您使用 CMake 来管理您的项目,此答案只会对您有所帮助。

CMake 可以选择创建一个 .json 文件,该文件包含所有带有命令行选项的编译器调用。可以使用以下选项将此文件提供给 clang-tidy:

-p <build-path> is used to read a compile command database.

    For example, it can be a CMake build directory in which a file named
    compile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
    CMake option to get this output). When no build path is specified,
    a search for compile_commands.json will be attempted through all
    parent paths of the first input file . See:
    http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an
    example of setting up Clang Tooling on a source tree.
Run Code Online (Sandbox Code Playgroud)

正如文档所述,您必须设置CMAKE_EXPORT_COMPILE_COMMANDS变量以使用 CMake 生成 .json 文件,然后将 CMake 输出目录传递给 clang-tidy。Clang-tidy 然后将从 .json 文件中的命令获取包含路径。


eug*_*rez 14

我告诉 clang-tidy 使用普通的编译器包含来搜索它们,但是它们必须在双破折号 (--) 之后引入。我也花了一段时间才发现它,因为它不包含在--help

clang-tidy -checks='...' <source0> ... -- -Iblabla/ ...
Run Code Online (Sandbox Code Playgroud)

再次阅读选项,您可以尝试尝试-extra-arg=参数,但我使用双破折号 approax,因为它允许我将所有选项放在一个文件中,以提供 clang 和 clang-tidy 的所有选项,而不需要$(cat $file)对两者进行更多处理。


来自:https : //clang.llvm.org/extra/clang-tidy/#using-clang-tidy

clang-tidy 是一个基于 LibTooling 的工具。您还可以在命令行上指定编译选项 --

  • 这个答案非常有用。 (2认同)