Gnu makefile detailed tutorial-2: Makefile Overview

Source: Internet
Author: User
I. What is in Makefile?

Makefile mainly contains five things:Explicit rules,Concealed rules,Variable definition,File indicationAndNote.

1,Explicit rules: Explicit rules demonstrate how to generate one or more target files. This is clearly pointed out by Makefile writers that the files to be generated, the dependent files of the files, and the generated commands.

2,Concealed rules: Since make has the function of automatic derivation, obscure rules allow us to write Makefile roughly, which is supported by make.

3,Variable definition: In Makefile, we need to define a series of variables. The variables are generally strings. This is a bit of a macro in your c language,When Makefile is executedAnd all the variables are extended to the corresponding reference location.

4,File indicationIt consists of three parts:
1) reference another Makefile in one Makefile, just like include in C;
2) Specify the valid part of The Makefile in some cases, just like the pre-compiled # if in C;
3) define a multi-line command. I will discuss this part in the subsequent sections.

5,Note: The Makefile contains only line comments. Like the UNIX Shell script, the comments use the "#" character, which is like "//" in C/C ++. If you want to use the "#" character in your Makefile, you can escape it with a reverse box, for example, "#".

Finally, it is worth mentioning that the command in Makefile must start with the [Tab] key.

Ii. Makefile file name

By default, the make command will find the file name in the current directory in sequence as"GNUmakefile","Makefile","Makefile.

Among the three file names, it is best to use the "Makefile" file name, because the first character of the file name is in upper case, so there is a clear sense of purpose.
It is best not to use "GNUmakefile", which is identified by GNU make. Some other make files are only sensitive to all lowercase "makefile" file names.
But basically, most make files support the "makefile" and "Makefile" default file names.

Of course, you can use other file names to write Makefile, such as: "Make. linux "," Make. solaris "," Make. if you want to specify a specific Makefile, you can use the "-f" and "-- file" parameters of make, such as make-f Make. linux or make -- file Make. AIX.

3. Reference other makefiles

Use in MakefileIncludeKeyword can include other makefiles, which is similar to the # include in C language. The original mode of the contained files is placed in the position where the current file is contained.

The include syntax is:
Include <filename>

Filename can be the file mode of the current operating system Shell (including paths and wildcards)

There can be some null characters before include,But it must not start with the [Tab] key.. Include and <filename> can be separated by one or more spaces.

For example, you have several makefiles:. mk, B. mk, c. mk, there is also a file named foo. make, and a variable $ (bar), which contains e. mk and f. mk, then, the following statement:

include  foo.make *.mk $(bar)


It is equivalent:

include  foo.make a.mk b.mk c.mk e.mk f.mk

When the make command starts, it searches for other makefiles specified by include and places the files in the current location. It is like the # include command of C/C ++. If no absolute or relative path is specified for the file, make will first search for it in the current directory. If no absolute or relative path is found in the current directory, make will also find it in the following directories:
1) if the "-I" or "-- include-dir" parameter exists during make execution, make will search for it in the directory specified by this parameter.
2) If the directory <prefix>/include (generally:/usr/local/bin or/usr/include) exists, make will also find it.

If a file is not found, make generates a warning message without a fatal error. It will continue to load other files. Once the makefile is read, make will retry the unfound or unreadable files. If it still does not work, make will produce a fatal message. If you want make to ignore unreadable files and continue execution, you can add a minus sign "-" before include. For example:

-include <filename>

It indicates that no errors occur during the include process. The commands related to make compatibility with other versions are sinclude, which serves the same purpose as this one.

Iv. environment variables MAKEFILES

If your current environment defines the environment variable MAKEFILES, make will perform an include-like action on the value of this variable.

The values in this variable are other makefiles separated by spaces. But, unlike include,The "target" of the Makefile introduced in this environment change does not work. If an error is found in the file defined in the environment variable, make will ignore it..

However, we recommend that you do not use this environment variable here, because as long as this variable is defined, all makefiles will be affected when you use make, this is not what you want to see. This is just to tell you that there may be some strange things in your Makefile, so you can check whether the variable is defined in the current environment.

V. How make works

The execution steps of GNU make are as follows: (similar to other make statements)
1. Read all makefiles.
2. Read Other makefiles to be included.
3. initialize the variables in the file.
4. Deduce hidden rules and analyze all rules.
5. create dependency links for all target files.
6. Determine the targets to be regenerated Based on the dependency.
7. Run the generated command.

Step 1-5 is the first phase. In this phase, if the defined variable is used, make will expand it to the position in use. But make will not be fully expanded immediately. make uses the procrastination strategy. If the variable appears in the dependency rule, it will only be used when the dependency is determined, variable is expanded within it.

6-7 is the second stage.

Of course, you do not have to be clear about this method, but you will be more familiar with make. With this foundation, the subsequent sections will be easy to understand.

Reference: http://blog.csdn.net/liang13664759/archive/2007/09/04/1771246.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.