linux使用autotools生成可執行文件、靜態庫、動態庫Makefile的流程介紹

Linux下,工程管理器make是可用於自動編譯、鏈接程序的實用工具。我們要做的是寫一個Makefile文件,然後用make命令來編譯、鏈接程序。Makefile的作用就是讓編譯器知道要編譯一個文件需要依賴其他的哪些文件。
    GNU autotools作用:收集系統配置信息並自動生成Makefile文件。
    GNU autotools主要包括三個工具:autoconf、automake、libtool。

 

生成可執行文件:

主要步驟:
1.開發者要書寫的文件主要是configure.inMakefile.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
 

同一個目錄

1.寫好源碼main.c hello.c hello.h
[root@localhost 1]# ls
hello.c  hello.h  main.c

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

        return 0;
}

[root@localhost 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 ;
}

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

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

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

4.aclocal
[root@localhost 1]# aclocal
[root@localhost 1]# ls
aclocal.m4  autoscan.log  hello.c  main.c  autom4te.cache  configure.in  hello.h

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

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

7.用vi編輯makefile.am文件,編輯如下,編輯完後保存退出
[root@localhost 1]# vi Makefile.am  //注意:不是makefile-即M應爲大寫
AUTOMAKE_OPTIONS= foreign //軟件等級
bin_PROGRAMS= main //可執行文件名
main_SOURCES= main.c hello.c  //源文件名

8.automake + automake --add-missing
[root@localhost 1]# automake
configure.in: required file `./install-sh' not found
configure.in: required file `./missing' not found
Makefile.am: required file `./depcomp' not found
[root@localhost 1]# automake --add-missing
configure.in: installing `./install-sh'
configure.in: installing `./missing'
Makefile.am: installing `./depcomp'
[root@localhost 1]# ls
 depcomp  install-sh  missing

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

10.make生成可執行文件main

11.運行main
[root@localhost 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。

==================================================================================================================================

生成靜態庫:

多個目錄下的autotools生成Makefile:

1.設計好目錄以及源碼(每個目錄下都必須配置文件Makefile.am)
一、main.c目錄下:
[root@localhost 2]# ls
include  main.c  Makefile.am  src
[root@localhost 2]# cat main.c
#include <stdio.h>
int main(void)
{
        printf("main\n");
        add(4,2);
        mul(4,2);

        return 0;
}
[root@localhost 2]# cat Makefile.am
AUTOMAKE_OPTIONS=foreign                            //軟件等級
SUBDIRS=src include                                             //指定需要處理的子目錄,如果要處理多個子目錄,以空格隔開
CURRENTPATH=$(shell /bin/pwd)                        //利用pwd取得當前路徑
INCLUDES=-I$(CURRENTPATH)/include            //頭文件路徑
export INCLUDES                                                    //導出供子目錄Makefile.am使用
bin_PROGRAMS=main                                           //指定要產生的可執行文件名
main_SOURCES=main.c                                        //指定產生可執行文件需要的源文件,如果有多個,以空格隔開
main_LDADD=$(CURRENTPATH)/src/libpro.a  //連接庫,也可以LIBS+=$(CURRENTPATH)/src/libpro.a  增加鏈接庫

二、src目錄下:
[root@localhost src]# pwd
/work/autotools/2/src
[root@localhost src]# ls
add.c  Makefile.am  mul.c
[root@localhost src]# cat add.c
#include <stdio.h>
void add(int a, int b)
{
        printf("a+b = %d\n", a+b);
        return ;
}
[root@localhost src]# cat mul.c
#include <stdio.h>
void mul(int a, int b)
{
        printf("a*b = %d\n", a*b);
        return ;
}
[root@localhost src]# cat Makefile.am
noinst_LIBRARIES=libpro.a                  //src目錄下生產靜態庫libpro.a,以便頂層目錄主文件鏈接使用
libpro_a_SOURCES=add.c mul.c       //產生靜態庫所需要的源文件,以空格隔開

三、include目錄下:
[root@localhost include]# ls
add.h  Makefile.am  mul.h
[root@localhost include]# cat add.h
#ifndef __ADD_H__
#define __ADD_H__
#include <stdio.h>
void add(int a, int b);
#endif
[root@localhost include]# cat mul.h
#ifndef __MUL_H__
#define __MUL_H__
#include <stdio.h>
void mul(int a, int b);
#endif
[root@localhost include]# cat Makefile.am
EXTRA_DIST=mul.h add.h          //EXTRA_DIST表示用來定義要額外打包的文件名稱

 

2.執行命令:autoscan
[root@localhost 2]# autoscan
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1
[root@localhost 2]# ls
autoscan.log  configure.scan  include  main.c  Makefile.am  src

 

3.編輯configure.scan
[root@localhost 1]# vim configure.scan
//只增加這2項:
AM_INIT_AUTOMAKE(main, 1.0)
AC_PROG_RANLIB   //庫--宏
[root@localhost 1]# mv configure.scan configure.in

 

4.aclocal
[root@localhost 1]# aclocal
生成aclocal.m4和autom4te.cache 文件

 

5.autoconf
[root@localhost 1]# autoconf
生成configure腳本

 

6.autoheader
[root@localhost 1]# autoheader
生成config.h.in

 

7.automake --add-missing
[root@localhost 2]# automake --add-missing
configure.in: installing `./install-sh'
configure.in: installing `./missing'
src/Makefile.am: installing `./depcomp'
生成 depcomp  install-sh  missing

 

8.運行configure
[root@localhost 2]# ./configure
生成 Makefile,config.log和config.status

 

9.make生成main可執行程序
[root@localhost 2]# make
生成main

 

10.運行可執行程序main
[root@localhost 2]# ./main
main
a+b = 6
a*b = 8


 這樣,autotools生成Makefile就差不多了

==================================================================================================================================

生成動態庫:

(待續)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章