9 如何执行make

本文详细介绍了makefile的使用方法,包括如何通过命令行参数指定编译方式、更新部分文件、选择不同编译目标等。makefile默认会尝试更新第一个目标,但可以通过指定目标来实现部分程序的编译或者执行特殊任务,如清理、安装、测试等。此外,还提到了通过MAKECMDGOALS变量来控制目标的选择及其在不同场景下的应用。

A makefile that says how to recompile a program can be used in more than one way. The simplest use is to recompile every file that is out of date. Usually, makefiles are written so that if you run make with no arguments, it does just that.

But you might want to update only some of the files; you might want to use a different compiler or different compiler options; you might want just to find out which files are out of date without changing them.

By giving arguments when you run make, you can do any of these things and many others.

The exit status of make is always one of three values:

  • 0:The exit status is zero if make is successful.
  • 2:The exit status is two if make encounters any errors. It will print messages describing the particular errors.
  • 1:The exit status is one if you use the ‘-q’ flag and make determines that some target is not already up to date. See Instead of Executing Recipes.

1 Arguments to Specify the Makefile

The way to specify the name of the makefile is with the ‘-f’ or ‘–file’ option (‘–makefile’ also works). 例如,-f altmake 命令指示将 文件 altmake 作为makefile。

If you use the ‘-f’ flag several times and follow each ‘-f’ with an argument, all the specified files are used jointly as makefiles.

If you do not use the ‘-f’ or ‘–file’ flag, the default is to try GNUmakefile, makefile, and Makefile, in that order, and use the first of these three which exists or can be made (see Writing Makefiles).

2 Arguments to Specify the Goals

The goals are the targets that make should strive ultimately to update. Other targets are updated as well if they appear as prerequisites of goals, or prerequisites of prerequisites of goals, etc.

By default, the goal is the first target in the makefile (not counting targets that start with a period 这里需要补充一下period是什么). Therefore, makefiles are usually written so that the first target is for compiling the entire program or programs they describe. If the first rule in the makefile has several targets, only the first target in the rule becomes the default goal, not the whole list. You can manage the selection of the default goal from within your makefile using the .DEFAULT_GOAL variable (see Other Special Variables).

You can also specify a different goal or goals with command line arguments to make. Use the name of the goal as an argument. If you specify several goals, make processes each of them in turn, in the order you name them.

Any target in the makefile may be specified as a goal (unless it starts with ‘-’ or contains an ‘=’, in which case it will be parsed as a switch or variable definition, respectively). Even targets not in the makefile may be specified, if make can find implicit rules that say how to make them.

Make will set the special variable MAKECMDGOALS to the list of goals you specified on the command line. If no goals were given on the command line, this variable is empty. Note that this variable should be used only in special circumstances.

An example of appropriate use is to avoid including .d files during clean rules (see Automatic Prerequisites), so make won’t create them only to immediately remove them again:

sources = foo.c bar.c

ifneq ($(MAKECMDGOALS),clean)
include $(sources:.c=.d)
endif

One use of specifying a goal is if you want to compile only a part of the program, or only one of several programs. Specify as a goal each file that you wish to remake. For example, consider a directory containing several programs, with a makefile that starts like this:

.PHONY: all
all: size nm ld ar as

If you are working on the program size, you might want to say ‘make size’ so that only the files of that program are recompiled.

Another use of specifying a goal is to make files that are not normally made. For example, there may be a file of debugging output, or a version of the program that is compiled specially for testing, which has a rule in the makefile but is not a prerequisite of the default goal.

Another use of specifying a goal is to run the recipe associated with a phony target or empty target (see Empty Target Files to Record Events). Many makefiles contain a phony target named clean which deletes everything except source files. Naturally, this is done only if you request it explicitly with ‘make clean’. Following is a list of typical phony and empty target names. See Standard Targets, for a detailed list of all the standard target names which GNU software packages use.

  • all:Make all the top-level targets the makefile knows about.
  • clean:Delete all files that are normally created by running make.
  • mostlyclean:Like ‘clean’, but may refrain from deleting a few files that people normally don’t want to recompile. For example, the ‘mostlyclean’ target for GCC does not delete libgcc.a, because recompiling it is rarely necessary and takes a lot of time.
  • clobber:Any of these targets might be defined to delete more files than ‘clean’ does. For example, this would delete configuration files or links that you would normally create as preparation for compilation, even if the makefile itself cannot create these files.
  • install:Copy the executable file into a directory that users typically search for commands; copy any auxiliary files that the executable uses into the directories where it will look for them.
  • print:Print listings of the source files that have changed.
  • tar:Create a tar file of the source files.
  • shar:Create a shell archive (shar file) of the source files.
  • dist:Create a distribution file of the source files. This might be a tar file, or a shar file, or a compressed version of one of the above, or even more than one of the above.
  • TAGS:Update a tags table for this program.
  • test:Perform self tests on the program this makefile builds.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值