Clang-Tidy无法找到我的头文件

时间:2016-09-12 16:47:33

标签: c++ 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"

2 个答案:

答案 0 :(得分:3)

如果您使用CMake管理项目,此答案将仅对您有所帮助。

CMake可以选择使用命令行选项创建包含所有编译器调用的.json文件。可以使用以下选项将此文件赋予铿锵声:

-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文件中的命令中获取包含的pathes。

答案 1 :(得分:0)

我告诉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的工具。您也可以在-

之后的命令行上指定编译选项。