如何解决在 Windows 上使用带有 vs 代码的 cmake 没有 compile_commands.json 文件
我正在尝试将 Clang-Tidy 静态分析工具集成到我的构建系统中。 我的设置出现了一些问题。
步骤 cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
未生成 compile_commands.json 文件。
因此,clang-tidy 给出了以下错误。
>cmake --build .
结果
Could not auto-detect compilation database from directory "C:/dev/my-project/build/compile_commands.json"
No compilation database found in C:\dev\my-project\build\compile_commands.json or any parent directory
fixed-compilation-database: Error while opening fixed database: no such file or directory
json-compilation-database: Error while opening JSON database: no such file or directory
Running without flags.
Error while processing C:\dev\my-project\src\.
CUSTOMBUILD : error : unable to handle compilation,expected exactly one compiler job in '' [clang-diagnostic-error] [C:\dev\my-project\build\analyze_clang_tidy.vcxproj]
Suppressed 1 warnings (1 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
Clang-tidy.cmake
文件
set(CLANG_TIDY_CHECKS "-checks='${CLANG_TIDY_CHECKS}'")
add_custom_target(analyze_clang_tidy ALL
COMMAND ${CLANG_TIDY}
-p ${CMAKE_BINARY_DIR}/compile_commands.json
${CLANG_TIDY_CHECKS}
-header-filter='.*'
${CMAKE_CURRENT_SOURCE_DIR}/src/
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Static code analysis with Clang-Tidy"
)
请同时查找配置结果
-- Building for: Visual Studio 14 2015
-- Selecting Windows SDK version to target Windows 10.0.17763.
-- The CXX compiler identification is MSVC 19.0.24215.1
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
Default build type: Debug
iD MSVC
-- ccache found and enabled
-- Configuring done
-- Generating done
-- Build files have been written to: C:/dev/my-project/build
我的 int main() {...}
文件夹中有一个简单的 hello world ../src/
。
请建议我如何在 Windows 上使用带有 VS 代码的 CMake 生成 compile_commands.json 文件。 谢谢!
解决方法
见https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html
特别是这个注意:
注意 此选项仅由 Makefile Generators 和 Ninja 实现。它在其他生成器上被忽略。
相反,您可以编写一个 Python 脚本来为您生成该格式,类似于此工具:https://github.com/nickdiego/compiledb
或者,实际上,您不需要 compile_commands.json
。您可以定义 CMAKE_<LANG>_CLANG_TIDY
变量以自动触发 clang-tidy:https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_CLANG_TIDY.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。