`

Makefile的使用技巧

 
阅读更多

Makefile的使用技巧

1、 makefile中的两种变量

Ø COMPILE=$(CC)$(CFLAGS) -c

在使用COMPILE的时候,CCCFLAGS才会展开,并且是每次都会展开,所以当定义COMPILE的时候,即使CCCFLAGS还没有值,都没有关系,只要当使用COMPILE时,CCCFLAGS有值就可以了。

OBJS=$(wildcard *.o)

OBJS每次在引用的时候,获取目标文件的列表,每一次可能都不同,并且每次计算通配符导致运行速度变慢。

Ø COMPILE=$(CC)$(CFLAGS) -c

在执行这行后,在使用COMPILE时,其值不会再变化,即使CCCFLAGS变化了。当然可以显示的赋值给COMPILE来改变其值。

2、 使用函数

Ø wildcard

SRCS := $(wildcard *.c)

获得所有的C文件列表

Ø patsubst

OBJS := $(patsubst %.c,%.o,$( wildcard *.c ))

获得所有c文件对应的o文件列表

3、 特殊符号

%.o: %.c

gcc -Wall -c -o $@ $<

将所有的C文件编译成O文件。

4、 两种编译方式

Ø gcc -o myprogram 1.c 2.c

1.c2.c编译成myprogram程序。

Ø gcc -c -o 1.o 1.c

1.c编译成O文件。

5、 依赖性计算

gcc -M init.c > init.d

将所有的依赖文件(包括系统文件)写入d文件

gcc -MM init.c > init.d

将不包括系统文件的依赖文件写入d文件

6、 智能makefile模板

# Lines starting with the pound sign are comments.

#

# This is one of two options you might need to tweak.

EXECUTABLE = myprogram

# You can modify the below as well, but probably

# won't need to.

#

# CC is for the name of the C compiler. CPPFLAGS denotes pre-processor

# flags, such as -I options. CFLAGS denotes flags for the C compiler.

# CXXFLAGS denotes flags for the C++ compiler. You may add additional

# settings here, such as PFLAGS, if you are using other languages such

# as Pascal.

CC = gcc

CPPFLAGS =

CFLAGS = -Wall -O2

CXXFLAGS = $(CFLAGS)

COMPILE = $(CC) $(CPPFLAGS) $(CFLAGS) -c

SRCS := $(wildcard *.c)

OBJS := $(patsubst %.c,%.o,$(SRCS))

DEPS := $(patsubst %.c,%.d,$(SRCS))

# "all" is the default target. Simply make it point to myprogram.

all: $(EXECUTABLE)

# Define the components of the program, and how to link them together.

# These components are defined as dependencies; that is, they must be

# made up-to-date before the code is linked.

#最终的二进制程序依赖于d文件和o文件

$(EXECUTABLE): $(DEPS) $(OBJS)

$(CC) -o $(EXECUTABLE) $(OBJS)

# Specify that the dependency files depend on the C source files.

#生成d文件,d文件内容包括两行,o文件的依赖关系,d文件的依赖关系

%.d: %.c

$(CC) -M $(CPPFLAGS) $< > $@

$(CC) -M $(CPPFLAGS) $< | sed s///.o/.d/ >> $@

# Specify that all .o files depend on .c files, and indicate how

# the .c files are converted (compiled) to the .o files.

#c文件生成o文件

%.o: %.c

$(COMPILE) -o $@ $<

#删除四类文件

clean:

-rm $(OBJS) $(EXECUTABLE) $(DEPS) *~

explain:

@echo "The following information represents your program:"

@echo "Final executable name: $(EXECUTABLE)"

@echo "Source files: $(SRCS)"

@echo "Object files: $(OBJS)"

@echo "Dependency files: $(DEPS)"

depend: $(DEPS)

@echo "Dependencies are now up-to-date."

#d文件内容导入makefile

-include $(DEPS)

特点:

Ø 没有出现任何一个具体的文件名

Ø 采用依赖性文件确定依赖关系,并最后通过include将依赖关系导入

Ø 最终的二进制程序,不仅仅依赖于o文件,还依赖于d文件

具体见example4

7、 级联嵌套makefile

# Lines starting with the pound sign are comments.

#

# This is one of two options you might need to tweak.

EXECUTABLE = myprogram

# You can modify the below as well, but probably

# won't need to.

#

# CC is for the name of the C compiler. CPPFLAGS denotes pre-processor

# flags, such as -I options. CFLAGS denotes flags for the C compiler.

# CXXFLAGS denotes flags for the C++ compiler. You may add additional

# settings here, such as PFLAGS, if you are using other languages such

# as Pascal.

CC = gcc

CPPFLAGS =

CFLAGS = -Wall -O2

CXXFLAGS = $(CFLAGS)

COMPILE = $(CC) $(CPPFLAGS) $(CFLAGS) -c

SRCS := $(wildcard *.c)

OBJS := $(patsubst %.c,%.o,$(SRCS))

DEPS := $(patsubst %.c,%.d,$(SRCS))

DIRS= input format

# "all" is the default target. Simply make it point to myprogram.

all: $(EXECUTABLE)

subdirs:

@for dir in $(DIRS); do $(MAKE) -C $$dir; done

# Define the components of the program, and how to link them together.

# These components are defined as dependencies; that is, they must be

# made up-to-date before the code is linked.

$(EXECUTABLE): subdirs $(DEPS) $(OBJS)

$(CC) -o $(EXECUTABLE) $(OBJS)

# Specify that the dependency files depend on the C source files.

%.d: %.c

$(CC) -M $(CPPFLAGS) $< > $@

$(CC) -M $(CPPFLAGS) $< | sed s///.o/.d/ >> $@

# Specify that all .o files depend on .c files, and indicate how

# the .c files are converted (compiled) to the .o files.

%.o: %.c

$(COMPILE) -o $@ $<

clean:

-rm $(OBJS) $(EXECUTABLE) $(DEPS) *~

@for dir in $(DIRS); do $(MAKE) -C $$dir clean; done

explain:

@echo "The following information represents your program:"

@echo "Final executable name: $(EXECUTABLE)"

@echo "Source files: $(SRCS)"

@echo "Object files: $(OBJS)"

@echo "Dependency files: $(DEPS)"

@echo "Subdictionary: $(DIRS)"

depend: $(DEPS)

@for dir in $(DIRS); do $(MAKE) -C $$dir; done

@echo "Dependencies are now up-to-date."

-include $(DEPS)

这是顶层makefile相对于标准智能模板进行的修改,inputformat中的makefile为标准的模块。

分享到:
评论

相关推荐

    Linux下makefile教程

    Linux下Makefile教程,结合实例详细介绍Linux下的Makefile编写规则和技巧,欢迎下载。

    makefile_patterns:可移植makefile的技巧和想法(Linux和BSD)

    Makefile模式当几个BSD用户尝试我的一个项目时告诉我他们无法构建我的项目时,我只是在想一些更高级的Gnu制作技巧。这个缺点激发了我重新思考自己的方法。那么,这个项目就是我记录成功想法的地方,以便以后找到它们...

    跟我一起学Makefile

    Makefile linux 帮助你快速掌握Makefile编写规则与技巧,

    makefile编写详解

    文档中详细讲解了makefile组织结构和编写技巧,阅读后便可对makefile有一个全面的了解,不仅能够很好理解makefile,还能编写简单实用的makefile。

    Makefile,跟我一起学习makefile

    linux中学习makefile;掌握makefile基本语法和一些常用技巧;

    跟我一起写Makefile.pdf

    附带一些多目录makefile编写技巧

    makefile教程

    makefile教程,主要介绍makefile的基本概念和相关的使用技巧

    makefile的快速学习文档

    详细的讲解makefile的资料,快速全面掌握使用技巧和方法

    jiqiao.rar_linux编程_makefile_makefile 编写

    linux编程小技巧,linux下C语言编程,makefile编写

    跟我一起写makefile

    很详细的makefile编写指南。其中涵盖各种编写技巧和规则。

    linux实验五 MAKEFILE工程管理

    综合运用Makefile工程管理的技巧操作。 1.3步骤及现象 Makefile 工程管理 1、实验要求: 使用Makefile完成对一个含有多个文件的程序的编译。 2、实验器材: 软件: 安装了ubuntu的VMware虚拟机。 硬件: ...

    GNU-Makefile中文手册-Ver3.8.pdf

    用法、技巧。它会为你在 下的工程开发、工程管理提供非常有用的帮助。 此中文文档当前版本 本文的所有勘误和最新版本可在主 页 上获取!! 谢谢! 徐海兵 年月日 中文于册 第一章:概述 概既述 环境下的程序员如果不会...

    Linux嵌入式开发+C语言+Makefile+网络编程+驱动开发+系统移植

    其中,Linux嵌入式开发部分介绍了如何搭建环境、交叉编译、制作根文件系统等实用技巧,帮助读者快速上手嵌入式开发领域。 而该文档中的C语言部分则详细介绍了基本数据类型、指针、结构体、函数、预处理等内容,并...

    展讯 平台 build 环境

    展讯 平台环境 搭建 Makefile 工作流程 第三方编译选项说明 相关使用技巧等等

    cbp2make_linux

    使用方法: 1.解压在linux文件下 2.用你的codeblock打开里面有个cbp2make.cbp然后编译 3.如果你经常要用就在你的解压文档下打开terminal,然后命令 sudo cp ./bin/Release/cbp2make /usr/local/bin/ 4. 要生成...

    Linux课程设计-手机缴费系统(源码+报告).zip

    在本次课程设计中,设计的目的就是在Linux系统环境下,使用makefile文件的方式,使用C语言编写出手机缴费系统。要求系统必须具有增查...在本次课程设计中,要求同学们掌握使用Linux系统、C语言做系统设计步骤和技巧。

    GNU make中文手册 徐海兵

    作为一个Linux程序员,make工具的使用以及编写Makefile是必需的。系统、详细讲述make的中文资料比较少,出于对广大中文Linuxer的支持,本人在工作之余,花了18个多月时间完成对“info make”的翻译整理,完成这个...

    GNU make中文手册 v1.5

    阅读本文的几点建议: 1. 如果之前你对GNU make 没有了解、当前也不想深入的学习GNU make 的读者。 可只阅读本文各章节前半部分的内容(作为各...技巧。它会为你在Linux 下的工程开发、工程管理提供非常有用的帮助。

Global site tag (gtag.js) - Google Analytics