C++ 如何使用 CMAKE_EXPORT_COMPILE_COMMANDS?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20059670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use CMAKE_EXPORT_COMPILE_COMMANDS?
提问by dzada
I've been trying to use clang-modernize
with CMAKE_EXPORT_COMPILE_COMMANDS
as recommended in the help of this tool.
我一直在尝试按照此工具的帮助中的建议使用clang-modernize
with CMAKE_EXPORT_COMPILE_COMMANDS
。
With this option cmake generates a JSON file containing compile info like include paths (see also).
使用此选项,cmake 会生成一个包含编译信息的 JSON 文件,例如包含路径(另请参阅)。
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).
这个变量在 cmake 的命令行上被接受,但cmake --help-variable CMAKE_EXPORT_COMPILE_COMMANDS
不起作用(这与此邮件列表发布一致)。
Has someone any idea on how to use it?
有人知道如何使用它吗?
I could also use it with cppcheck.
我也可以将它与 cppcheck 一起使用。
Some more info
更多信息
I've discovered on a clang developer forumthat 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.
我在clang 开发者论坛上发现,并非所有生成器都提供此 cmake 功能。这在未来可能会改变,同时我的问题仍然存在,我也会尝试看看如果我使用 Visual Studio 以外的其他生成器会发生什么。
回答by J?rn Reimerdes
I suggest setting
我建议设置
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
in the CMakeList.txt
在 CMakeList.txt 中
回答by maxschlepzig
As of CMake 3.5 the CMAKE_EXPORT_COMPILE_COMMANDS
option is supported by the Ninja and Makefiles generators.
从 CMake 3.5 开始,Ninja 和 Makefiles 生成器支持该CMAKE_EXPORT_COMPILE_COMMANDS
选项。
That means to generate a JSON compile database one has to select a generator that supports it.
这意味着要生成一个 JSON 编译数据库,必须选择一个支持它的生成器。
For example on UNIX just:
例如在 UNIX 上:
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 /path/to/src
(as it uses the makefile generator there, by default)
(因为它在那里使用 makefile 生成器,默认情况下)
Otherwise you can explicitly specify a generator like this:
否则,您可以像这样明确指定一个生成器:
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 /path/to/src -G Ninja
Or:
或者:
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 /path/to/src -G 'Unix Makefiles'
Or another makefiles variant that your cmake supports - a list of supported generators is included in the output of cmake --help
.
或者你的 cmake 支持的另一个 makefiles 变体 - 支持的生成器列表包含在cmake --help
.
Note that the compile database JSON file is generated at cmake execution time - not at compile time. Also, with recent clang versions (e.g. clang >= 3.8
), clang-modernize
was merged into clang-tidy
.
请注意,编译数据库 JSON 文件是在 cmake 执行时生成的 - 而不是在编译时。此外,最近的 clang 版本(例如 clang >= 3.8
)clang-modernize
已合并到clang-tidy
.
回答by zr.
I too was not able to get to work on the Visual Studio generator. It did, however, work using the "NMake Makefiles" generator.
我也无法开始使用 Visual Studio 生成器。但是,它确实使用“NMake Makefiles”生成器工作。
C:\work\build>cmake -G "NMake Makefiles" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..