Android makefile (android. mk) analysis (1)

Source: Internet
Author: User
Tags automake

1.1 makefile helloworld

MakefileThe rules are as follows:

Target...: prerequisites...

Command ......

Target can be a target File, an Object File (such as helloworld. obj), an execution File, and a tag.

Prerequisites is the file or target required to generate the target.

Command is the command to be executed to reach the target. Here we do not say "Use the command to generate the target" because the target may be a tag. Note that the command must be preceded by a TAB key rather than a space. Therefore, users who prefer to replace the TAB key with spaces in the editor must be especially careful.

We generally write programs like helloworld. After we write a c helloworld, how should we write helloworld to compile helloworld. c?

The following is the makefile for compiling helloworld.

Helloworld: helloworld. o

Cc-o helloworld. o

Helloworld. o: helloworld. c

Cc-c main. c

Clean:

Rm helloworld helloworl. o

Then we can execute make to compile helloworld. c, and execute make clean to clear the compilation result (in fact, it is to delete helloworld helloworl. o ).

Some people may ask why helloworld is generated when make is executed? Starting from the default processing of make: make regards the first target of makefile as the final

Target. All rules on which the rule depends will be executed. Otherwise, the rule will not be executed. Therefore, when executing make, the clean rule is not executed.

The above is the simplest makefile. The complex makefile begins to use advanced techniques, such as using variables and implicit rules, execute shell commands (string processing and file processing are common). These rules are not described here. The specific code will be used to analyze the makefile of android, let's take a look at Chen Hao's "Write makefile with me" to learn more.

The general structure of makefile is the program tree, as shown below:

In this way, it is easy to write makefile. We will use the target as the first rule, break down the target into sub-targets, write rules one by one, and so on, until the bottom rule is easily implemented. In fact, this is very similar to the divide and conquer method in the algorithm, and divide and conquer a complicated problem.

Speaking of the tree, I think of the syntax analysis in the compilation principle. There are top-bottom analysis methods and bottom-bottom analysis methods in the syntax analysis. Of course, makefile does not require syntax analysis, but is opposite to syntax analysis. (Syntax analysis is about whether a sentence can be introduced according to syntax, while makefile is about to generate a command execution Queue according to rules .) However, makefile rules are similar to lexical analysis. The following is an example of the compilation principle. You can understand the differences and similarities between makefile and lexical analysis:

<Identifier>-> <letter/number string>
<Letter/number string>-> <letter/number string> | <number> <letter/number string> | <underline> <letter/number string> | ε
<Unsigned integer>-> <number string>
<Digit string>-> <digit string> | ε
<Addition operator>-> +
<Subtraction operator>-
<Greater than Relational operators>->
<Greater than or equal to Relational operators>-> =

 

Finally, we will introduce autoconfautomake, which can be used to automatically generate makefile.

From the figure above, we can see that through autoscan, we can generate a file called configure. scan according to the code, and then we edit this file, the parameter is a file of configure. in. Then we write a makefile. am file, and then we can use automake to generate makefile. in. Finally, makefile can be generated based on makefile. in and configure. In many open-source projects, we can see makefile. am, configure. in, makefine. in and configure files may also see a very complex makefile file. Many people want to learn makefile by reading this file, but they finally find it too complicated. If we know that this file is automatically generated, we can understand why this makefile file is complicated.

For more details about tools such as automake, refer to http://www.ibm.com/?works/cn/linux/l-makefile /.

Related Article

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.