我有一个简单的设置,其中 CMake 生成以下编译命令:
cd /workspaces/cmake-general/tests/project/build/examples/hello-world && /usr/local/bin/cmake -E __run_co_compile
--iwyu=/usr/local/bin/include-what-you-use
--tidy="/usr/bin/clang-tidy;-extra-arg=-Wno-unknown-warning-option;-warnings-as-errors=*;--extra-arg-before=--driver-mode=g++"
--source=/workspaces/cmake-general/tests/project/build/examples/hello-world/CMakeFiles/example-hello-world.dir/cmake_pch.hxx.cxx --
/usr/bin/c++
-I/workspaces/cmake-general/tests/project/examples/hello-world/src
-I/workspaces/cmake-general/tests/project/build -O3 -DNDEBUG -stdlib=libc++
-fcolor-diagnostics -Wall -Wextra -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Wunused -Woverloaded-virtual -Wpedantic -Wconversion
-Wsign-conversion -Wnull-dereference -Wdouble-promotion
-Wformat=2 -Werror -std=c++20 -Winvalid-pch -fpch-instantiate-templates
-Xclang -emit-pch -Xclang -include
-Xclang /workspaces/cmake-general/tests/project/build/examples/hello-world/CMakeFiles/example-hello-world.dir/cmake_pch.hxx
-x c++-header -MD -MT examples/hello-world/CMakeFiles/example-hello-world.dir/cmake_pch.hxx.pch -MF
CMakeFiles/example-hello-world.dir/cmake_pch.hxx.pch.d -o CMakeFiles/example-hello-world.dir/cmake_pch.hxx.pch
-c /workspaces/cmake-general/tests/project/build/examples/hello-world/CMakeFiles/example-hello-world.dir/cmake_pch.hxx.cxx
我们没有找到这个错误
/workspaces/cmake-general/tests/project/build/examples/example-sanitizer/CMakeFiles/example-sanitizer-address.dir/cmake_pch.hxx:5:10: error: 'iostream' file not found [clang-diagnostic-error]
#include <iostream>
^
3 errors generated.
当我从命令中取出
--tidy="..."
的东西时。它完全编译,(如果我不在 CMakeLists.txt 中指定 PCH)它也可以工作。
我不确定到底是什么问题。
PCH 和 clang-tidy 不知何故不起作用?为什么包含目录不知何故丢失了?我在 Ubuntu 上使用 LLVM-12、clang-12。 另外添加
-I/usr/lib/llvm-12/include/c++/v1
也没有帮助。
如果您使用带有预编译头的 CMake,则 clang-tidy 将无法使用compile_commands.json,除非您实际上使用 clang 进行编译。 clang-tidy 只理解 clang 预编译头,而不理解 GCC 或 MSVC,如果遇到后者将会产生错误。
目前似乎没有任何方法可以解决这个问题。
我最近遇到了同样的问题。我发现的唯一令人满意的解决方法是使用 CMAKE 预设来使用 clang 而不是 GCC 构建项目,并使用它在整个项目上运行 clang-tidy。