wiki:shunfa/2011/0729

Version 7 (modified by shunfa, 13 years ago) (diff)

--

Autotool 環境設定及範例筆記

目標

將寫好的程式碼透過以下指令就可編譯.安裝及使用

1. ./configure
2. make
3. make install

第一階段:寫好source code及 Makefile

Step2. 編輯程式碼

  1. 參考autotool tutorial
  2. 新增hello.c
    /* hello.c: A standard "Hello, world!" program */
    #include <stdio.h>
    int main(int argc, char* argv[])
    {
           printf("Hello, world!\n");
           return ;
    }
    
  1. 編輯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. 工具取得及安裝

  1. 工具取得 autoconf下載(v2.68)
  2. 安裝(步驟略)

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並支援 protable

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 

第四階段:auto make

  • 下載
  • 新增Makefile.am
    bin_PROGRAMS=hello
    hello_SOURCES=hello.c
    
  • 執行
    $ automake
    configure.ac: no proper invocation of AM_INIT_AUTOMAKE was found.
    configure.ac: You should verify that configure.ac invokes AM_INIT_AUTOMAKE,
    configure.ac: that aclocal.m4 is present in the top-level directory,
    configure.ac: and that aclocal.m4 was recently regenerated (using aclocal).
    Makefile.am: required file `./INSTALL' not found
    Makefile.am:   `automake --add-missing' can install `INSTALL'
    Makefile.am: required file `./NEWS' not found
    Makefile.am: required file `./README' not found
    Makefile.am: required file `./AUTHORS' not found
    Makefile.am: required file `./ChangeLog' not found
    Makefile.am: required file `./COPYING' not found
    Makefile.am:   `automake --add-missing' can install `COPYING'
    Makefile.am: required file `./depcomp' not found
    Makefile.am:   `automake --add-missing' can install `depcomp'
    /usr/local/share/automake-1.11/am/depend2.am: am__fastdepCC does not appear in AM_CONDITIONAL
    /usr/local/share/automake-1.11/am/depend2.am:   The usual way to define `am__fastdepCC' is to add `AC_PROG_CC'
    /usr/local/share/automake-1.11/am/depend2.am:   to `configure.ac' and run `aclocal' and `autoconf' again.
    /usr/local/share/automake-1.11/am/depend2.am: AMDEP does not appear in AM_CONDITIONAL
    /usr/local/share/automake-1.11/am/depend2.am:   The usual way to define `AMDEP' is to add one of the compiler tests
    /usr/local/share/automake-1.11/am/depend2.am:     AC_PROG_CC, AC_PROG_CXX, AC_PROG_CXX, AC_PROG_OBJC,
    /usr/local/share/automake-1.11/am/depend2.am:     AM_PROG_AS, AM_PROG_GCJ, AM_PROG_UPC
    /usr/local/share/automake-1.11/am/depend2.am:   to `configure.ac' and run `aclocal' and `autoconf' again.
    
  • fix error
  • done