Linux programming basics 3 make automated Compilation

Source: Internet
Author: User
1 Introduction

GNU make is a utility software that automatically constructs software by reading files called makefile. It is a tool used to convert files. The conversion target is called "target". At the same time, it also checks the file dependency. If necessary, it calls some external software to complete the task. Its Dependency check system is very simple, mainly based on the dependent file modification time. In most cases, it is used to compile the source code, generate the result code, and then connect the result code to generate executable files or library files. It uses a file named "makefile" to determine the dependency of a target file, and then sends the command for generating this target to shell for execution.

Ii. Make command

Make: by default, makefile or makefile is used for compilation.

Make-f [makefile_name]: explicitly specifies the name of The makefile.

Three makefile details the MAKEFILE file consists of one or more rules.

3.1 rules

The rule is used to describe how to generate one or more target files.

Makefile example:

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.PHONY: cleanclean:        rm -f hello main.o func1.o func2.o

The first two rows are a rule. The basic form is as follows:

Target: Dependent commands

That is:

targest : prerequisites        command

Note: The command must start with the tab key.

3.2 Variables

The usage of variables is similar to that of shell scripts:

obj=main.o func1.o func2.o func3.ohello:$(obj)        gcc $(obj) -o hello

The benefit of using variables is maintainability.

3.3 default Variables

The default variables are as follows:

$ ^: Indicates all dependent files.
$ @: Indicates the target

$ <: Indicates the first dependent file.

Use default variables:

obj=main.o func1.o func2.o func3.ohello:$(obj)        gcc $^ -o $@

3.4 Miscellaneous

1. # post-Annotation
2. The command must start with the tab key.

3. Add @ before the command to cancel the screen echo.

#makefile of helloobj=main.o func1.o func2.o func3.ohello:$(obj)        @gcc $^ -o $@

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.