Android system Development (3)--makefile writing

Source: Internet
Author: User

What is makefile?

The role of Makefile:

1, the project document organization, compiles the complex procedure

2. Install and uninstall our program

Makefile Use Example

The following three files are available in the/home/username/makefile directory:

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

Makefile rules for writing

The makefile consists of a number of the above rules, each of which is as follows:

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, and you need to rely on the MAIN.O func1.o func2.o three files while generating Hello, and execute 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 revise the makefile above.

#建立第二个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 but also a few func1.c func1.o func2.c func2.o main.c main.o file, below we modify the makefile file, 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, so let's take a look at the results.

FUNC1.O func2.o MAIN.O is removed after performing make clean (you can think of the principle of uninstalling the program), let's add an install and uninstall pseudo-target

When we perform the make install, we copy the execution files and library files we use to the specified directory and delete the copy files at the time of uninstall execution.

Let's use 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
If this is written mobj: = MAIN.O func1.o func2.o means no recursive variable. The predefined variables of the system can also be used, and the common predefined variables are as follows:

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

Cc_____c the name of the 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 predefined variables to rewrite the makefile file above us


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

Let's take a look at the automatic variables and environment variables in makefile:

$*_______ the name of the target file that does not contain an extension

$<_______ First dependent file name

$?_______ dependent files with all timestamps later than the target file

[Email protected]______ target file Full name

$^_______ all non-repeating dependent files

This time we should be clear about the content 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

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.