Android system Development (3)--makefile writing

Source: Internet
Author: User

What is makefile?

The role of Makefile:

1, project file organization, compiled into a complex program

2. Install and uninstall our program

Makefile using Demo sample

Under the/home/username/makefile folder there are for example the following three files:

Main.c

#include <stdio.h> #include <stdlib.h>int main (int argc, char *argv[]) {        int x, y;        SSCANF (Argv[1], "%u", &x);        SCCANF (Argv[2], "%u", &y);        printf ("func1:%u\n", func1 (x, y));        printf ("func2:%u\n", Func2 (x, y));        return 0;}
func1.c

#include <stdio.h>int func1 (int x, int y) {        return x + y;}
Func2.c

#include <stdio.h>int func2 (int x, int y) {        return x + y;}
Here we start compiling the above file:

The makefile file is written as follows:

OBJ=MAIN.C func1.c func2.chello2:$ (obj)        gcc $^-o [email protected]. Phony:cleanclean:        RM hello2install:        CP hello2/usr/local
Run make

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzgf3yw5nyw5iyw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

Makefile rules for writing

Makefile is made up of several rules above. Each rule such as the following:

Taget target: prequisites (dependent)

Command (commands)

Note: The second line must have a tab indent

For example, the above two lines indicate that you want to generate the target file Hello, at the same time to generate Hello, you need to rely on MAIN.O func1.o func2.o these three files. and run the GCC main.o func1.o func2.o-o Hello command to generate.

#建立第一个Makefile文件hello: main.c func1.c func2.c        gcc main.c func1.c func2.c-o Hello
Let's change the makefile.

#建立第二个Makefile文件hello: main.o func1.o func2.o        gcc main.o func1.o func2.o-o hellomain.o:main.c        gcc-c main.cfunc1.o:func1.c        gcc-c func1.cfunc2.o:func2.c        gcc-c func2.c

Find not only generated Hello and a few func1.c func1.o func2.c func2.o main.c main.o file, let's change the makefile file below. Add a pseudo target

HELLO:MAIN.O func1.o func2.o        gcc main.o func1.o func2.o-o hellomain.o:main.c gcc-c        main.cfunc1.o:func1.c        Gcc-c func1.cfunc2.o:func2.c        gcc-c func2.cclean:        rm func1.o FUNC2.O MAIN.O
Clean is a pseudo-target and does not generate new files, let's run the make clean to see the effect

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzgf3yw5nyw5iyw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

After running make clean func1.o func2.o main.o all removed (can think of the principle of uninstalling the program). Let's add an install and uninstall pseudo-target.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzgf3yw5nyw5iyw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

When we run make install, we copy the running files and library files we use to the specified folder, and when we run uninstall, we delete the copied files at the time of installation.

Let's use the variable mobj instead of MAIN.O func1.o FUNC2.O

Mobj = MAIN.O func1.o func2.ohello:$ (mobj)        gcc $ (mobj)-O hellomain.o:main.c        gcc-c main.cfunc1.o:func1.c        Gcc-c func1.cfunc2.o:func2.c        gcc-c Func2.cclean:        RM $ (mobj) Install:        CP Hello/usr/local/hellouninstall:        Rm/usr/local/hello
Suppose this writes mobj: = MAIN.O func1.o func2.o means no recursive variable.

It is also possible to use the system's pre-defined variables, which are commonly defined in advance, such as the following:

ar_____ the name of the library file maintenance program, the default value is AR

The name of the as_____ assembler, with the default value as

The name of the Cc_____c compiler. The default value is CC

Cxx____c++ the name of the compiler, the default value is g++

ARFLAGS_____ library file maintenance program options, no default values

asflags_____ assembler options, no default values

Cflags______c compiler option, no default value

cxxflags____c++ compiler option, no default value

Here we use the pre-defined variables to rewrite our makefile file above

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzgf3yw5nyw5iyw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

The advantage of this is that it is convenient when we change the compiler.

Let's take a look at our own active variables and environment variables in makefile:

$*_______ destination file name with extension not included

$<_______ First dependent file name

$?_______ dependent file with full timestamp later than target file

[Email protected]______ target file Full name

$^_______ all non-recurring dependent files

This time we should be clear about the contents of the makefile file that we just started writing.


The pseudo-target is also declared (. Phony) This is done to avoid conflicts with the names of other files.


Android system Development (3)--makefile writing

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.