Linux autotools的使用

autotools系列工具可以帮助用户轻松地生成makefile文件,用户只需输入简单的目标文件、依赖文件、文件目录即可

一个简单的实例

新建两个源文件hello.c  display.c  display.h,其中hello.c中include "display.h",display.c中定义了显示函数display

1、在源文件所在的目录下输入命令autoscan,它会搜索源文件以寻找一般的移植性问题并创建一个文件configure.scan

$ autoscan

$ ls -a

2、将configure.scan修改成configure.ac,并修改内容如下所示

$ mv configure.scan configure.ac

$ vi configure.ac

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT(hello, 1.0, [email protected])
AM_INIT_AUTOMAKE(hello,1.0)
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADERS([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

3、运行命令aclocal,生成 aclocal.m4文件,这个文件主要处理本地的宏定义

$ aclocal

4、运行autoconf,生成configure可执行文件

$ autoconf

5、使用autoheader命令生成config.h.in文件,这个工具通常会从acconfig.h文件中复制用户附加的符号定义,但因为我们这里没附加符号定义,所以不需要创建acconfig.h文件

$ autoheader

6、创建automake要用的脚本配置文件makefile.am

$ vi makefile.am

#AUTOMAKE_OPTIONS设置软件等级,有三种:foreign、gnu、gnits,foreign等级只检测必需的文件

AUTOMAKE_OPTIONS=foreign  
bin_PROGRAMS=hello                          #定义要产生的执行文件名
hello_SOURCES=hello.c display.c display.h   #定义执行目标文件所需的原始文件,用空格隔开

7、用automake命令生成configure .in文件,使用-a选项自动添加必需的脚本文件

$ automake -a

8、运行configure将makefile.in变成最终的makefile

$ ./configure

9、运行make生成可执行目标文件hello

$ make

猜你喜欢

转载自blog.csdn.net/yhl_sophia/article/details/86490103