autotool使用 - linux

來源:互聯網
上載者:User

標籤:c   style   blog   a   http   tar   

執行步驟:

1、autoscan


2、vi configure.scan


3、mv configure.scan configure.in

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.68])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE(main, 1.0)

# Checks for programs.
AC_PROG_CC
AC_CONFIG_FILES([Makefile])

# Checks for libraries.

# Checks for header files.
AC_CHECK_HEADERS([string.h])

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT


4、建立Makefile.am

UTOMAKE_OPTIONS= foreign
bin_PROGRAMS= main 
main_SOURCES= main.c hello.c hello.h


5、touch NEWS AUTHORS ChangeLog README;aclocal;autoconf;autoheader;automake;automake --add-missing;touch INSTALL COPYING;automake;autoreconf;./configure 


make

./main

ok!!!


下面是別人的文章。


主要步驟:
1.開發人員要書寫的檔案主要是configure.in和Makefile.am
2.運行autoscan檢測源檔案產生configure.scan並修改成configure.in
3.編輯configure.in
4.由aclocal命令產生aclocal.m4
5.運行autoconf產生configure指令碼
6.運行autoheader產生config.h.in檔案
7.建立並編輯Makfile.am
8.運行automake產生makefile.in  有時候要加automake --add-missing,這很重要哦
9.運行configure指令碼產生Makefile
10.make
11.運行可執行程式main
 

簡單-同一個目錄(不同目錄見《autotools產生Makefile(二)》)

1.寫好源碼main.c hello.c hello.h
[[email protected] 1]# ls
hello.c  hello.h  main.c

[[email protected] 1]# cat main.c
#include <stdio.h>
int main(void)
{
        printf("main start!\n");
        print();

        return 0;
}

[[email protected] 1]# cat hello.c
#include "hello.h"
void print(void)
{
        char buf[32] = "123";
        strcat(buf, "456");
        printf("%s\n", buf);
        printf("hello,world!\n");

        return ;
}

[[email protected] 1]# cat hello.h
#ifndef __HELLO_H__
#define __HELLO_H__
#include <stdio.h>
#include <string.h>
void print(void);
#endif

2.執行命令:autoscan
[[email protected] 1]# autoscan
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1
[[email protected] 1]# ls
autoscan.log  configure.scan hello.c  hello.h  main.c

3.編輯configure.scan
[[email protected] 1]# vim configure.scan
//只增加這2項:
AM_INIT_AUTOMAKE(main, 1.0)
AC_OUTPUT(Makefile)
[[email protected] 1]# mv configure.scan configure.in

4.aclocal
[[email protected] 1]# aclocal
[[email protected] 1]# ls
aclocal.m4  autoscan.log  hello.c  main.c  autom4te.cache  configure.in  hello.h

5.autoconf
[[email protected] 1]# autoconf
[[email protected] 1]# ls
aclocal.m4 autoscan.log  configure.in  hello.h  autom4te.cache  configure  hello.c   main.c

6.autoheader
[[email protected] 1]# autoheader
[[email protected] 1]# ls
aclocal.m4 autoscan.log  configure hello.c  main.c  autom4te.cache  config.h.in   configure.in  hello.h

7.用vi編輯makefile.am檔案,編輯如下,編輯完後儲存退出
[[email protected] 1]# vi Makefile.am  //注意:不是makefile-即M應為大寫
AUTOMAKE_OPTIONS= foreign //軟體等級
bin_PROGRAMS= main //可執行檔名
main_SOURCES= main.c hello.c  //源檔案名稱

8.automake + automake --add-missing
[[email protected] 1]# automake
configure.in: required file `./install-sh‘ not found
configure.in: required file `./missing‘ not found
Makefile.am: required file `./depcomp‘ not found
[[email protected] 1]# automake --add-missing
configure.in: installing `./install-sh‘
configure.in: installing `./missing‘
Makefile.am: installing `./depcomp‘
[[email protected] 1]# ls
 depcomp  install-sh  missing

9.運行configure 
   產生 Makefile config.log  config.status

10.make產生可執行檔main

11.運行main
[[email protected] 1]# ./main
main start!
123456
hello,world!

 

需要編輯的二個主要檔案

configure.in :

AC_PREREQ(2.59) 
AC_INIT(hello,1.0,[email protected])    //在此行內容中設定當前軟體包資訊 
AM_INIT_AUTOMAKE(main,1.0)      //automake 所必備的宏,必須添加 
AC_CONFIG_SRCDIR([hello.c])      //源檔案名稱 
AC_CONFIG_HEADER([config.h])      //config 檔案 
# Checks for programs. 
AC_PROG_CC          //編譯器,可以不指定 
# Checks for libraries. # Checks for header files. 
# Checks for typedefs, structures, and compiler 
characteristics. 
# Checks for library functions. 
AC_OUTPUT(Makefile)        //輸出檔案名為 makefile

分析:

(1)AC_PREREQ 宏聲明本檔案要求的 autoconf 版本,本例使用的版本為 2.59。 
(2)AC_INIT 宏用來定義軟體的名稱和版本等資訊,"FULL-PACKAGE-NAME"為軟體包名稱,"VERSION"為軟體版本號碼,"BUG-REPORT-ADDRESS"為 BUG 報告地址 (一般為軟體作者郵件地址)。
(3)AC_CONFIG_SRCDIR 宏用來偵測所指定的源碼檔案是否存在,來確定源碼目錄的有效性。此處為目前的目錄下的 hello.c。  
(4)AC_CONFIG_HEADER 宏用於產生 config.h 檔案,以便 autoheader 使用。  
(5)AC_PROG_CC 用來指定編譯器,如果不指定,選用預設 gcc。 
(6)AC_OUTPUT 用來設定configure 所要產生的檔案,如果是 makefile,configure 會把它檢查出來的結果帶入 makefile.in 檔案產生合適的 makefile。
使用 Automake 時,還需要一些其他的參數,這些額外的宏用 aclocal 工具產生。 中間的注釋可以分別添加使用者測試程式、測試函數庫和測試標頭檔等宏定義。
此檔案只是下面要使用的 configure.ac 檔案的原型,要使用此檔案,還需要根據情況修改相關內容。      
  (7)AM_INIT_AUTOMAKE(PACKAGE,VERSION) 這個是使用 Automake 所必備的宏,PACKAGE 是所要產生軟體套件的名稱,VERSION 是版本編號。

   

Makefile.am
         Automake 會根據 configure.in 中的宏把Makefile.am 轉成 Makefile.in 檔案。 Makefile.am 檔案定義所要產生的目標:
AUTOMAKE_OPTIONS 
    設定 automake 的選項。
       Automake 主要是協助開發 GNU 軟體的人員來維護軟體,所以在執行 automake 時,會檢查目錄下是否存在標準 GNU 軟體中應具備的檔案,例如 ‘NEWS‘、‘AUTHOR‘、‘ChangeLog‘ 等檔案。設定 foreign 時,automake 會改用一般軟體的標準來檢查。
bin_PROGRAMS 
    定義要產生的執行檔案名稱。如果要產生多個執行檔案,每個檔案名稱用空白符隔開。 
main_SOURCES 
    定義 ‘hello‘ 這個執行程式所需要的原始檔案。如果 ‘hello‘這個程式是由多個原始檔案所產生,必須把它所用到的所有原始檔案都列出來,以空白符隔開。假設 ‘hello‘ 還需要 ‘hello.c‘、‘main.c‘、‘hello.h‘ 三個檔案的話,則定義
hello_SOURCES= hello.c main.c hello.h 
如果定義多個執行檔案,則對每個執行程式都要定義相對的filename_SOURCES。 

 

總結:只要有感覺,nothing is impossible!


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.