您的位置:首页 > 运维架构 > Linux

linux使用autotools生成可执行文件、静态库、动态库Makefile的流程介绍

2014-09-24 11:07 836 查看
Linux下,工程管理器make是可用于自动编译、链接程序的实用工具。我们要做的是写一个Makefile文件,然后用make命令来编译、链接程序。Makefile的作用就是让编译器知道要编译一个文件需要依赖其他的哪些文件。

GNU autotools作用:收集系统配置信息并自动生成Makefile文件。

GNU autotools主要包括三个工具:autoconf、automake、libtool。

生成可执行文件:

主要步骤:

1.开发者要书写的文件主要是configure.in和Makefile.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

同一个目录

1.写好源码main.c hello.c hello.h

[root@localhost 1]# ls

hello.c hello.h main.c

[root@localhost 1]# cat main.c

#include <stdio.h>

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 <stdio.h>

#include <string.h>

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.scan hello.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。

==================================================================================================================================

生成静态库:

多个目录下的autotools生成Makefile:

1.设计好目录以及源码(每个目录下都必须配置文件Makefile.am)

一、main.c目录下:

[root@localhost 2]# ls

include main.c Makefile.am src

[root@localhost 2]# cat main.c

#include <stdio.h>

int main(void)

{

printf("main\n");

add(4,2);

mul(4,2);

return 0;

}

[root@localhost 2]# cat Makefile.am

AUTOMAKE_OPTIONS=foreign //软件等级

SUBDIRS=src include //指定需要处理的子目录,如果要处理多个子目录,以空格隔开

CURRENTPATH=$(shell /bin/pwd) //利用pwd取得当前路径

INCLUDES=-I$(CURRENTPATH)/include //头文件路径

export INCLUDES //导出供子目录Makefile.am使用

bin_PROGRAMS=main //指定要产生的可执行文件名

main_SOURCES=main.c //指定产生可执行文件需要的源文件,如果有多个,以空格隔开

main_LDADD=$(CURRENTPATH)/src/libpro.a //连接库,也可以LIBS+=$(CURRENTPATH)/src/libpro.a 增加链接库

二、src目录下:

[root@localhost src]# pwd

/work/autotools/2/src

[root@localhost src]# ls

add.c Makefile.am mul.c

[root@localhost src]# cat add.c

#include <stdio.h>

void add(int a, int b)

{

printf("a+b = %d\n", a+b);

return ;

}

[root@localhost src]# cat mul.c

#include <stdio.h>

void mul(int a, int b)

{

printf("a*b = %d\n", a*b);

return ;

}

[root@localhost src]# cat Makefile.am

noinst_LIBRARIES=libpro.a //src目录下生产静态库libpro.a,以便顶层目录主文件链接使用

libpro_a_SOURCES=add.c mul.c //产生静态库所需要的源文件,以空格隔开

三、include目录下:

[root@localhost include]# ls

add.h Makefile.am mul.h

[root@localhost include]# cat add.h

#ifndef __ADD_H__

#define __ADD_H__

#include <stdio.h>

void add(int a, int b);

#endif

[root@localhost include]# cat mul.h

#ifndef __MUL_H__

#define __MUL_H__

#include <stdio.h>

void mul(int a, int b);

#endif

[root@localhost include]# cat Makefile.am

EXTRA_DIST=mul.h add.h //EXTRA_DIST表示用来定义要额外打包的文件名称

2.执行命令:autoscan

[root@localhost 2]# autoscan

autom4te: configure.ac: no such file or directory

autoscan: /usr/bin/autom4te failed with exit status: 1

[root@localhost 2]# ls

autoscan.log configure.scan include main.c Makefile.am src

3.编辑configure.scan

[root@localhost 1]# vim configure.scan

//只增加这2项:

AM_INIT_AUTOMAKE(main, 1.0)

AC_PROG_RANLIB //库--宏

[root@localhost 1]# mv configure.scan configure.in

4.aclocal

[root@localhost 1]# aclocal

生成aclocal.m4和autom4te.cache 文件

5.autoconf

[root@localhost 1]# autoconf

生成configure脚本

6.autoheader

[root@localhost 1]# autoheader

生成config.h.in

7.automake --add-missing

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

configure.in: installing `./install-sh'

configure.in: installing `./missing'

src/Makefile.am: installing `./depcomp'

生成 depcomp install-sh missing

8.运行configure

[root@localhost 2]# ./configure

生成 Makefile,config.log和config.status

9.make生成main可执行程序

[root@localhost 2]# make

生成main

10.运行可执行程序main

[root@localhost 2]# ./main

main

a+b = 6

a*b = 8

这样,autotools生成Makefile就差不多了

==================================================================================================================================

生成动态库:

(待续)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐