Android system development (3) -- compiling Makefile

Source: Internet
Author: User

Android system development (3) -- compiling Makefile
What is Makefile?

Functions of makefile:

1. Organize project files and compile them into complex programs

2. Install and uninstall our programs

Makefile example

The/home/username/makefile directory contains the following three files:

Main. c

#include 
 
  #include 
  
   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 
 
  int func1(int x, int y){        return x + y;}
 
Func2.c

#include 
 
  int func2(int x, int y){        return x + y;}
 
Next we will compile the above files:

The prepared Makefile file is as follows:

obj=main.c func1.c func2.chello2:$(obj)        gcc $^ -o $@.PHONY:cleanclean:        rm hello2install:        cp hello2 /usr/local
Run make

Makefile writing rules

Makefile consists of several rules. Each rule is as follows:

Taget objective: prequisites (dependency)

Command)

Note: The second line must have a Tab indent.

For example, the above two lines indicate that the target file "hello" is to be generated, and "hello" must depend on main. o func1.o func2.o and execute gcc main. o func1.o func2.o-o hello command to generate.

# Create the first Makefile file hello: main. c func1.c func2.c gcc main. c func1.c func2.c-o hello
Let's modify the Makefile above.

# Create the second Makefile file 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

It is found that not only the hello file is generated, but there are several more func1.c func1.o func2.c func2.o main. c main. o files. Let's modify the Makefile file and 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 that does not generate new files. Let's execute make clean to see the effect.

After executing make clean, func1.o func2.o main. o is all deleted (you can think of the principle of uninstalling the program). Next we will add an install and uninstall pseudo targets.

When we execute make install, We will copy the execution files and library files we use to the specified directory, and delete the copy files during installation when executing uninstall.

Next we will use the variable MObj to replace 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 you write MObj: = main. o func1.o func2.o, it indicates no recursive variable. You can also use the system's predefined variables. Common predefined variables are as follows:

AR _____ name of the Library File Maintenance Program, which defaults to ar

AS _____ assembler name, default value:

The name of the CC_____C compiler. The default value is cc.

CXX____C ++ compiler name. Default Value: g ++

ARFLAGS _____ Library File Maintenance Program option, no default value

ASFLAGS _____ assembler options, no default value

Cflags______ C compiler option, no default value

CXXFLAGS____C ++ compiler option, no default value

Next we will use the predefined variables to rewrite the Makefile file above.


The advantage of doing so is that it is very convenient for us to change the compiler. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Release/release + x5MG/release + 7 XExL + x6s7EvP7D + release + w/release/L + release/serOxLz + zerV + 8P7s8Y8L3A + PHA + JF5fX19fX19fy/release + PHA + + 1eK49sqxuvLO0sPH06a4w8fls/5E + tcTE2sjdwcs8L3A + PHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140825/2014082508533576.png" alt = ""/>

We have also declared the pseudo-target (. PHONY) to avoid conflicts with other files with the same name.


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.