Makefile pseudo-Command Parsing and makefile pseudo-Parsing

Source: Internet
Author: User

Makefile pseudo-Command Parsing and makefile pseudo-Parsing

This article was found in my blog and I think the pseudo commands for makefile are very detailed! I also mentioned why to use. PHONY: To declare a pseudo command! I hope this article will help you understand the pseudocommands of makefile!

My understanding:

Take the clean command as an example. If you define another file named clean after make is complete, the rm command will not be executed when you execute make clean.

To avoid this problem, you need. PHONY: clean

========================================================== ========================================================== ========

The so-called pseudo-target is such a target. It does not represent a real file name. When executing make, you can specify this target to execute the command defined by its rule, sometimes we turn a pseudo target into a tag.

So what is a pseudo-target? As a beginner, we may not care about this issue. Let's take a look at when we will need it.

First, let's look at the following example:

There is only one myls1.c in the current directory. In order to allow the program to manage makefiles, a simple makefile is written as follows.

Run:

You will find that you can use this makefile to manage the current project and generate the execution file myls as scheduled.

Run make clean to delete the executable program.

Then I set up a clean file in the current directory. What is the result of this execution?

So why can't I execute it again? My makefile does not actually modify anything. Why can't I manage my project's makefile at this time.

To solve this problem, add two lines. The modified makefile is as follows:

Run the following command again:

In this way, the problem is solved. What is the specific reason?

In makefile, we can solve the above problem by using a pseudo target. Why should we use a pseudo target, to avoid name conflicts between the targets defined in makefile that only execute commands and the actual files in the working directory, the other is the efficiency of submitting and executing makefile.

First case:

If we need to write such a rule: the command defined by the rule is not to create the target file, but to explicitly specify it through the make command line to execute some special commands, just like clean in the example. When there is no clean file in the folder, we enter "make clean" to execute as expected, but once the clean file appears in the folder, we enter "make clean" again ", because this rule does not have any dependency files, the target is considered to be the latest and does not execute the commands defined by the rule. Therefore, the rm command is not executed. To solve the problem, we define the target clean as a pseudo target.

That is, add:

. PHONY: clean

If there is a clean file in the directory, you only need to enter "make clean" to execute the rm command.

When a target is declared as a pseudo-target, make will not try to find the implicit rule to create it when executing the rule. In this way, the execution efficiency of make is improved, and there is no need to worry about the duplicate Object Name and file name.

Case 2:

Another type of pseudo-target is used in the parallel and Recursive Execution of make.

Example:

SUBDIRS = foo bar baz

Subdirs:

For dir in $ (SUBDIRS)

Do

$ (MAKE)-C $ dir

Done

In this case, several problems may occur:

1. make will not exit when the subdirectory executes make;

2. When using this shell loop method, the directory parallel processing function of make is not used.

With a pseudo-target, you can solve the above two problems.

SUBDIRS = foo bar baz

. PHONY: subdirs $ (SUBDIRS)

Subdirs: $ (SUBDIRS)

$ (SUBDIRS ):

$ (MAKE)-C $ @

Generally, a pseudo-target is not dependent on another target. When a pseudo-target is not dependent on any target, we can only use the make command to specify it as the ultimate goal of make and execute the command defined by its rule.

There is also a special pseudo-target -- all. If we create multiple executable programs in a directory, we can describe the reconstruction rules of all programs in a makefile.

All: p1 p2 p3

P1: p1.c

P2: p2.c

P3: p3.c


What is the definition of pseudo targets in makefile?

Eight Environment Variables
8.1 view environment variables
$ Env Environment: Display All environment variable settings
$ Echo $ ENV_VARIABLE variable display the setting of the specified environment variable
Example:
$ Echo $ PATH
/Bin:/etc:/usr/bin:/tcb/bin

8.2 set Environment Variables
$ ENV_VARIABLE = XXX; export ENV_VARIABLE
Example:
$ PATH = $ PATH: $ INFORMIXDIR/bin; export PATH Environment sets the environment variable PATH to the original PATH value + $ INFORMIXDIR/bin

8.3 cancel environment variable settings
$ Unset $ ENV_VARIABLE
Example:
$ Set GZJ = gzj; export GZJ environment set the environment variable GZJ
$ Echo $ GZJ
Gzj environment: environment variable value
$ Unset $ GZJ environment cancels the setting of the Environment Variable GZJ
$ Echo $ GZJ
Canceled

1. makefile rules
Makefile is a make rule description script file, which includes four types of lines: Target Line, command line, macro definition line, and make pseudo command line (such as "include "). The comments in the makefile file start. When a row cannot be written, you can use the "\" symbol to transfer it to the next row.
1.1 target row
The target row tells make what to create. It consists of a target table followed by a colon ":" And a dependency table.
Example:
Example: depfile deptarget
The target row indicates that the target example is dependent on depfile and deptarget. If depfile or deptarget is modified, the target is regenerated.
Example1 example2 example3: deptarget1 deptarget2 depfile
The target row indicates that the independent targets example1, example2, and example3 in the target table are generated using the same dependency list and rules.
Clean:
The empty dependency list indicates that the target clean has no other dependency.

The row starting with a Tab next to the target row indicates the generation rule of the target. The Tab character cannot be replaced by a space. For example:
Example. o: example. c example. h
Cc-c example. c
This example shows that the target example. o depends on example. c and example. h. If one of example. c or example. h is changed, run the cc-c example. c command to regenerate the target example. o.
You can use the file name pattern matching to automatically generate a dependency table for the target, for example:
Prog: *. c

The following is an example of a simple makefile:

Figure 1 simplest makefile example
Make uses makefile to start scanning from the first target. In the preceding example, the first target is all, so the target clean is not automatically executed. You can use the make clean command to generate the target.

1.2 command line
The command line defines the actions for generating the target.
In the target line, the semicolon ";" is regarded as a command, or a line that starts with a Tab is also a command.
For example, in the makefile example above, the cc command starting with the Tab character in the third line is a command line, which indicates the command to be generated for hello. It can also be written as: hello. o; cc-c hello-L...
In general, the command line will echo the full text in the standard output...>
 
Description of a makefile

View <write makefile with me>

Table 13-2 main pre-defined variables of GNU make
Predefined Variables
Description
$ *
The name of the target file that does not contain the extension.
$ +
All dependent files are separated by spaces and appear sequentially. They may contain duplicate dependent files.
$ <
The name of the first dependent file.
$?
All dependent files are separated by spaces. The modified date of these dependent files is later than the object creation date.
$ @
The complete name of the target.
$ ^
All dependent files are separated by spaces and do not contain duplicate dependent files.
$ %
If the target is an archive member, this variable indicates the name of the archive member of the target. For example, if the target name is
Mytarget. so (image. o), then $ @ is mytarget. so, and $ % is image. o.
AR
The name of the archive maintenance program. The default value is ar.
ARFLAGS
Archive maintenance program options.
AS
The name of the assembler. The default value is.
ASFLAGS
Assembler options.
CC
The name of the C compiler. The default value is cc.
CFLAGS
C compiler options.
CPP
The name of the C pre-compiler. The default value is $ (CC)-E.
CPPFLAGS
C precompiled options.
CXX
The name of the C ++ compiler. The default value is g ++.
CXXFLAGS
C ++ compiler options.
FC
Name of the FORTRAN compiler. The default value is f77.
FFLAGS
FORTRAN compiler options.

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.