查看: 353|回复: 3

用autotools制作Makefile 和configure文件

 关闭 [复制链接]

签到天数: 2090 天

连续签到: 2 天

[LV.Master]伴坛终老IIII

发表于 2009-5-23 13:01 | 显示全部楼层 |阅读模式
用autotools制作Makefile 和configure文件
目的 从复杂的工作中简化出来。

网上有一些制作Makfile的文章,只停留在Makefile而已。用autotools的工具相对来说要简单的多,其它一些介绍autotools文章又有很多漏洞,而且步骤烦琐。

制作一个最简单的helloworld程序:

现有目录test

mkdir src 建立src目录存放 源代码
在src下。
编辑hello.c文件

代码:
#include <stdio.h>

int main()
{
printf(\"hello world\\n\");
return 0;
}


在src目录下建立Makefile.am文件 (src/Makefile.am)

代码:
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS = hello
hello_SOURCES = hello.c
hello_LDADD = -lpthread (只是测试,实际不需要连接该库)


保存退出

退到test目录

编辑Makefile.am文件 (Makefile.am)

SUBDIRS = src

退出保存

然后
执行
autoscan
生成configure.scan文件

按此编辑此文件

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

AC_PREREQ(2.59)
AC_INIT(hello,1.0, [miaoquan@nou.com.cn])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([src/hello.c])
AC_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.
# FIXME: Replace `main&#39; with a function in `-lpthread&#39;:
AC_CHECK_LIB([pthread], [main])

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

#AC_CONFIG_FILES([Makefile
# src/Makefile])
AC_OUTPUT(Makefile src/Makefile)



退出保存
将此文件更名 mv configure.scan configure.in
然后执行
touch NEWS README AUTHORS ChangeLog

然后执行
autoreconf -fvi

至此生成configure文件
执行configure文件

生成Makefile文件

make
make install
make uninstall
make dist
试验一下吧。

继续完善这个例子,论坛里有人问,如何生成静态库,并连接.

完善hello.c这个例子


当前目录
|- src 目录
|- hello.c 文件
|- include 目录
|- hello.h文件
|- lib 目录
|- test.c文件 此文件用来生成 libhello.a

在当前目录 编写Makefile.am

代码:
SUBDIRS = lib src



在include目录下 编写hello.h


代码:
extern void print(char *);



在lib目录下编写test.c


代码:
#include <stdio.h>

void print(char *msg)
{
printf(\"%s\\n\",msg);
}



在lib目录下编写Makefile.am


代码:
noinst_LIBRARIES=libhello.a
libhello_a_SOURCES=test.c



这里noinst_LIBRARIES 的意思是生成的静态库 ,不会被make install 安装
然后指定libhello.a的源文件test.c


在src目录下编写hello.c


代码:
#include \"hello.h\"

int main()
{
print(\"haha\");//这里是静态库里的print函数
return 0;
}




在src目录下编写Makefile.am

代码:
INCLUDES= -I../include

bin_PROGRAMS=hello
hello_SOURCES=hello.c
hello_LDADD=../lib/libhello.a



首先指定头文件的位置 ../include
然后指定要生成执行文件 hello
然后指定源代码文件 hello.c
最后添加静态库的位置 ../lib/libhello.a


按照我首篇帖子的方式.
执行autoscan 生成configure.scan

修改该文件

按照首篇帖子修改.

然后不同之处
需要添加一行:AC_PROG_RANLIB



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

AC_PREREQ(2.59)
AC_INIT(hello,1.1,[miaoquan@nou.com.cn])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([src/hello.c])
AC_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CC


# Checks for libraries.
AC_PROG_RANLIB
# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

#AC_CONFIG_FILES([Makefile
#lib/Makefile
# src/Makefile])
AC_OUTPUT([Makefile
lib/Makefile
src/Makefile])



mv configure.scan configure.in


touch NEWS README AUTHORS ChangeLog


执行autoreconf -fvi

生成configure.执行configure生成Makefile..


后面同上...
PCOS系统下载站:http://zhuangji.wang

签到天数: 28 天

连续签到: 0 天

[LV.4]偶尔看看III

发表于 2009-5-23 15:47 | 显示全部楼层
学习学习,支持一下。
PCOS系统下载站:http://zhuangji.wang

签到天数: 2090 天

连续签到: 2 天

[LV.Master]伴坛终老IIII

 楼主| 发表于 2009-5-23 13:01 | 显示全部楼层

用autotools制作Makefile 和configure文件

用autotools制作Makefile 和configure文件
目的 从复杂的工作中简化出来。

网上有一些制作Makfile的文章,只停留在Makefile而已。用autotools的工具相对来说要简单的多,其它一些介绍autotools文章又有很多漏洞,而且步骤烦琐。

制作一个最简单的helloworld程序:

现有目录test

mkdir src 建立src目录存放 源代码
在src下。
编辑hello.c文件

代码:
#include <stdio.h>

int main()
{
printf(\"hello world\\n\");
return 0;
}


在src目录下建立Makefile.am文件 (src/Makefile.am)

代码:
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS = hello
hello_SOURCES = hello.c
hello_LDADD = -lpthread (只是测试,实际不需要连接该库)


保存退出

退到test目录

编辑Makefile.am文件 (Makefile.am)

SUBDIRS = src

退出保存

然后
执行
autoscan
生成configure.scan文件

按此编辑此文件

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

AC_PREREQ(2.59)
AC_INIT(hello,1.0, [miaoquan@nou.com.cn])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([src/hello.c])
AC_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.
# FIXME: Replace `main&#39; with a function in `-lpthread&#39;:
AC_CHECK_LIB([pthread], [main])

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

#AC_CONFIG_FILES([Makefile
# src/Makefile])
AC_OUTPUT(Makefile src/Makefile)



退出保存
将此文件更名 mv configure.scan configure.in
然后执行
touch NEWS README AUTHORS ChangeLog

然后执行
autoreconf -fvi

至此生成configure文件
执行configure文件

生成Makefile文件

make
make install
make uninstall
make dist
试验一下吧。

继续完善这个例子,论坛里有人问,如何生成静态库,并连接.

完善hello.c这个例子


当前目录
|- src 目录
|- hello.c 文件
|- include 目录
|- hello.h文件
|- lib 目录
|- test.c文件 此文件用来生成 libhello.a

在当前目录 编写Makefile.am

代码:
SUBDIRS = lib src



在include目录下 编写hello.h


代码:
extern void print(char *);



在lib目录下编写test.c


代码:
#include <stdio.h>

void print(char *msg)
{
printf(\"%s\\n\",msg);
}



在lib目录下编写Makefile.am


代码:
noinst_LIBRARIES=libhello.a
libhello_a_SOURCES=test.c



这里noinst_LIBRARIES 的意思是生成的静态库 ,不会被make install 安装
然后指定libhello.a的源文件test.c


在src目录下编写hello.c


代码:
#include \"hello.h\"

int main()
{
print(\"haha\");//这里是静态库里的print函数
return 0;
}




在src目录下编写Makefile.am

代码:
INCLUDES= -I../include

bin_PROGRAMS=hello
hello_SOURCES=hello.c
hello_LDADD=../lib/libhello.a



首先指定头文件的位置 ../include
然后指定要生成执行文件 hello
然后指定源代码文件 hello.c
最后添加静态库的位置 ../lib/libhello.a


按照我首篇帖子的方式.
执行autoscan 生成configure.scan

修改该文件

按照首篇帖子修改.

然后不同之处
需要添加一行:AC_PROG_RANLIB



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

AC_PREREQ(2.59)
AC_INIT(hello,1.1,[miaoquan@nou.com.cn])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([src/hello.c])
AC_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CC


# Checks for libraries.
AC_PROG_RANLIB
# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

#AC_CONFIG_FILES([Makefile
#lib/Makefile
# src/Makefile])
AC_OUTPUT([Makefile
lib/Makefile
src/Makefile])



mv configure.scan configure.in


touch NEWS README AUTHORS ChangeLog


执行autoreconf -fvi

生成configure.执行configure生成Makefile..


后面同上...
PCOS系统下载站:http://zhuangji.wang

签到天数: 2090 天

连续签到: 2 天

[LV.Master]伴坛终老IIII

 楼主| 发表于 2009-5-23 13:01 | 显示全部楼层

用autotools制作Makefile 和configure文件

用autotools制作Makefile 和configure文件
目的 从复杂的工作中简化出来。

网上有一些制作Makfile的文章,只停留在Makefile而已。用autotools的工具相对来说要简单的多,其它一些介绍autotools文章又有很多漏洞,而且步骤烦琐。

制作一个最简单的helloworld程序:

现有目录test

mkdir src 建立src目录存放 源代码
在src下。
编辑hello.c文件

代码:
#include <stdio.h>

int main()
{
printf(\"hello world\\n\");
return 0;
}


在src目录下建立Makefile.am文件 (src/Makefile.am)

代码:
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS = hello
hello_SOURCES = hello.c
hello_LDADD = -lpthread (只是测试,实际不需要连接该库)


保存退出

退到test目录

编辑Makefile.am文件 (Makefile.am)

SUBDIRS = src

退出保存

然后
执行
autoscan
生成configure.scan文件

按此编辑此文件

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

AC_PREREQ(2.59)
AC_INIT(hello,1.0, [miaoquan@nou.com.cn])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([src/hello.c])
AC_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.
# FIXME: Replace `main&#39; with a function in `-lpthread&#39;:
AC_CHECK_LIB([pthread], [main])

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

#AC_CONFIG_FILES([Makefile
# src/Makefile])
AC_OUTPUT(Makefile src/Makefile)



退出保存
将此文件更名 mv configure.scan configure.in
然后执行
touch NEWS README AUTHORS ChangeLog

然后执行
autoreconf -fvi

至此生成configure文件
执行configure文件

生成Makefile文件

make
make install
make uninstall
make dist
试验一下吧。

继续完善这个例子,论坛里有人问,如何生成静态库,并连接.

完善hello.c这个例子


当前目录
|- src 目录
|- hello.c 文件
|- include 目录
|- hello.h文件
|- lib 目录
|- test.c文件 此文件用来生成 libhello.a

在当前目录 编写Makefile.am

代码:
SUBDIRS = lib src



在include目录下 编写hello.h


代码:
extern void print(char *);



在lib目录下编写test.c


代码:
#include <stdio.h>

void print(char *msg)
{
printf(\"%s\\n\",msg);
}



在lib目录下编写Makefile.am


代码:
noinst_LIBRARIES=libhello.a
libhello_a_SOURCES=test.c



这里noinst_LIBRARIES 的意思是生成的静态库 ,不会被make install 安装
然后指定libhello.a的源文件test.c


在src目录下编写hello.c


代码:
#include \"hello.h\"

int main()
{
print(\"haha\");//这里是静态库里的print函数
return 0;
}




在src目录下编写Makefile.am

代码:
INCLUDES= -I../include

bin_PROGRAMS=hello
hello_SOURCES=hello.c
hello_LDADD=../lib/libhello.a



首先指定头文件的位置 ../include
然后指定要生成执行文件 hello
然后指定源代码文件 hello.c
最后添加静态库的位置 ../lib/libhello.a


按照我首篇帖子的方式.
执行autoscan 生成configure.scan

修改该文件

按照首篇帖子修改.

然后不同之处
需要添加一行:AC_PROG_RANLIB



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

AC_PREREQ(2.59)
AC_INIT(hello,1.1,[miaoquan@nou.com.cn])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([src/hello.c])
AC_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CC


# Checks for libraries.
AC_PROG_RANLIB
# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

#AC_CONFIG_FILES([Makefile
#lib/Makefile
# src/Makefile])
AC_OUTPUT([Makefile
lib/Makefile
src/Makefile])



mv configure.scan configure.in


touch NEWS README AUTHORS ChangeLog


执行autoreconf -fvi

生成configure.执行configure生成Makefile..


后面同上...
PCOS系统下载站:http://zhuangji.wang

本版积分规则