C语言用GNU源码编译建构系统工具(GNU BUILD SYSTEM)编译创建源码包
C语言用GNU源码编译建构系统工具(GNU BUILD SYSTEM)编译创建源码包
GNU源码统建构系统工具AUTOTOOLS
- Automake 参考手册
- Autoconf 参考手册
- Libtool 参考手册
- 系统未安装以上工具的,需要安装!!!
创建工作目录
- 创建工作目录 mkdir ab,进入目录 cd ab,开始工作!
- 编辑源码文件 hello.c
代码如下:
/* filename : hello.c */
#include <stdio.h>
/**/
int
main (int argc, char *argv[])
{
printf ("Hello world!\n");
return 0;
}
/* --(:-)-- */
- 编辑Makefile.am文件
内容如下:
bin_PROGRAMS = hello
hello_SOURCE = hello.c
运行命令 autoscan,生成配置文件 configure.scan
如下所示:
songvm@ubuntu:~/works/xdn/aoo/ab$ ls
hello.c Makefile.am
songvm@ubuntu:~/works/xdn/aoo/ab$ autoscan
songvm@ubuntu:~/works/xdn/aoo/ab$ ls
autoscan.log configure.scan hello.c Makefile.am
songvm@ubuntu:~/works/xdn/aoo/ab$
将 configure.scan 改名为 configure.ac
- 改名命令: mv configure.scan configure.ac
- configure.scan文件内容如下:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
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
修改源码包名字、版本、出错报告邮件地址,加入AUTOMAKE相关配置
- FULL-PACKAGE-NAME 源码包名字全称
- VERSION 源码包版本号
- BUG-REPORT-ADDRESS 报错邮件地址
- 加入AUTOMAKE配置,AM_INIT_AUTOMAKE
- -Wall -Werror foreign 表示输出全部出错信息,定义源码包为外部包(非GNU系统内部的,如果是内部的要加入相关说明和开源声明等信息文件)!!!
只修改这两行即可,内容如下:
AC_INIT([hello], [1.0], [gwsong52@qq.com])
AM_INIT_AUTOMAKE([ -Wall -Werror foreign ])
运行autoreconf命令
- autoreconf --install,加参数–install,为源码包安装(复制)相关的宏文件到工作目录
- autoreconf同时创建configure脚本,为生成Makefile做准备
如下所示:
songvm@ubuntu:~/works/xdn/aoo/ab$ ls
configure.ac hello.c Makefile.am
songvm@ubuntu:~/works/xdn/aoo/ab$ autoreconf --install
configure.ac:11: installing './compile'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
Makefile.am: installing './depcomp'
songvm@ubuntu:~/works/xdn/aoo/ab$ ls
aclocal.m4 compile configure depcomp install-sh Makefile.in
autom4te.cache config.h.in configure.ac hello.c Makefile.am missing
songvm@ubuntu:~/works/xdn/aoo/ab$
运行configure脚本,指定安装目录前缀为/tmp/foo,创建Makefile文件
- 运行: ./configure --prefix=/tmp/foo
- 将安装目录定为/tmp/foo,测试用临时目录,避免造成系统混乱!!!
- 可执行文件安装到/tmp/foo/bin目录下!
如下所示:
songvm@ubuntu:~/works/xdn/aoo/ab$ ./configure --prefix=/tmp/foo
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... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
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 whether gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands
songvm@ubuntu:~/works/xdn/aoo/ab$
Makefile文件已创建出来,可以运行make命令了
- 运行 make 命令
- 运行 ./hello 查看编译结果是否符合预期!!!
如下所示:
songvm@ubuntu:~/works/xdn/aoo/ab$ ls
aclocal.m4 config.h config.status depcomp Makefile missing
autom4te.cache config.h.in configure hello.c Makefile.am stamp-h1
compile config.log configure.ac install-sh Makefile.in
songvm@ubuntu:~/works/xdn/aoo/ab$ make
make all-am
make[1]: 进入目录“/home/songvm/works/xdn/aoo/ab”
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT hello.o -MD -MP -MF .deps/hello.Tpo -c -o hello.o hello.c
mv -f .deps/hello.Tpo .deps/hello.Po
gcc -g -O2 -o hello hello.o
make[1]: 离开目录“/home/songvm/works/xdn/aoo/ab”
songvm@ubuntu:~/works/xdn/aoo/ab$ ls
aclocal.m4 config.h.in configure.ac hello.o Makefile.in
autom4te.cache config.log depcomp install-sh missing
compile config.status hello Makefile stamp-h1
config.h configure hello.c Makefile.am
songvm@ubuntu:~/works/xdn/aoo/ab$ ./hello
Hello world!
songvm@ubuntu:~/works/xdn/aoo/ab$
顺利通过,make命令的其他参数
- 通过configure脚本生成的Makefile功能丰富!!!
- make 编译,默认为 make all
- make install 安装
- make uninstall 缷载(反安装)
- make dist 打包,生成tar.gz格式的源码包
- make clean 清除编译结果
- make distclean 清除编译结果,同时清除Makefile,configure脚本运行时创建的文件
如下所示:
songvm@ubuntu:~/works/xdn/aoo/ab$ make dist
make dist-gzip am__post_remove_distdir='@:'
make[1]: 进入目录“/home/songvm/works/xdn/aoo/ab”
if test -d "hello-1.0"; then find "hello-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "hello-1.0" || { sleep 5 && rm -rf "hello-1.0"; }; else :; fi
test -d "hello-1.0" || mkdir "hello-1.0"
test -n "" \
|| find "hello-1.0" -type d ! -perm -755 \
-exec chmod u+rwx,go+rx {} \; -o \
! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \
! -type d ! -perm -400 -exec chmod a+r {} \; -o \
! -type d ! -perm -444 -exec /bin/bash /home/songvm/works/xdn/aoo/ab/install-sh -c -m a+r {} {} \; \
|| chmod -R a+r "hello-1.0"
tardir=hello-1.0 && ${TAR-tar} chof - "$tardir" | eval GZIP= gzip --best -c >hello-1.0.tar.gz
make[1]: 离开目录“/home/songvm/works/xdn/aoo/ab”
if test -d "hello-1.0"; then find "hello-1.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "hello-1.0" || { sleep 5 && rm -rf "hello-1.0"; }; else :; fi
songvm@ubuntu:~/works/xdn/aoo/ab$ ls
aclocal.m4 config.h.in configure.ac hello.c Makefile.am
autom4te.cache config.log depcomp hello.o Makefile.in
compile config.status hello install-sh missing
config.h configure hello-1.0.tar.gz Makefile stamp-h1
songvm@ubuntu:~/works/xdn/aoo/ab$
解包hello-1.0.tar.gz,重构测试一下
- 解包命令 tar xvfz hello-1.0.tar.gz
- 进入hello-1.0目录
- 运行configure脚本
- 重新运行make和make install
如下所示:
songvm@ubuntu:~/works/xdn/aoo/ab/hello-1.0$ make
make all-am
make[1]: 进入目录“/home/songvm/works/xdn/aoo/ab/hello-1.0”
gcc -DHAVE_CONFIG_H -I. -g -O2 -MT hello.o -MD -MP -MF .deps/hello.Tpo -c -o hello.o hello.c
mv -f .deps/hello.Tpo .deps/hello.Po
gcc -g -O2 -o hello hello.o
make[1]: 离开目录“/home/songvm/works/xdn/aoo/ab/hello-1.0”
songvm@ubuntu:~/works/xdn/aoo/ab/hello-1.0$ make install
make[1]: 进入目录“/home/songvm/works/xdn/aoo/ab/hello-1.0”
/bin/mkdir -p '/tmp/foo/bin'
/usr/bin/install -c hello '/tmp/foo/bin'
make[1]: 对“install-data-am”无需做任何事。
make[1]: 离开目录“/home/songvm/works/xdn/aoo/ab/hello-1.0”
songvm@ubuntu:~/works/xdn/aoo/ab/hello-1.0$
查看安装结果,运行hello检测一下
- 安装目录前缀为prefix /tmp/foo
如下所示:
songvm@ubuntu:~$ ls /tmp/foo/bin
hello
songvm@ubuntu:~$ /tmp/foo/bin/hello
Hello world!
运行make uninstall命令缷载hello
如下所示:
songvm@ubuntu:~/works/xdn/aoo/ab/hello-1.0$ make uninstall
( cd '/tmp/foo/bin' && rm -f hello )
songvm@ubuntu:~/works/xdn/aoo/ab/hello-1.0$
以上操作,就可以将hello-1.0.tar.gz源码包上传论坛共享给朋友了!!!
再重构一下,将源码保存在src目录下
- 创建工作目录ac,进入目录ac,创建源码目录src
- 在ac目录下编辑Makefile.am,内容如下:
Makefile.am
SUBDIRS = src
- 在src目录下编辑Makefile.am,内容如下:
src/Makefile.am
bin_PROGRAMS = hello
hello_SOURCE = hello.c
- 将hello.c复制到src目录下
再运行autoscan生成脚本配置文件
- 会发现ACOUTPUT多了一行,每个目录都生成一个Makefile!!!
- 编辑配置文件configureac将版本号由1.0改为1.1!
内容如下:
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([hello], [1.1], [gwsong52@qq.com])
AM_INIT_AUTOMAKE([ -Wall -Werror foreign ])
AC_CONFIG_SRCDIR([src/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
src/Makefile])
AC_OUTPUT
运行autoreconf --install,注意加install参数!!!
- 依次运行下列命令,查看运行结果:
- configure
- make
- hello
- make dist
- make install
- make uninstall
如下所示:
songvm@ubuntu:~/works/xdn/aoo/abc$ ls
autoscan.log configure.ac Makefile.am src
songvm@ubuntu:~/works/xdn/aoo/abc$ autoreconf --install
configure.ac:11: installing './compile'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
src/Makefile.am: installing './depcomp'
songvm@ubuntu:~/works/xdn/aoo/abc$ ./configure --prefix=/tmp/foo
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... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
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 whether gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating config.h
config.status: executing depfiles commands
songvm@ubuntu:~/works/xdn/aoo/abc$ make
make all-recursive
make[1]: 进入目录“/home/songvm/works/xdn/aoo/abc”
Making all in src
make[2]: 进入目录“/home/songvm/works/xdn/aoo/abc/src”
gcc -DHAVE_CONFIG_H -I. -I.. -g -O2 -MT hello.o -MD -MP -MF .deps/hello.Tpo -c -o hello.o hello.c
mv -f .deps/hello.Tpo .deps/hello.Po
gcc -g -O2 -o hello hello.o
make[2]: 离开目录“/home/songvm/works/xdn/aoo/abc/src”
make[2]: 进入目录“/home/songvm/works/xdn/aoo/abc”
make[2]: 离开目录“/home/songvm/works/xdn/aoo/abc”
make[1]: 离开目录“/home/songvm/works/xdn/aoo/abc”
songvm@ubuntu:~/works/xdn/aoo/abc$ ./hello
bash: ./hello: 没有那个文件或目录
songvm@ubuntu:~/works/xdn/aoo/abc$ ./src/hello
Hello world!
songvm@ubuntu:~/works/xdn/aoo/abc$ make dist
make dist-gzip am__post_remove_distdir='@:'
make[1]: 进入目录“/home/songvm/works/xdn/aoo/abc”
if test -d "hello-1.1"; then find "hello-1.1" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "hello-1.1" || { sleep 5 && rm -rf "hello-1.1"; }; else :; fi
test -d "hello-1.1" || mkdir "hello-1.1"
(cd src && make top_distdir=../hello-1.1 distdir=../hello-1.1/src \
am__remove_distdir=: am__skip_length_check=: am__skip_mode_fix=: distdir)
make[2]: 进入目录“/home/songvm/works/xdn/aoo/abc/src”
make[2]: 离开目录“/home/songvm/works/xdn/aoo/abc/src”
test -n "" \
|| find "hello-1.1" -type d ! -perm -755 \
-exec chmod u+rwx,go+rx {} \; -o \
! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o \
! -type d ! -perm -400 -exec chmod a+r {} \; -o \
! -type d ! -perm -444 -exec /bin/bash /home/songvm/works/xdn/aoo/abc/install-sh -c -m a+r {} {} \; \
|| chmod -R a+r "hello-1.1"
tardir=hello-1.1 && ${TAR-tar} chof - "$tardir" | eval GZIP= gzip --best -c >hello-1.1.tar.gz
make[1]: 离开目录“/home/songvm/works/xdn/aoo/abc”
if test -d "hello-1.1"; then find "hello-1.1" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -rf "hello-1.1" || { sleep 5 && rm -rf "hello-1.1"; }; else :; fi
songvm@ubuntu:~/works/xdn/aoo/abc$ ls
aclocal.m4 config.h configure hello-1.1.tar.gz Makefile.in
autom4te.cache config.h.in configure.ac install-sh missing
autoscan.log config.log configure.ac~ Makefile src
compile config.status depcomp Makefile.am stamp-h1
songvm@ubuntu:~/works/xdn/aoo/abc$ make install
Making install in src
make[1]: 进入目录“/home/songvm/works/xdn/aoo/abc/src”
make[2]: 进入目录“/home/songvm/works/xdn/aoo/abc/src”
/bin/mkdir -p '/tmp/foo/bin'
/usr/bin/install -c hello '/tmp/foo/bin'
make[2]: 对“install-data-am”无需做任何事。
make[2]: 离开目录“/home/songvm/works/xdn/aoo/abc/src”
make[1]: 离开目录“/home/songvm/works/xdn/aoo/abc/src”
make[1]: 进入目录“/home/songvm/works/xdn/aoo/abc”
make[2]: 进入目录“/home/songvm/works/xdn/aoo/abc”
make[2]: 对“install-exec-am”无需做任何事。
make[2]: 对“install-data-am”无需做任何事。
make[2]: 离开目录“/home/songvm/works/xdn/aoo/abc”
make[1]: 离开目录“/home/songvm/works/xdn/aoo/abc”
songvm@ubuntu:~/works/xdn/aoo/abc$ /tmp/foo/bin/hello
Hello world!
songvm@ubuntu:~/works/xdn/aoo/abc$ make uninstall
Making uninstall in src
make[1]: 进入目录“/home/songvm/works/xdn/aoo/abc/src”
( cd '/tmp/foo/bin' && rm -f hello )
make[1]: 离开目录“/home/songvm/works/xdn/aoo/abc/src”
make[1]: 进入目录“/home/songvm/works/xdn/aoo/abc”
make[1]: 对“uninstall-am”无需做任何事。
make[1]: 离开目录“/home/songvm/works/xdn/aoo/abc”
songvm@ubuntu:~/works/xdn/aoo/abc$
以上操作,hello的1.1版本的源码包就创建成功了!
- 下一步创建一个动态链接库试试!!!