I use clang-tidy in cmake like this:
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)
source tree is:
root/
- build/
- dependency/
- printer/
- include/
- printer.h
- main.cpp
And I have a main.cpp for test.
#include "printer.h"
int main(int argc, char** argv) {
Printer p;
p.Print();
}
I use NMake as the cmake generator to create a compile_commands.json file for clang-tidy.I can compile and build main.cpp, but clang-tidy give a error(I skiped this with --fix-errors) saying it can not find the 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"
^
I don't know why clang-tidy can't find that header file,in compile_commands.json the include path is added and the final build is succeed.