您的位置:首页 > 其它

autotools生成Makefile的步骤(非转载,经过实机测试)

2012-12-01 15:12 302 查看

GNU autotools工具介绍

Linux下,工程管理器
Make 可用于自动编译、链接程序的实用工具。我们要做的是写一个makefile 文件,然后用make命令来编译、链接程序。

Makefile的作用就是让编译器知道要编译一个文件需要依赖其他的哪些文件。这里我们就是要用GNU Autotools来收集系统配置信息并自动生成Makefile文件。

GNU Autotools指的就是下面的五个工具:



(1)aclocal

(2)autoscan

(3)autoconf

(4)autoheader

(5)automake

GUN autotools工具使用步骤

步骤1:编写源码文件。

[root@localhost home]# vi hello.c

内容如下:

#include"hello.h"

int main()

{

printf("Hello world!\n");

}

[root@localhost home]# vi hello.h

内容如下:

#include<stdio.h>

步骤2:在当前目录下建立auto目录。

命令:

[root@localhost home]# mkdir auto

步骤3:把hello文件复制到auto目录下。

命令:

[root@localhost home]# cp hello.* ./auto

步骤4:输入命令autoscan。

命令:

[root@localhost home]# cd auto

[root@localhost auto]# autoscan

此时生成了2个文件 ,分别是 autoscan.log configure.scan

Autoscan会在给定的目录及其子目录树中检查源文件,若没有给出目录,就在当前目录及其子目录树中进行检查。它会搜索源文件以寻找一般的移植性问题并创建一个文件configure.scan

步骤5:用vi编写configure.scan,并保存命名为configure.in,configure.in是autoconf的脚本配置文件。

命令:

[root@localhost auto]# vi configure.scan

修改内容如下:

# -*- Autoconf -*-

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

AC_PREREQ(2.57)

AC_INIT(hello,1.0)

AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)

AM_INIT_AUTOMAKE

AC_PROG_RANLIB 这两行需要手动添加

AC_CONFIG_SRCDIR([hello.c])

AM_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])--------------------指定生成Makefile。如果没有该行,请手动添加。

AC_OUTPUT下面对这个脚本文件进行解释:

AC_PREREQ宏声明本文件要求的autoconf版本,本例使用的版本为2.57

AC_INIT宏用来定义软件的名称和版本等信息

AM_INIT_AUTOMAKE是笔者另加的,它是automake所必备的宏。

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

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

步骤6:运行命令aclocal

命令:

[root@localhost auto]# aclocal

生成文件aclocal.m4,该文件主要处理本地的宏定义。

步骤7:运行autoconf

命令:

[root@localhost auto]# autoconf

生成2个文件:分别为autom4te.cache configure

步骤8:运行autoheader

命令:

[root@localhost auto]# autoheader

生成config.h.in

步骤9:用vi编辑Makefile.am

[root@localhost auto]# vi Makefile.am

内容如下:

AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS=hello

hello_SOURCES=hello.c hello.h

步骤10:运行automake

命令:

[root@localhost auto]# automake --add-missing

可以让automake自动添加一些必需的脚本文件。

生成configure depcomp install-sh missing mkinstalldirs

步骤11:运行./configure

命令:

[root@localhost auto]# ./configure

显示内容如下

[root@localhost auto]# ./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 ranlib... ranlib

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------------------------如果没有该行请检查configure.in文件是否存在AC_CONFIG_FILES([Makefile])

config.status: creating config.h

config.status: config.h is unchanged

config.status: executing depfiles commands

步骤12:运行make

命令:

[root@localhost auto]# make

步骤13:执行./hello

命令:

[root@localhost auto]# ./hello

显示结果如下:

Hello world!

步骤14:运行make install

命令:

[root@localhost auto]# make install

把程序安装到系统目录下。

[root@localhost auto]# hello

Hello world!

步骤15:运行make dist

命令:

[root@localhost auto]# make dist

打包

步骤16:运行make clean

命令:

[root@localhost auto]# make clean

显示如下:

test -z "hello" || rm -f hello

rm -f *.o core *.core

此时.o文件及可执行文件都会删除,也就是说hello及hello.o文件都删除了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: