如何使用 CMAKE_EXPORT_COMPILE_COMMANDS?

2021-12-26 00:00:00 clang cmake c++ cppcheck

我一直在尝试将 clang-modernizeCMAKE_EXPORT_COMPILE_COMMANDS 结合使用,如在此工具的帮助中推荐的那样.

I've been trying to use clang-modernize with CMAKE_EXPORT_COMPILE_COMMANDS as recommended in the help of this tool.

使用此选项,cmake 会生成一个包含编译信息的 JSON 文件,例如包含路径(另见).

With this option cmake generates a JSON file containing compile info like include paths (see also).

这个变量在cmake的命令行中被接受,但是 cmake --help-variable CMAKE_EXPORT_COMPILE_COMMANDS 不起作用(这与 此邮件列表发布).

This variable is accepted on the command line of cmake, but cmake --help-variable CMAKE_EXPORT_COMPILE_COMMANDS doesn't work (which is coherent with this mailing list posting).

有人知道如何使用它吗?

Has someone any idea on how to use it?

我也可以将它与 cppcheck 一起使用.

I could also use it with cppcheck.

更多信息

我在 clang 开发者论坛 指出此 cmake 功能并非在所有生成器上都可用.这在未来可能会改变,同时我的问题仍然存在,我也会尝试看看如果我使用 Visual Studio 以外的其他生成器会发生什么.

I've discovered on a clang developer forum that this cmake feature is not available on all generators. This might change in the future, in the mean time my question remains and I will try too see what happen if I use other generators than Visual Studio.

推荐答案

我建议设置

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

在 CMakeList.txt 中

in the CMakeList.txt

相关文章