【问题标题】:Clang-Tidy can't find my header filesClang-Tidy 找不到我的头文件
【发布时间】:2017-01-20 04:09:27
【问题描述】:

这里是 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"

【问题讨论】:

  • 会不会是不在同一个层级('someFile.hpp'不在/src)?
  • @Grif-fin 我已经想到了,但我不允许弄乱文件结构。是否有我可以设置的命令选项让它知道 *.hpp 文件在哪里?每个选项的描述并不总是足以让我了解正在发生的事情。 *:-/
  • 我刚刚尝试在 project/component1/ 目录上运行 clang-tidy 命令,但我遇到了同样的错误或src/ 下的那些文件。
  • 您可以尝试使用标志 --header-filter= 或使用 '../' 在 cpp 中包含 hpp 文件。例如#include "../someFile.hpp"。不推荐后一种选择。

标签: c++ clang clang-tidy


【解决方案1】:

只有在您使用 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.

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

【讨论】:

    【解决方案2】:

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

    clang-tidy -checks='...' <source0> ... -- -Iblabla/ ...
    

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


    发件人:https://clang.llvm.org/extra/clang-tidy/#using-clang-tidy

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

    【讨论】:

    • 这个答案非常有用。
    【解决方案3】:

    在 CMake 中遇到了同样的问题。我的问题是 Clang-Tidy 无法通过响应文件处理包含目录 - 至少在版本 10 中。

    停用它们解决了它:

    # Disable response files
    set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES Off)
    

    【讨论】:

      猜你喜欢
      • 2020-02-16
      • 2016-05-17
      • 2021-09-06
      • 2018-03-20
      • 1970-01-01
      • 2015-04-29
      • 1970-01-01
      • 2021-09-04
      • 2013-01-28
      相关资源
      最近更新 更多