Analyze the differences between the linux make command and Makefile

Source: Internet
Author: User

When learning the linux make command, you may encounter the difference between make and makefile. Here we will introduce the detailed methods for the differences between make and makefile. Here we will share with you.

Make command

After the linux make command, you can not only see macro definitions, but also other command line parameters that specify the target file to be compiled. The standard format is:
Target1 [target2…] : [:] [Dependent1…] [; Commands] [#…]
[(Tab) commands] [#…]
The part in the middle of the square brackets indicates the option. Targets and dependents can contain characters, numbers, periods, and "/" symbols. In addition to references, commands cannot contain "#" or line breaks.
In general, the command line parameter contains only one ":". In this case, the command sequence is generally related to the description line of the dependency between some definition files in the makefile file. If the descriptive lines related to the target are specified with the relevant command sequence, execute these related command commands, even in the semicolon and (tab) the following aommand field may even be NULL. If no command is specified for the lines related to the target, the system will call the default target file generation rule.

If the command line parameter contains two colons ":", the command sequence may be related to all the lines in the makefile that describe the file dependency. At this time, the related commands directed to the description line related to the target will be executed. The build-in rule is also executed. If a non-"0" error signal is returned when the command is executed, for example, the wrong target file name or the command string with a hyphen appears in the makefile file, the make operation is generally terminated. However, if the make operation carries the "-I" parameter, make ignores this error signal. Make contains four parameters: Flag, macro definition, description file name, and target file name. The standard format is:
Make [flags] [macro definitions] [targets]

The flags option in Unix System and Its Meaning are:
◆-F file specifies that the file is a description file. If the file parameter is a "-" character, the description file points to the standard input. If the "-f" parameter is not specified, the system uses makefile or Makefile in the current directory as the description file by default. In linux, the GNU make tool searches for makefile files in the current working directory in the order of GNUmakefile, Makefile, and makefile.
◆-I ignore the error message returned by the command execution.
◆-S silence mode. No command line information is output before execution.
◆-R the build-in rule is forbidden.
◆-N non-execution mode. All execution commands are output but not executed.
◆-T update the target file.
◆-Q make will return the status information of "0" or not "0" based on whether the target file has been updated.
◆-P: output the description of all macro definitions and target files.
◆-D Debug mode: outputs detailed information about the file and detection time.

The common options of the make flag in linux are slightly different from those in Unix systems. Below we only list different parts:
◆-C dir changes to the specified directory dir before reading makefile.
◆-I dir when other makefile files are included, use this option to specify the search directory.
◆-H help text block, show all make options.
◆-W displays the working directory before and after makefile processing.

You can use the target parameter in the command line parameter to specify the targets to be compiled by make and allow simultaneous definition of multiple targets for compilation, during the operation, the target Files specified in the target option are compiled sequentially from left to right. If no target is specified in the command line, the system default target points to the first target file in the description file.

In general, the makefile also defines the clean object, which can be used to clear intermediate files in the compilation process, for example:
Clean:
Rm-f *. o
When you run make clean, the rm-f *. o command is executed to delete all intermediate files generated during compilation.
Implicit rules

The make tool contains built-in or implicit rules that define how to create specific types of targets from different dependent files. Unix systems generally support implicit rules based on file extensions, that is, filename suffixes. This suffix rule defines how to convert a file with a specified filename suffix, such as a. c file, into a file with another filename suffix, such as a. o file ):
. C:. o
$ (CC) $ (CFLAGS) $ (CPPFLAGS)-c-o $ @ $ <
The default common file extensions in the system and their meanings are:
◆. O target file
◆. C source file
◆. F FORTRAN source file
◆. S compile the source file
◆. Y Yacc-C source syntax
◆. L Lex source syntax
In earlier Unix systems, Yacc-C source syntax and Lex source syntax were also supported. During the compilation process, the system first looks for the files related to the target file in the makefile file. C file, if there is a dependency with it. y and. l file, first convert it. compile the c file and generate the corresponding one. o file; if there is no target-related. c files and only related. y file, the system will compile directly. y file.

In addition to suffix rules, GNU make also supports another type of implicit rules-pattern rules. This type of rule is more common, because pattern rules can be used to define more complex dependency rules. The Pattern Rule looks very similar to a regular rule. However, a % sign is added before the target name and can be used to define the relationship between the target and the dependent files, for example, the following pattern rule defines how to convert any file. c file to file. o file:
%. C: %. o
$ (CC) $ (CFLAGS) $ (CPPFLAGS)-c-o $ @ $ <
# EXAMPLE #

The following provides a comprehensive example to explain how to execute the makefile and make commands. The make command not only involves the C source file but also the Yacc syntax. This example is from "Unix Programmer's Manual 7th Edition, Volume 2A" Page 283-284
The following describes the specific content of the file:
# Description file for the Make command
# Send to print
P = und-3 | opr-r2
# The source files that are needed by object files
FILES = Makefile version. c defs main. c donamc. c misc. c file. c
Dosys. c gram. y lex. c gcos. c
# The definitions of object files
OBJECTS = vesion. o main. o donamc. o misc. o file. o dosys. o gram. o
LIBES =-LS
LINT = lnit-p
CFLAGS =-O
Make: $ (OBJECTS)
Cc $ (CFLAGS) $ (OBJECTS) $ (LIBES)-o make
Size make
$ (OBJECTS): defs
Gram. o: lex. c
Cleanup:
-Rm *. o gram. c
Install:
@ Size make/usr/bin/make
Cp make/usr/bin/make; rm make
# Print recently changed files
Print: $ (FILES)
Pr $? | $ P
Touch print
Test:
Make-dp | grep-v TIME> 1zap
/Usr/bin/make-dp | grep-v TIME> 2zap
Diff 1zap 2zap
Rm 1zap 2zap
Lint: dosys. c donamc. c file. c main. c misc. c version. c gram. c
$ (LINT) dosys. c donamc. c file. c main. c misc. c version. c
Gram. c
Rm gram. c
Arch:
Ar uv/sys/source/s2/make. a $ (FILES)

Generally, the description file should define the command to be executed as above. After running the linux make command, the output result is:
$ Make
Cc-c version. c
Cc-c main. c
Cc-c donamc. c
Cc-c misc. c
Cc-c file. c
Cc-c dosys. c
Yacc gram. y
Mv y. tab. c gram. c
Cc-c gram. c
Cc version. o main. o donamc. o misc. o file. o dosys. o gram. o
-LS-o make
13188 + 3348 + 3044 = 19580b = 046174b
The final numeric information is the output result of executing the "@ size make" command. The reason why only the output result does not have the corresponding command line is that the "@ size make" command starts with "@". This symbol does not allow printing the command line where the output is located.

The last several command lines in the description file are very useful in maintaining compilation information. The "print" command line is used to print the names of all files modified after the last "make print" command is executed. The system uses a 0-byte file named print to determine the specific time for executing the print command, while the macro $? Point to the file names of the files modified after the print file is changed. If you want to specify to run the print command and send the output results to a specified file, you can modify the macro definition of P:
Make print "P = cat> zap"

In linux, most software provides source code rather than ready-made executable files. This requires you to configure and compile the source program based on your system's actual situation and requirements, software can be used. Only by mastering the make tool can we truly enjoy the free software world of Linux, which brings us endless pleasure.
The above describes the differences between the linux make command and Makefile.

  1. Linux vendors released the latest operating system Ubuntu 9.10
  2. Linux Makefile
  3. Linux script compilation Basics
  4. Linux makefile
  5. Discuss the linux Emacs User Manual

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.