使用 Makefile 中的标志进行编译
为了澄清问题,我们假设我正在编译一个包含 3 个文件的程序 (prg
),main.c
、person.h
和 person.c
。
如果我使用简洁的方式编写 makefile,如下所示(更具体地说是最后两行):
prg : main.o person.o
gcc -Wall -o prg -c $^
main.o : person.h
person.o : person.h
-Wall
将应用于 main.o
和 person.o 自动?或者这根本不重要?
我知道,正如文件所述,如果 person.o
需要重新编译,prg
也需要重新构建。但是,我不知道仅在主要目标中指定 -Wall
是否足以启用其他目标,以便在编译其他目标时发出警告。
也许我错过了一些非常重要的事情,或者我说了一些毫无意义的话;但别着急,我只是个初学者:P
To clarify things, let's suppose I'm compiling a program (prg
) with 3 files, main.c
, person.h
and person.c
.
If I use a concise way of writing the makefile, like this (more specifically the two last lines):
prg : main.o person.o
gcc -Wall -o prg -c $^
main.o : person.h
person.o : person.h
Will the -Wall
be applied to main.o
and person.o
automatically? Or that doesn't even matter?
I know that, as the file says, if person.o
needs to be re-compiled, prg
will need a re-build too. But, I don't know if specifying -Wall
only in the main goal is enough to enable it the other targets so warnings are emitted as the others are compiled.
Maybe I'm missing something really important, or I'm saying something that makes no sense; but take it easy, I'm just a beginner :P
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您将 -Wall 应用于链接阶段(将目标文件收集为可执行文件时),但该选项应用于编译阶段(将源文件转换为目标文件),因此它没有任何好处它写在哪里。
您应该通过设置宏来修改编译。
通常,将 C 源文件编译为目标文件的规则如下所示:
其中可能还有其他位,并且符号可能使用
$*.c
以外的内容来标识源文件- 有类似的(大致等效)方法来指定这一点,但它与我的观点相切。$(CC)
表示法也相当于${CC}
。因此,要更改编译器,请指定“
CC=new_c_compiler
”,要更改编译选项,请(谨慎地)指定“CFLAGS=-Wall
”。这可以在 makefile 中或在命令行上完成。命令行会覆盖 makefile。
因此:
为什么要谨慎?因为在更复杂的情况下,CFLAGS 可能有一个复杂的定义,它是从多个来源构建的,并且随意设置 CFLAGS = -Wall 可能会丢失包含路径、宏定义和所有类型。就您而言,看起来您只需如图所示进行设置即可。
如果您使用 GNU Make,则只需将
-Wall
添加到CFLAGS
即可:这不一定适用于所有类型的
make
。您的链接行最终可能还需要收集一些与库相关的选项。
Since you apply the
-Wall
to the link phase (when collecting the object files into an executable), but the option applies to the compilation phase (converting the source files into object files), it provides no benefit where it is written.You should modify the compilation by setting macros.
Normally, the rule for compiling an C source file to an object file looks something like:
There could be other bits in there, and the notation might use something other than
$*.c
to identify the source file - there are similar (roughly equivalent) methods for specifying that, but it is tangential to the point I'm making. The$(CC)
notation is equivalent to${CC}
too.So, to change the compiler, you specify '
CC=new_c_compiler
' and to change to compilation options, you (cautiously) specify 'CFLAGS=-Wall
'.This can be done in the makefile, or on the command line. The command line overrides the makefile.
Hence:
Why cautiously? Because in more complex situations, there may be a complex definition of CFLAGS which build it up from a number of sources, and blithely setting
CFLAGS = -Wall
may lose your include paths, macro definitions, and all sorts. In your case, it looks like you can simply set it as shown.If you use GNU Make, you can simply add
-Wall
toCFLAGS
:This does not necessarily work with all varieties of
make
.Your link line may eventually need to collect some library-related options too.
不,这些标志不会神奇地应用于其他目标。
将如下行添加到 Makefile 顶部规则之上:
然后尝试不使用
prg
的显式行。No, the flags will not magically be applied to other targets.
Add a line like this to the top of your Makefile, above the rules:
Then try without the explicit line for
prg
.