linux下makefile编写及automake整理

makefile编写规则

在一个makefile中通常包含如下内容:

1 需要由make工具创建的目标体(target),通常是目标文件或可执行文件

2 要创建的目标体所依赖的文件(dependency_file)

3 创建每个目标体时需要运行的命令(command),这一行必须以制表符(tab键)开头

格式:

target: dependency_files

command /* 该行必须以tab键开头*/

例如,有两个文件分别为hello.c和hello.h,创建的目标体为hello.o,执行的命令为gcc编译指令:gcc –c hello.c,那么,对应的makefile就可以写为:

#The simplest example

hello.o: hello.c hello.h

gcc –c hello.c –o hello.o

执行:

[[email protected] makefile]# make hello.o

gcc –c hello.c –o hello.o

[[email protected] makefile]# ls

hello.c hello.h hello.o makefile

makefile变量

用户自定义变量:

makefile变量定义的两种方式:(使用格式为:$(VAR))

递归展开方式:能很好的完成用户指令,但变量后不可追加内容,可能造成无穷循环:定义格式:VAR=var

简单方式:变量的值在定义处展开,并且只展开一次,不包含任何对其他变量的引用:定义格式:VAR:=var

变量名:

变量名大小写敏感

推荐在makefile内部使用小写字母作为变量名,预留大写字母作为控制隐含规则参数或用户重载命令选项参数的变量名。

不包括“:”、“#”、“=”以及结尾空格的任何字符串,同时,变量名中包含字母、数字以及下划线以外的情况应尽量避免,因为它们可能在将来被赋予特别的含义

常见预定义变量:

AR       库文件维护程序的名称,默认值为ar

AS       汇编程序的名称,默认值为as

CC       C编译器的名称,默认值为cc

CPP      C预编译器的名称,默认值为$(CC) –E

CXX      C++编译器的名称,默认值为g++

FC       Fortran编译器的名称,默认值为f77

RM       文件删除程序的名称,默认值为rm –f

ARFLAGS  库文件维护程序的选项,无默认值

ASFLAGS  汇编程序的选项,无默认值

CFLAGS   C编译器的选项,无默认值

CPPFLAGS C预编译的选项,无默认值

CXXFLAGS C++编译器的选项,无默认值

FFLAGS   Fortran编译器的选项,无默认值

常见的自动变量:

[email protected]    目标文件的完整名称

$*    不包含扩展名的目标文件名称

$<    第一个依赖文件的名称

$%    如果目标是归档成员,则该变量表示目标的归档成员名称

$+    所有的依赖文件,以空格分开,并以出现的先后为序,可能包含重复的依赖文件

$?    所有时间戳比目标文件晚的依赖文件,并以空格分开

$^    所有不重复的依赖文件,以空格分开

在makefile中还可以使用环境变量。使用环境变量的方法相对比较简单,make 在启动时会自动读取系统当前已经定义了的环境变量,并且会创建与之具有相同名称和数值的变量。用户自定义变量会覆盖同名的环境变量。

makefile规则

隐式规则:只需把目标文件列出即可,make 会自动搜索隐式规则目录来确定如何生成目标文件,它只能查找到相同文件名的不同后缀名文件,如“kang.o”文件必须由“kang.c”文件生成

makefile中常见隐式规则目录

C编译:      .c变为.o $(CC) –c $(CPPFLAGS) $(CFLAGS)

C++编译:    .cc或.C变为.o $(CXX) -c $(CPPFLAGS) $(CXXFLAGS)

Pascal编译: .p变为.o $(PC) -c $(PFLAGS)

Fortran编译:.r变为-o $(FC) -c $(FFLAGS)

模式规则:用来定义相同处理规则的多个文件,隐式规则仅仅能够用make 默认的变量来进行操作,而模式规则还能引入用户自定义变量,为多个文件建立相同的规则,实用模式规则时相关文件前必须用“%”标明

例如:

david:kang.o yul.o

gcc kang.o bar.o -o myprog

kang.o : kang.c kang.h head.h

gcc –Wall –O -g –c kang.c -o kang.o

yul.o : bar.c head.h

gcc - Wall –O -g –c yul.c -o yul.o

修改后:(递归展开方式)

OBJS = kang.o yul.o

CC = gcc

CFLAGS = -Wall -O -g

david : $(OBJS)

$(CC) $(OBJS) -o david

kang.o : kang.c kang.h

$(CC) $(CFLAGS) -c kang.c -o kang.o

yul.o : yul.c yul.h

$(CC) $(CFLAGS) -c yul.c -o yul.o

改写后:(自动变量)

OBJS = kang.o yul.o

CC = gcc

CFLAGS = -Wall -O -g

david : $(OBJS)

$(CC) $^ -o [email protected]

kang.o : kang.c kang.h

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

yul.o : yul.c yul.h

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

修改后:(隐式规则)

OBJS = kang.o yul.o

CC = gcc

CFLAGS = -Wall -O -g

david : $(OBJS)

$(CC) $^ -o [email protected]

改写后:(模式规则)

OBJS = kang.o yul.o

CC = gcc

CFLAGS = -Wall -O -g

david : $(OBJS)

$(CC) $^ -o [email protected]

%.o : %.c

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

make命令的使用

只需在make命令的后面键入目标名即可建立指定的目标,如果直接运行make,则建立makefile中的第一个目标

make的命令行选项

-C dir 读入指定目录下的makefile

-f file 读入当前目录下的file文件作为makefile

-I 忽略所有的命令执行错误

-I dir 指定被包含的makefile所在目录

-n 只打印要执行的命令,但不执行这些命令

-p 显示make变量数据库和隐含规则

-s 在执行命令时不显示命令

-w 如果make在执行过程中改变目录,则打印当前目录名

autotools

生成makefile 同时又能拥有make的功能

which命令进行查看aclocal autoscan autoconf autoheader automake是否安装

vim hello.c
#include <stdio.h>
int sum(int);
int main(){
    printf("the sum of 1-50 is %d\n",sum(50));
}
int sum(int n){
    int i,sum;
    for(i = 1,sum = 0; i <= n; ++i){
        sum += i;
    }
    return sum;
}

流程:

1.autoscan

在给定目录及其子目录树中检查源文件,若没有给出目录,就在当前目录及其子目录树中进行检查。它会搜索源文件以寻找一般的移植性问题并创建一个文件"configure.scan",该文件就是接下来autoconf 要用到的"configure.in"原型

[[email protected] makefile]# ll

总用量 4

-rw-r--r-- 1 root root 207 4月  30 10:15 hello.c

[[email protected] automake]# autoscan

[[email protected] automake]# ll

总用量 8

-rw-r--r-- 1 root root   0 4月  30 10:18 autoscan.log

-rw-r--r-- 1 root root 467 4月  30 10:18 configure.scan

-rw-r--r-- 1 root root 207 4月  30 10:15 hello.c

2.修改configure.scan

configure.in是autoconf的脚本配置文件,它的原型文件"configure.scan"

# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.63)
#AC_INIT(FULL-PACKAGE-NAME,VERSION,BUG-REPORT-ADDRESS)
AC_INIT(hello,1.0)
AM_INIT_AUTOMAKE(hello,1.0)
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADER([config.h])
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_CONFIG_FILES([makefile])
AC_OUTPUT

脚本文件解释:

1 以"#"号开始的行是注释。

2 AC_PREREQ 宏声明本文件要求的autoconf版本,如本例使用的版本2.59。

3 AC_INIT 宏用来定义软件的名称和版本等信息,在本例中省略了BUG-REPORT-ADDRESS,一般为作者的E-mail。

4 AM_INIT_AUTOMAKE 是automake 所必备的宏,使automake 自动生成makefile.in,也同前面一样,PACKAGE 是所要产生软件套件的名称,VERSION 是版本编号,也可不带参数直接写成AM_INIT_AUTOMAKE。

5 AC_CONFIG_SRCDIR宏用来检查所指定的源码文件是否存在,以及确定源码目录的有效性。在此处源码文件为当前目录下的hello.c。

6 AC_CONFIG_HEADER宏用于生成config.h文件,以便autoheader 使用。

7 AC_CONFIG_FILES宏用于生成相应的makefile文件。

8 中间的注释之间可以分别添加用户测试程序、测试函数库、测试头文件等宏定义。

改写"configure.scan"文件,并重命名为"configure.in"

3.autoheader

接着使用autoheader 命令,它负责生成config.h.in 文件。该工具通常会从"acconfig.h"文件中复制用户附加的符号定义,因为这里没有附加符号定义,所以不需要创建"acconfig.h"文件

4 aclocal

使用aclocal生成一个"aclocal.m4"文件,该文件主要处理本地的宏定义

5.automake

这一步是创建makefile 很重要的一步,automake 要用的脚本配置文件是makefile.am,用户需要自己创建相应的文件。之后,automake工具转换成makefile.in。

AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS= hello
hello_SOURCES= hello.c

解释:

1 AUTOMAKE_OPTIONS 为设置automake 的选项。GNU 对自己发布的软件有严格的规范,比如必须附带许可证声明文件COPYING 等,否则automake执行时会报错。automake提供了3种软件等级:foreign、gnu和gnits,让用户选择采用,默认等级为gnu。在本示例中采用foreign等级,它只检测必须的文件。

2 bin_PROGRAMS  定义要产生的执行文件名。如果要产生多个执行文件,每个文件名用空格隔开。

3 hello_SOURCES  定义"hello"这个执行程序所需要的原始文件。如果"hello"这个程序是由多个原始文件所产生的,则必须把它所用到的所有原始文件都列出来,并用空格隔开。例如:若目标体"hello"需要"hello.c"、"david.c"、"hello.h"三个依赖文件,则定义hello_SOURCES=hello.c david.c hello.h。要注意的是,如果要定义多个执行文件,则对每个执行程序都要定义相应的file_SOURCES

接下来可以使用automake命令来生成"configure.in"文件,在这里使用选项"-a"(或者"—adding-missing")可以让automake 自动添加一些必需的脚本文件

[[email protected] automake]# vim makefile.am

[[email protected] automake]# automake –a(或者automake --add-missing)

configure.in: installing ‘./install-sh‘

configure.in: installing ‘./missing‘

makefile.am: installing ‘depcomp‘

[[email protected] automake]# ls

aclocal.m4 autoscan.log configure.in hello.c makefile.am missing

autom4te.cache configure depcomp install-sh makefile.in config.h.in

在automake之后就生成了makefile.in文件

6 autoconf

运行autoconf,生成"configure"可执行文件

7 运行configure

运行自动配置设置文件configure,把makefile.in变成了最终的makefile

运行configure 时收集了系统的信息,用户可以在configure 命令中对其进行配置。

./configure的自定义参数有两种,一种是开关式(--enable-XXX或--disable-XXX),另一种是开放式,即后面要填入一串字符(--with-XXX=yyyy)参数。

同一目录下的"config.log"文件,可以方便调试。

大体流程图:

后续工作:

autotools所生成的makefile

1 make  生成可执行文件

2 make  install安装到系统目录中

3 make clean  make会清除之前所编译的可执行文件及目标文件(object file, *.o)

4 make dist  make将程序和相关的文档打包为一个压缩文档以供发布

5 制作debian或rmp程序包(后续)

示例步骤:

[[email protected] automake]# autoscan
[[email protected] automake]# ls
autoscan.log  configure.scan  hello.c

[[email protected] automake]# vim configure.scan
[[email protected] automake]# mv configure.scan configure.in
[[email protected] automake]# ls
autoscan.log  configure.in  hello.c

[[email protected] automake]# autoheader
[[email protected] automake]# ls
autom4te.cache  autoscan.log  config.h.in  configure.in  hello.c

[[email protected] automake]# aclocal
[[email protected] automake]# ls
aclocal.m4  autom4te.cache  autoscan.log  config.h.in  configure.in  hello.c

[[email protected] automake]# vim makefile.am
[[email protected] automake]# automake -a
configure.in:6: installing `./install-sh'
configure.in:6: installing `./missing'
makefile.am: installing `./depcomp'
[[email protected] automake]# ls
aclocal.m4  autom4te.cache  autoscan.log  config.h.in  configure.in  depcomp  hello.c  install-sh  makefile.am  makefile.in  missing

[[email protected] automake]# autoconf
[[email protected] automake]# ls
aclocal.m4  autom4te.cache  autoscan.log  config.h.in  configure  configure.in  depcomp  hello.c  install-sh  makefile.am  makefile.in  missing

[[email protected] automake]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
configure: creating ./config.status
config.status: creating makefile
config.status: creating config.h
config.status: executing depfiles commands

[[email protected] automake]# ls
aclocal.m4      autoscan.log  config.h.in  config.status  configure.in  hello.c     makefile     makefile.in  stamp-h1
autom4te.cache  config.h      config.log   configure      depcomp       install-sh  makefile.am  missing

[[email protected] automake]# cat makefile

时间: 2024-08-07 08:25:23

linux下makefile编写及automake整理的相关文章

浅谈Linux下Makefile编写

浅谈Linux下Makefile的编写 前言:本文简要介绍Makefile文件的编写规范,结合具体项目中的应用进行讲解. 具体代码地址: https://github.com/AnSwErYWJ/DogFood/blob/master/Makefile 简介 Make工具最主要也是最基本的功能就是通过makefile文件来描述源程序之间的相互关系并自动维护编译工作.而makefile 文件需要按照某种语法进行编写,文件中需要说明如何编译各个源文件并连接生成可执行文件,并要求定义源文件之间的依赖关

linux 下shell 编写脚本

linux 下shell 编写脚本: 1.程序结构练习:编写一个脚本,给定一个正整数,计算出这个数所有位的数字之和. 例如:程序给定输入123,那么应该返回1+2+3=6. 2.程序结构练习:编写一个脚本,给定一个正整数,反序输出这个这个数. 例如:程序输入123,那么应该输出321. 3.Linux命令+shell编程:编写一个程序,实现定时文件备份的功能. 要求: 1.备份某个目录下的所有文件 2.对备份文件进行压缩 3.生成合理的日志文件 4.Shell下的gui(libnotify-bi

Linux下Makefile的automake生成全攻略

作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便.一般情况下,大家都是手工写一个简单Makefile,如果要想写出一个符合自由软件惯例的Makefile就不那么容易了. 在本文中,将给大家介绍如何使用 autoconf和automake两个工具来帮助我们自动地生成符合自由软件惯例的Makefile,这样就可以象常见的GNU程序一样,只要使用“./configure”,“make”,“make instal”就可以把程序安装到Linux

3.android下Makefile编写规范

随着移动互联网的发展,移动开发也越来越吃香了,目前最火的莫过于android,android是什么就不用说了,android自从开源以来,就受到很多人的追捧.当然,一部人追捧它是因为它是Google开发的.对一个程序员来说,一个系统值不值得追捧得要拿代码来说话.我这里并不打算分析android的代码,而是android的makefile,也许大家已经知道了在android源码里,我们可以看见很多makefile文件,起初我也不明白,经过一段时间的研究,后来慢慢明白了,我想通过分析andorid的

Linux下MakeFile初探

make是linux下的编译命令,用于编译和生成Linux下的可执行文件.这个命令处理的对象是Makefile,makefile等.由于make的强大解析能力,makefile文件的编写也变得极为简单. 以下是一个makefile的例子 1 objects = main.o kbd.o command.o display.o 2 insert.o search.o files.o utils.o 3 4 edit : $(objects) 5 gcc -o edit $(objects) 6 7

Linux下makefile教程

转自陈皓 (CSDN)概述--什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和 professional的程序员,makefile还是要懂.这就好像现在有这么多的HTML的编辑器,但如果你想成为一个专业人士,你还是要了解HTML的标识的含义.特别在Unix下的软件编译,你就不能不自己写makefile了,会不会写makefile,从一个侧面说明了一个人是否具备完成大型工程的能力.因为,makefile

Linux下高效编写Shell——shell特殊字符汇总

Linux下无论如何都是要用到shell命令的,在Shell的实际使用中,有编程经验的很容易上手,但稍微有难度的是shell里面的那些个符号,各种特殊的符号在我们编写Shell脚本的时候如果能够用的好,往往能给我们起到事半功倍的效果,为此,特地将Shell里面的一些符号说明罗列成对照表的形式,以便快速的查找.看看你知道下表中哦你的哪些Shell符号呢? Shell符号及各种解释对照表: Shell符号 使用方法及说明 # 注释符号(Hashmark[Comments]) 1.在shell文件的行

linux下执行sql脚本链接整理

linux下执行sql脚本        :http://blog.csdn.net/huoyunshen88/article/details/11556951

linux之Makefile 编写、规则、伪目标

什么是Makefile?首先,make是一个工具程序(Utility software),是一种控制编译或者重复编译软件的工具:make可以自动管理软件编译的内容.方式和时机从而使程序员能够把精力集中在编写代码上.那make怎样工作呢?其实make是通过makefile文件实现的.makefile是一个文本形式的数据库文件.其中包含一些规则,这些规则告诉make需要编译哪些文件.怎样编译这些文件以及在什么样的条件下去编译. 关于Makefile的基本规则: target: dependency