将 CMake 与 GNU Make 结合使用:如何查看确切的命令?
我将 CMake 与 GNU Make 一起使用,并希望准确地查看所有命令(例如编译器如何执行、所有标志等)。
GNU make 有 --debug
,但它似乎没有那么有用,还有其他选项吗? CMake 是否在生成的 Makefile 中提供附加标志以用于调试目的?
I use CMake with GNU Make and would like to see all commands exactly (for example how the compiler is executed, all the flags etc.).
GNU make has --debug
, but it does not seem to be that helpful are there any other options? Does CMake provide additional flags in the generated Makefile for debugging purpose?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
运行 make 时,添加
VERBOSE=1
以查看完整的命令输出。例如:或者您可以将
-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON
添加到 cmake 命令,以从生成的 Makefile 中获得永久详细命令输出。为了减少一些可能不太有趣的输出,您可能需要使用以下选项。选项
CMAKE_RULE_MESSAGES=OFF
删除诸如 [ 33%] Building C object... 之类的行,而--no-print-directory
告诉 make不打印当前目录,过滤掉诸如make[1]: Entering Directory
和make[1]: Leaving Directory
之类的行。When you run make, add
VERBOSE=1
to see the full command output. For example:Or you can add
-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON
to the cmake command for permanent verbose command output from the generated Makefiles.To reduce some possibly less-interesting output you might like to use the following options. The option
CMAKE_RULE_MESSAGES=OFF
removes lines like [ 33%] Building C object..., while--no-print-directory
tells make to not print out the current directory filtering out lines likemake[1]: Entering directory
andmake[1]: Leaving directory
.在
CMakeLists.txt
文件中将选项设置为:It is convenient to set the option in the
CMakeLists.txt
file as:或者简单地在 shell 上导出 VERBOSE 环境变量,如下所示:
<代码>
导出详细=1
Or simply export VERBOSE environment variable on the shell like this:
export VERBOSE=1
cmake --build 。 --verbose
在 Linux 上生成 Makefile,这可能只是在底层调用
make VERBOSE=1
,但是cmake --build
对于您的构建系统来说可以更具可移植性,例如跨操作系统工作,或者如果您决定稍后进行 Ninja 构建:其文档还表明它相当于
VERBOSE=1
:在 Cmake 3.22.1、Ubuntu 22.04 上测试。
cmake --build . --verbose
On Linux and with Makefile generation, this is likely just calling
make VERBOSE=1
under the hood, butcmake --build
can be more portable for your build system, e.g. working across OSes or if you decide to do e.g. Ninja builds later on:Its documentation also suggests that it is equivalent to
VERBOSE=1
:Tested on Cmake 3.22.1, Ubuntu 22.04.
如果您使用 CMake GUI,则切换到高级视图,该选项称为 CMAKE_VERBOSE_MAKEFILE。
If you use the CMake GUI then swap to the advanced view and then the option is called
CMAKE_VERBOSE_MAKEFILE
.CMake 3.14+
CMake 现在具有
--verbose
来指定详细构建输出。无论您的发电机如何,这都有效。值得注意的是,Xcode 可能无法与 -- 一起使用verbose
另一种选择是使用
VERBOSE
环境变量。CMake 3.14+
CMake now has
--verbose
to specify verbose build output. This works regardless of your generator.It's worth noting however Xcode may not work with --verbose
Another option it to use the
VERBOSE
environment variable.我正在尝试类似的方法来确保
-ggdb
标志存在。在干净的目录中调用 make 并 grep 您要查找的标志。寻找
debug
而不是ggdb
我只会写。<代码>使详细= 1 | grep debug
-ggdb
标志非常模糊,只弹出编译命令。I was trying something similar to ensure the
-ggdb
flag was present.Call make in a clean directory and grep the flag you are looking for. Looking for
debug
rather thanggdb
I would just write.make VERBOSE=1 | grep debug
The
-ggdb
flag was obscure enough that only the compile commands popped up.cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE
将生成一个文件 编译命令。某些 LSP 需要此文件来了解如何开箱即用地编译源文件,但它也可以帮助调试编译问题。
输出文件名为
${CMAKE_BINARY_DIR}/compile_commands.json
。cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE
will generate a file with all compilation commands.This file is required by some LSP to know how to compile a source file out of the box, but it could also help for debugging compilation problems.
The output file is named
${CMAKE_BINARY_DIR}/compile_commands.json
.