Linux under Programming tools

Source: Internet
Author: User

This article is reproduced from http://blog.csdn.net/zjf280441589/article/details/39959057

Brief introduction

1) Make: The build tool can be used to automate the compilation process. These include: If only a few source files have been modified, only those source files will be recompiled [make passes the time of the corresponding. c files and. o files]; If a header file is modified, all source files containing the header file will be recompiled. This automatic compilation can greatly simplify the development effort and avoid unnecessary recompilation.

2) The Mackfile:make tool completes and automatically maintains the compilation work through a file called Mackfile. The Mackfile file describes the compilation, connection, and other rules for the entire project.

Mackfile Basic Rules

Target ...: Dependencies ...

Command ...

Description

1) Target: The file that is intended to be produced, such as an executable file, a target file, or an intermediate file, or a target can be an action to be performed, such as clean, also known as a pseudo-target.

2) Dependent (DEPENDENCIES): The list of files to be relied upon to produce the target file, a target usually depends on multiple files.

3) Command: Is the action performed by make (a shell command or a program that can be executed under the shell, such as Echo). Note: The starting character of each command line must be the tab character!

If one or more files are updated in dependencies, the command is executed, which is the core content of makefile.

A simple Mackfile example

#1 the simplest

Hello

Gcc-o Hello hello.c

#2 a little complicated.

hello:hello.o

Gcc-o Hello hello.o

HELLO.O:

Gcc-o hello.o-c hello.c

#3 further improvement

hello:hello.o

Gcc-o Hello hello.o

HELLO.O:

Gcc-o hello.o-c hello.c

Clean

RM-RF hello.o #delete File hello.o

#4 Execute shell command in mackfile [@ symbol: Do not output command itself]

hello:hello.o

Gcc-o Hello hello.o

@echo "--------------OK------------"

HELLO.O:

Gcc-o hello.o-c hello.c

Clean

RM-RF hello.o #delete File hello.o

#5 synthesis [. Phony: #显示声明伪目标]

. Phony:clean

MAIN:MAIN.O SUB.O ADD.O

Gcc-wall-g-o main main.o sub.o ADD.O

Main.o:main.c

Gcc-wall-g-O main.o-c main.c

ADD.O:ADD.C add.h

Gcc-wall-g-O add.o-c add.c

SUB.O:SUB.C Sub.h

Gcc-wall-g-O sub.o-c sub.c

Clean

Rm-f MAIN.O SUB.O ADD.O

Perform:

1) Make #生成第一个目标

2) Make clean # "generate" clean pseudo target

3) Make MAIN.O #仅生成main. O Target

4) make-f Mackfile #显示指定执行的文件名

Makefile variable

To simplify and maintain mackfile, you can use variables in the mackfile format

Varname=some_text

Value of reference variable: $ (varname)

By convention, in Mackfile, variables are generally capitalized

Makefile Automation variables

Option name

Role

[Email protected]

The target file name of the rule

$<

The first dependent file name of a rule

$^

List of all dependent text for the rule

$*

The current dependent file name that does not include the suffix name

$?

New files in the list of files that the current target depends on than the current target file

#综合示例

. Phony:clean

. Suffixes:. C. o

. C.O:

GCC-WALL-G-o [email protected]-C $^

OBJECTS = MAIN.O SUB.O add.o

SOURCES = $ (OBJECTS:.O=.C)

Main: $ (OBJECTS)

GCC-WALL-G-o [email protected] $^

Clean

@echo "Delete execute file and object file ..."

Rm-f $ (OBJECTS) Main

Attached

. Suffixes:.c. o #表示任何x. c files associated with the X.O file

Make uses implicit inference rules/generate multiple executables

#示例-Generate multiple executables 1

. Phony:clean All

BIN = 01test 02test

All: $ (BIN)

Clean

-rm-f $ (BIN)

#示例-Generate multiple Executables 2-generate the. o Intermediate file using your own custom rules.

. Phony:clean All

BIN = 01test 02test

OBJECTS = $ (BIN:=.O)

All: $ (BIN)

01test.o:01test.c

02test.o:02test.c

Clean

-rm-f $ (BIN) $ (OBJECTS)

#示例-Generate multiple executables 3-use more granular rules

. Phony:clean All

CFLAGS =-g-wall

CC = gcc

BIN = 01test 02test

OBJECTS = $ (BIN:=.O)

. Suffixes:. C. o

. C.O:

$ (CC) $ (CFLAGS)-C $<-o [email protected]

All: $ (BIN)

01test.o:01test.c

02test.o:02test.c

Clean

-rm-f $ (BIN) $ (OBJECTS)

Pattern rules and suffix rules

%.o:%.c

. C.O:

#示例

. Phony:clean All

CC = gcc

CFLAGS =-wall-g

BIN = 01test 02test

SOURCES = $ (BIN:=.C)

OBJECTS = $ (BIN:=.O)

All: $ (BIN)

01test:01test.o

02test:02test.o

#%.o:%.c #模式规则

# $ (CC) $ (CFLAGS)-C $<-o [email protected]

. C.O: #后缀规则, same as previous function

$ (CC) $ (CFLAGS)-C $<-o [email protected]

Clean

-RM-RF $ (BIN) $ (OBJECTS)


Linux under Programming tools

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.