本文中,我们演示如何创建源码安装包:amhello-1.0.0.tar.gz,代码结构如下:
MacBook:amhello sam$ tree
.
├── AUTHORS
├── COPYING
├── ChangeLog
├── Makefile.am
├── NEWS
├── README
└── src
├── Makefile.am
└── main.c
1 directory, 8 files
下面列出关键几个文件的内容,其它文件的内容并不是制作源码安装包所必需的,所以暂时为空。
本文用到的初始代码可以在 这里 下载。
Makefile.am
MacBook:amhello sam$ cat Makefile.am
SUBDIRS = src
dist_doc_DATA = README
src/Makefile.am
MacBook:amhello sam$ cat src/Makefile.am
bin_PROGRAMS = amhello
amhello_SOURCES = main.c
src/main.c
MacBook:amhello sam$ cat src/main.c
#include <config.h>
#include <stdio.h>
int main(int argc, const char *argv[]) {
puts("This is " PACKAGE_STRING ".");
return 0;
}
生成 configure 脚本
关键流程:
- 执行 autoscan 命令
- 把 configure.scan 重命名为 configure.ac
- 编辑 configure.ac 的内容
- 执行 autoreconf 命令
执行命令:
MacBook:amhello sam$ autoscan
MacBook:amhello sam$ mv configure.scan configure.ac
autoscan
是用于创建和维护源码包中 configure.ac 的辅助工具,它会递归扫描指定目录树中所有的源文件(如果没有指定目录则默认是当前目录),并自动生成 configure.scan
文件,这个文件需要手动重命名成 configure.ac
,并按如下格式修改内容:
AC_PREREQ([2.69])
AC_INIT([amhello], [1.0.0])
AM_INIT_AUTOMAKE([subdir-objects])
AC_CONFIG_HEADERS([config.h])
# Checks for programs.
AC_PROG_CC
AC_CONFIG_FILES([
Makefile
src/Makefile
])
AC_OUTPUT
生成 configure 脚本:
MacBook:amhello sam$ autoreconf --install
configure.ac:10: installing './compile'
configure.ac:6: installing './install-sh'
configure.ac:6: installing './missing'
Makefile.am: installing './INSTALL'
src/Makefile.am: installing './depcomp'
这个命令会生成 configure 脚本。
生成 Makefile 文件
执行刚刚生成的 configure 脚本,生成最终的 Makefile 文件:
MacBook:amhello sam$ ./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... ./install-sh -c -d
checking for gawk... no
checking for mawk... no
checking for nawk... no
checking for awk... awk
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
完成后,你可以看到 Makefile , src/Makefile 和 config.h 已自动生成。现在可以使用 make build 编译了:
MacBook:amhello sam$ make
...
MacBook:amhello sam$ src/amhello
This is amhello 1.0.0.
生成源码包
MacBook:amhello sam$ make distcheck
...
================================================
amhello-1.0.0 archives ready for distribution:
amhello-1.0.0.tar.gz
================================================
遇到的问题
缺少必要的文件
如果遇到以下错误:
Makefile.am: error: required file ‘./NEWS’ not found
Makefile.am: error: required file ‘./README’ not found
Makefile.am: error: required file ‘./AUTHORS’ not found
Makefile.am: error: required file ‘./ChangeLog’ not found
就创建这4个文件:
touch NEWS README AUTHORS ChangeLog