Version 5 (modified by shunfa, 13 years ago) (diff) |
---|
Autotool 環境設定及範例筆記
目標
將寫好的程式碼透過以下指令就可編譯.安裝及使用
1. ./configure 2. make 3. make install
第一階段:寫好source code及 Makefile
Step2. 編輯程式碼
- 參考autotool tutorial
- 新增hello.c
/* hello.c: A standard "Hello, world!" program */ #include <stdio.h> int main(int argc, char* argv[]) { printf("Hello, world!\n"); return ; }
- 編輯Makefile(註:指令前的空白為<TAB>)
# Makefile: A standard Makefile for hello.c all: gcc hello.c -o hello clean: rm -f hello
Step3. 執行make,產生執行檔
- 執行
$ make
- 步驟結束後目錄下會有以下檔案: hello.c hello Makefile
第二階段:使用autoconf產生安裝及設定檔
Step1. 工具取得及安裝
- 工具取得 autoconf下載(v2.68)
- 安裝(步驟略)
Step2. autoscan
- 此一步驟為讓系統自動產生 configure.scan
- 將產生的configure.scan更名為configure.ac,以便日後自行修改configure時不會被覆寫掉
- 將Makefile 更名為 Makefile.in 做為日後修改的樣板
Step3. autoconf
- 產生configure的可執行檔
- 執行configure
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 configure: creating ./config.status config.status: creating Makefile config.status: error: cannot find input file: `config.h.in'
- 錯誤訊息:因為無法找到header檔
Step4. autoheader
- 執行 autoheader
- 產生header檔
- 執行configure
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 configure: creating ./config.status config.status: creating Makefile config.status: creating config.h
第三階段:修改Source Code
Step1. 修改hello.c
/* hello.c: A program to show the time since the Epoch */ #include <stdio.h> #include <sys/time.h> int main(int argc, char* argv[]) { double sec; struct timeval tv; gettimeofday(&tv, NULL); sec = tv.tv_sec; sec += tv.tv_usec / 1000000.0; printf("%f\n", sec); return 0; }
Step2. 更新設定檔 ===
$ autoscan $ mv configure.scan configure.ac $ autoconf $ autoheader $ ./configure
Step3. 修改source code使其支援protable
/* hello.c: A program to show the time since the Epoch */ #include <stdio.h> #include "config.h" #ifdef HAVE_SYS_TIME_H #include <sys/time.h> #else #include <time.h> #endif double get_sec_since_epoch() { double sec; #ifdef HAVE_GETTIMEOFDAY struct timeval tv; gettimeofday(&tv, NULL); sec = tv.tv_sec; sec += tv.tv_usec / 1000000.0; #else sec = time(NULL); #endif return sec; } int main(int argc, char* argv[]) { printf("%f\n", get_sec_since_epoch()); return 0; }
Step4. 編譯及執行
$ autoscan $ ./configure $ make clean all $ ./hello