配置軟體原始碼包構建環境

weixin_33766168發表於2015-12-12

本文中,我們演示如何建立原始碼安裝包: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 指令碼

關鍵流程:

  1. 執行 autoscan 命令

  2. 把 configure.scan 重新命名為 configure.ac

  3. 編輯 configure.ac 的內容

  4. 執行 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

完成後,你可以看到 Makefilesrc/Makefileconfig.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

最新文章請訪問 這裡

相關文章