Linux programming--make commands and Makefile files (chapter Nineth)

Source: Internet
Author: User

The 9.2 make command and the makefile file 9.2.6 built-in rules made commands themselves have a number of built-in rules that can greatly simplify the content of makefile files, especially if you have many source files. To test these built-in rules, create a file foo.c below, He is a traditional "Hello World" program:
#include <stdlib.h>
#include <stdio.h>
int main () {
printf ("Hello world\n");
Exit (exit_success);
}
In When you do not specify a makefile file, try to compile with the make commandIt:
Make Foo
CC Foo.c-o foo
can see that The Make command knows how to invoke the compiler, although in this case it chooses CC instead of GCC (in Linux systems, this is not a problem because CC is usually a connection file for GCC). Sometimes these built-in rules are also derived rules, because they all use macro definitions, so you can change their default behavior by giving the macro a new value.
RM Foo
Make CC=GCC cflags= "-wall-g" foo
Given the existence of these built-in rules, you can eliminate the rules for creating goals in the file makefile, and simply specify the dependencies to simplify the makefile file.
The 9.2.7 suffix and schema rule built-in rules use the file's suffix when used, so when a file with a specific suffix is given, The make command knows which rule should be used to create a file with a different suffix name. One of the most common rules is to create a file with a suffix of. O for a file that has the suffix name of. C.
To add a new suffix rule, you first need to add a line to the makefile file to tell the make command the new suffix name. You can then define the rule with this new suffix, and make uses special syntax:
. <odl_suffix>.<new_suffix>:
To define a general rule that allows you to create a file with a new suffix name from a file with the old suffix name, preserving the first half of the original file.
Here is a fragment of the makefile file that compiles the. cpp file into an. o file with a new general rule:
. Suffixes:. cpp
. CPP.O:
$ (CC)-xc++ $ (CFLAGS)-i$ (INCLUDE)-C $<
Special dependencies. CPP.O tells make that the following rule is used to convert a file with the suffix. cpp to a file with a suffix of. O. When defining this dependency, a special macro name is used, because the name of the file that will be converted is not known at this time. To understand this rule, Just remember that the macro $< will be extended to the name of the starting file (with the old suffix name). Note that just tell make how to get the. o file from the. cpp file, making already knows how to get a binary executable from a target file.
When the make command is called, it will use this new rule to get the bar.o file from the Bar.cpp file, and then use its rules to get the binary executable from the. o file. The role of the-xc++ flag is to tell the GCC compiler that this is a C + + source file.
Today's make version already knows how to handle the C + + source files for the suffix. cpp, but this technique is still useful when you need to convert one type of file to another type of file.
The latest make release also includes a new syntax for the same effect, and is more powerful. For example, a pattern rule can match a file name with a% wildcard, rather than relying solely on the suffix name of the files.
The pattern rules that can be achieved with the same effect as in the previous example. CPP rules are as follows:
%.cpp:%o
$ (CC)-xc++ $ (CFLAGS)-i$ (INCLUDE)-C $<
9.2.8 with make management library for large projects, a comparative approach is to use a library of functions to manage multiple compiled products. Libraries are actually files, and they usually have a. A (A is the first letter of archive) as the suffix name, A set of target files is included in the file. The make command uses a special syntax to handle library functions, making it easy to manage the library.
The syntax for managing a library is Lib (FILE.O), which means that the destination file file.o is stored in the library LIB.A. The make command uses a built-in rule to manage the library of functions, and the common form of this rule is as follows:
. C.A:
$ (CC)-O $ (CFLAGS) $<
$ (AR) $ (arflags) [email protected] $*. O
The default values for macro $ (AR) and $ (arflags) are usually command AR and option RV respectively. This fairly concise syntax tells make that to get. A library files from. c files, they should apply the above two rules.
The first rule tells it that the source file must be compiled to generate the target file
The second rule tells it to add the new target file to the library with the AR command
.
Therefore, if you have a library of functions named FUD that contains the target file bas.o, $< in the first rule will be replaced with BAS.C, and the [email protected] and $* in the second rule will be replaced by the library file fud.a and the name Bas respectively.


Linux programming--make commands and Makefile files (chapter Nineth)

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.