简体   繁体   English

默认的“ make”行为从何而来?

[英]Where does the default `make` behavior come from?

I'm pretty new to Makefiles; 我对Makefile很陌生。 thus, I encountered a question for which I can't come up with a good google search to help answer. 因此,我遇到了一个我无法通过良好的Google搜索来帮助回答的问题。

I am running a virtual OS which has a distro of fedora setup by someone else. 我正在运行一个虚拟操作系统,其中包含其他人发行的fedora安装程序。 If I construct my own Makefile in a directory, I can setup my .c files to compile however I like. 如果我在目录中构造自己的Makefile,则可以设置.c文件以进行任意编译。 Yet, if I simply run make test , whereby in my directory exists test.c , I will get the following : clang -ggdb3 -std=c99 -Wall -Werror test.c -lcs50 -lm -o test . 但是,如果仅运行make test ,从而在我的目录中存在test.c ,则将得到以下内容: clang -ggdb3 -std=c99 -Wall -Werror test.c -lcs50 -lm -o test

My question following this observation was where does this default, seemingly universal, make behavior come from? 我下面这个观察问题是哪里这个默认的,看似普遍, make行为从何而来? In other words, where does this Makefile, if it is one, sit on my file system? 换句话说,这个Makefile(如果是一个)位于我的文件系统上的什么位置?

make has several predefined implicit rules . make几个预定义的隐式规则 Two of which are: 其中两个是:

Compiling C programs no is made automatically from nc with a recipe of the form '$(CC) $(CPPFLAGS) $(CFLAGS) -c'. nc会自动以配方形式'$(CC)$(CPPFLAGS)$(CFLAGS)-c' 编译C程序 no。

Linking a single object file n is made automatically from no by running the linker (usually called ld) via the C compiler. 通过C编译器运行链接器(通常称为ld),可以自动从否进行 链接单个目标文件 n。 The precise recipe used is '$(CC) $(LDFLAGS) no $(LOADLIBES) $(LDLIBS)'. 使用的精确配方为“ $(CC)$(LDFLAGS)否$(LOADLIBES)$(LDLIBS)”。

Note, make is smart enough to effectively concatenate the above two into one rule when it makes sense: 注意, make足够聪明,可以在有意义的情况下有效地将上述两个规则合并为一条规则:

... could be done by using the '.o' object files as intermediates, but it is faster to do the compiling and linking in one step, so that's how it's done. ...可以通过将'.o'对象文件用作中间文件来完成,但是一步完成编译和链接会更快,因此就是这样。

You can dump the predefined rules with make -pn . 您可以使用make -pn转储预定义的规则。 eg: 例如:

$ make -pn -f /dev/null | grep -A3 '^%: %.c$'
make: *** No targets.  Stop.
%: %.c
#  commands to execute (built-in):
    $(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@

$

This goes for GNU make, which normally is the default make implementation on linux. 这适用于GNU make,这通常是linux上的默认make实现。

There's no default Makefile on your file system containing the default rules. 文件系统上没有包含默认规则的默认Makefile。 There are however implicit rules built into make that are in effect whether you supply a makefile or not, and what make does when invoked is documented here . 然而有内置化妆但在效果上是否提供一个makefile文件或不隐含的规则,什么make调用它时,记录在这里

These rules knows eg how to build an executable from a .c source file. 这些规则知道例如如何从.c源文件构建可执行文件。 You can learn about those implicit rules here , 您可以在此处了解这些隐式规则,

eg make has this default rule when building an executable: 例如,make在生成可执行文件时具有以下默认规则:

n is made automatically from no by running the linker (usually called ld) via the C compiler. n是通过C编译器运行链接器(通常称为ld)从否自动生成的。 The precise recipe used is '$(CC) $(LDFLAGS) no $(LOADLIBES) $(LDLIBS)' 使用的精确配方是“ $(CC)$(LDFLAGS)否$(LOADLIBES)$(LDLIBS)”

Meaning if you run make test it will try to create an executable test from the file test.o, and you can set the respective CC/LDFLAGS/etc. 这意味着如果您运行make test ,它将尝试从文件test.o创建可执行test ,并且可以设置相应的CC / LDFLAGS / etc。 variables that will be used when linking. 链接时将使用的变量。

And as another implicit rule it can build a .o file from a .c file, so the above will look for test.o , and try to rebuild that using the rule: 作为另一个隐式规则,它可以从.c文件构建.o文件,因此以上内容将查找test.o ,并尝试使用以下规则重新构建该文件:

no is made automatically from nc with a recipe of the form '$(CC) $(CPPFLAGS) $(CFLAGS) -c'. nc会自动以配方“ $(CC)$(CPPFLAGS)$(CFLAGS)-c”的形式自动生成否。

Ie the implicit rules when running make test will first compile test.c and then link test.o using the compiler you specify with the CC envirnment variable(or the default compiler cc ) and the various compiler/linker flags if you set then as environment variables. 也就是说,运行make test时的隐式规则将首先编译test.c,然后使用您使用CC环境变量(或默认编译器cc )指定的编译器和各种编译器/链接器标志(如果设置为环境)链接test.o。变量。

.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM