【Linux】【编译】编译调试过程中如何打印出实际的编译命令
- 🐚作者简介:花神庙码农(专注于Linux、WLAN、TCP/IP、Python等技术方向)
- 🐳博客主页:花神庙码农 ,地址:https://blog.csdn.net/qxhgd
- 🌐系列专栏:Linux技术
- 📰如觉得博主文章写的不错或对你有所帮助的话,还望大家三连支持一下呀!!! 👉关注✨、点赞👍、收藏📂、评论。
- 如需转载请参考转载须知!!
编译调试过程中如何打印出实际的编译命令
- 引言
- @
- make选项
- VERBOSE
- KBUILD_VERBOSE
- 利用shell -x
- 小结
引言
- 在Linux编译调试过程中,在分析参数过长(execvp: /bin/sh: Argument list too long)、宏不生效、头文件找不到、结构体字段对不上等问题时,经常需要确认实际的编译命令(gcc、ldd等)及相关参数,但makefile的编写维护人员,有可能通过某些手段将编译命令的打印做了控制。此时,就需要了解编译命令打印的控制机制以及一些操作技巧,以便更好的分析编译问题。
@
- 默认会打印全的gcc命令:
%.o:%.c$(CC) $(CFLAGS) $(INC) -o $@ -c $<
- 默认不会打印gcc命令:
%.o:%.c@$(CC) $(CFLAGS) $(INC) -o $@ -c $<
make选项
-n
--just-print
--dry-run
--recon
--debug=j
# Print the recipe that would be executed, but do not execute it (except in certain
circumstances).
- 上述这些选项,会导致make不管目标是否更新,把规则和连带规则下的命令打印出来,但不执行,这些参数对于我们调试makefile很有用处。
VERBOSE
- make(V=0是默认值),是quiet编译模式。
- make VERBOSE=1或make V=1是开启verbose选项,会打印详细命令。
- 原则上可以打印详细命令,但至于是否可以达到目的,还得看具体makefile的编写情况。
KBUILD_VERBOSE
- 官方文档:
- Set the kbuild verbosity. Can be assigned same values as “V=…”.
- See make help for the full list. Setting “V=…” takes precedence over
- KBUILD_VERBOSE.
- KBUILD_VERBOSE 定义在Linux/Makefile中,而make V=1会将V赋值给KBUILD_VERBOSE ,进而修改Q变量,Q变量会控制编译命令之前是否加@,进而达到控制编译命令是否输出的目的。
- 毫无疑问,该变量仅仅影响内核、驱动等模块的编译。
- ps:Linux-2.6.34\Makefile
ifeq ("$(origin V)", "command line")KBUILD_VERBOSE = $(V)
endif
ifndef KBUILD_VERBOSEKBUILD_VERBOSE = 0
endif
# Beautify output
# ---------------------------------------------------------------------------
#
# Normally, we echo the whole command before executing it. By making
# that echo $($(quiet)$(cmd)), we now have the possibility to set
# $(quiet) to choose other forms of output instead, e.g.
#
# quiet_cmd_cc_o_c = Compiling $(RELDIR)/$@
# cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
#
# If $(quiet) is empty, the whole command will be printed.
# If it is set to "quiet_", only the short version will be printed.
# If it is set to "silent_", nothing will be printed at all, since
# the variable $(silent_cmd_cc_o_c) doesn't exist.
#
# A simple variant is to prefix commands with $(Q) - that's useful
# for commands that shall be hidden in non-verbose mode.
#
# $(Q)ln $@ :<
#
# If KBUILD_VERBOSE equals 0 then the above command will be hidden.
# If KBUILD_VERBOSE equals 1 then the above command is displayed.ifeq ($(KBUILD_VERBOSE),1)quiet =Q =
elsequiet=quiet_Q = @
endif
利用shell -x
- make最终借助shell来执行,通过shell -x,可打印更为详尽的命令;
make SHELL='sh -x'
小结
- 每种方法都有一定的适应场合,同时还与具体makefile、编译框架等有关系,不一定就真的起作用。关键还是要对makefile的框架、make的机制以及具体模块编的译流程等比较熟悉,本文的手段对你才有意义。
如本文对你有些许帮助,欢迎大佬加关注、评论、点赞,有关必回关