我在cmake中使用clang-tidy:
cmake_minimum_required(VERSION 3.10)
project(clang_tidy_test)
set(CMAKE_CXX_STANDARD 17)
# generate compile_commands.json if possible
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_VERBOSE_MAKEFILE ON)
# dependency headers
include_directories(dependency/printer/include)
# clang-tidy
# TODO header files-not found
set(CMAKE_CXX_CLANG_TIDY clang-tidy -checks=* --fix --fix-errors)
# compile executables
add_executable(test main.cpp)
源代码树是:
root/
- build/
- dependency/
- printer/
- include/
- printer.h
- main.cpp
我有一个 main.cpp 进行测试。
#include "printer.h"
int main(int argc, char** argv) {
Printer p;
p.Print();
}
我使用 NMake 作为cmake生成器为 clang-tidy 创建 compile_commands.json 文件。我可以编译并构建 main.cpp ,但是clang-tidy给出了一个错误(我用--fix-errors跳过了),说它找不到 printer.h
[ 50%] Building CXX object CMakeFiles/test.dir/main.cpp.obj
"C:\Program Files\CMake\bin\cmake.exe" -E __run_co_compile --tidy=clang-tidy;-checks=*;--fix;--fix-errors --source=C:\Users\micha\Documents\test\main.cpp -- C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\MSVC\1423~1.281\bin\Hostx64\x64\cl.exe @C:\Users\micha\AppData\Local\Temp\nmAC52.tmp
C:\Users\micha\Documents\test\main.cpp:1:10: error: 'printer.h' file not found [clang-diagnostic-error]
#include "printer.h"
^
我不知道为什么clang-tidy找不到该头文件,在 compile_commands.json 中添加了include路径,并且最终构建成功。