配置軟件源碼包構建環境

本文中,咱們演示如何建立源碼安裝包:amhello-1.0.0.tar.gz,代碼結構以下:git

MacBook:amhello sam$ tree
.
├── AUTHORS
├── COPYING
├── ChangeLog
├── Makefile.am
├── NEWS
├── README
└── src
    ├── Makefile.am
    └── main.c

1 directory, 8 files

下面列出關鍵幾個文件的內容,其它文件的內容並非製做源碼安裝包所必需的,因此暫時爲空。github

本文用到的初始代碼能夠在這裏下載。vim

Makefile.am工具

MacBook:amhello sam$ cat Makefile.am
SUBDIRS = src
dist_doc_DATA = README

src/Makefile.amui

MacBook:amhello sam$ cat src/Makefile.am
bin_PROGRAMS = amhello
amhello_SOURCES = main.c

src/main.ccode

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 腳本

關鍵流程:blog

  1. 執行 autoscan 命令遞歸

  2. 把 configure.scan 重命名爲 configure.acget

  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

最新文章請訪問 這裏

相關文章
相關標籤/搜索