Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2707165
  • 博文数量: 505
  • 博客积分: 1552
  • 博客等级: 上尉
  • 技术积分: 2514
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-23 18:24
文章分类

全部博文(505)

文章存档

2019年(12)

2018年(15)

2017年(1)

2016年(17)

2015年(14)

2014年(93)

2013年(233)

2012年(108)

2011年(1)

2009年(11)

分类: LINUX

2012-12-24 23:09:18

Linux下,工程管理器make是可用于自动编译、链接程序的实用工具。我们要做的是写一个Makefile文件,然后用make命令来编译、链接程序。Makefile的作用就是让编译器知道要编译一个文件需要依赖其他的哪些文件。
    GNU autotools作用:收集系统配置信息并自动生成Makefile文件。
    GNU autotools主要包括三个工具:autoconf、automake、libtool。

 

主要步骤:
1.开发者要书写的文件主要是configure.inMakefile.am
2.运行autoscan检测源文件生成configure.scan并修改成configure.in
3.编辑configure.in
4.由aclocal命令生成aclocal.m4
5.运行autoconf生成configure脚本
6.运行autoheader生成config.h.in文件
7.创建并编辑Makfile.am
8.运行automake生成makefile.in  有时候要加automake --add-missing,这很重要哦生气
9.运行configure脚本生成Makefile
10.make
11.运行可执行程序main
 

简单-同一个目录(不同目录见《autotools生成Makefile(二)》)

1.写好源码main.c hello.c hello.h
[root@localhost 1]# ls
hello.c  hello.h  main.c

[root@localhost 1]# cat main.c
#include
int main(void)
{
        printf("main start!\n");
        print();

        return 0;
}

[root@localhost 1]# cat hello.c
#include "hello.h"
void print(void)
{
        char buf[32] = "123";
        strcat(buf, "456");
        printf("%s\n", buf);
        printf("hello,world!\n");

        return ;
}

[root@localhost 1]# cat hello.h
#ifndef __HELLO_H__
#define __HELLO_H__
#include
#include
void print(void);
#endif

2.执行命令:autoscan
[root@localhost 1]# autoscan
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1
[root@localhost 1]# ls
autoscan.log  configure.scahello.c  hello.h  main.c

3.编辑configure.scan
[root@localhost 1]# vim configure.scan
//只增加这2项:
AM_INIT_AUTOMAKE(main, 1.0)
AC_OUTPUT(Makefile)
[root@localhost 1]# mv configure.scan configure.in

4.aclocal
[root@localhost 1]# aclocal
[root@localhost 1]# ls
aclocal.m4  autoscan.log  hello.c  main.c  autom4te.cache  configure.in  hello.h

5.autoconf
[root@localhost 1]# autoconf
[root@localhost 1]# ls
aclocal.m4 autoscan.log  configure.in  hello.h  autom4te.cache  configure  hello.c   main.c

6.autoheader
[root@localhost 1]# autoheader
[root@localhost 1]# ls
aclocal.m4 autoscan.log  configure hello.c  main.c  autom4te.cache  config.h.in   configure.in  hello.h

7.用vi编辑makefile.am文件,编辑如下,编辑完后保存退出
[root@localhost 1]# vi Makefile.am  //注意:不是makefile-即M应为大写
AUTOMAKE_OPTIONS= foreign //软件等级
bin_PROGRAMS= main //可执行文件名
main_SOURCES= main.c hello.c  //源文件名

8.automake + automake --add-missing
[root@localhost 1]# automake
configure.in: required file `./install-sh' not found
configure.in: required file `./missing' not found
Makefile.am: required file `./depcomp' not found
[root@localhost 1]# automake --add-missing
configure.in: installing `./install-sh'
configure.in: installing `./missing'
Makefile.am: installing `./depcomp'
[root@localhost 1]# ls
 depcomp  install-sh  missing

9.运行configure
   生成 Makefile config.log  config.status

10.make生成可执行文件main

11.运行main
[root@localhost 1]# ./main
main start!
123456
hello,world!

 

需要编辑的二个主要文件

configure.in :

AC_PREREQ(2.59)
AC_INIT(hello,1.0,yangzongde@163.com)    //在此行内容中设置当前软件包信息
AM_INIT_AUTOMAKE(main,1.0)      //automake 所必备的宏,必须添加
AC_CONFIG_SRCDIR([hello.c])      //源文件名
AC_CONFIG_HEADER([config.h])      //config 文件
# 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_OUTPUT(Makefile)        //输出文件名为 makefile

分析:

(1)AC_PREREQ 宏声明本文件要求的 autoconf 版本,本例使用的版本为 2.59。
(2)AC_INIT 宏用来定义软件的名称和版本等信息,"FULL-PACKAGE-NAME"为软件包名称,"VERSION"为软件版本号,"BUG-REPORT-ADDRESS"为 BUG 报告地址 (一般为软件作者邮件地址)。
(3)AC_CONFIG_SRCDIR 宏用来侦测所指定的源码文件是否存在,来确定源码目录的有效性。此处为当前目录下的 hello.c。 
(4)AC_CONFIG_HEADER 宏用于生成 config.h 文件,以便 autoheader 使用。 
(5)AC_PROG_CC 用来指定编译器,如果不指定,选用默认 gcc。
(6)AC_OUTPUT 用来设定configure 所要产生的文件,如果是 makefile,configure 会把它检查出来的结果带入 makefile.in 文件产生合适的 makefile。
使用 Automake 时,还需要一些其他的参数,这些额外的宏用 aclocal 工具产生。 中间的注释可以分别添加用户测试程序、测试函数库和测试头文件等宏定义。
此文件只是下面要使用的 configure.ac 文件的原型,要使用此文件,还需要根据情况修改相关内容。     
  (7)AM_INIT_AUTOMAKE(PACKAGE,VERSION) 这个是使用 Automake 所必备的宏,PACKAGE 是所要产生软件套件的名称,VERSION 是版本编号。

   

Makefile.am
         Automake 会根据 configure.in 中的宏把Makefile.am 转成 Makefile.in 文件。 Makefile.am 文件定义所要产生的目标:
AUTOMAKE_OPTIONS
    设置 automake 的选项。
       Automake 主要是帮助开发 GNU 软件的人员来维护软件,所以在执行 automake 时,会检查目录下是否存在标准 GNU 软件中应具备的文件,例如 'NEWS'、'AUTHOR'、'ChangeLog' 等文件。设置 foreign 时,automake 会改用一般软件的标准来检查。
bin_PROGRAMS
    定义要产生的执行文件名。如果要产生多个执行文件,每个文件名用空白符隔开。
main_SOURCES
    定义 'hello' 这个执行程序所需要的原始文件。如果 'hello'这个程序是由多个原始文件所产生,必須把它所用到的所有原始文件都列出来,以空白符隔开。假设 'hello' 还需要 'hello.c'、'main.c'、'hello.h' 三个文件的话,则定义
hello_SOURCES= hello.c main.c hello.h
如果定义多个执行文件,则对每个执行程序都要定义相对的filename_SOURCES。
阅读(1399) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~