Linux下自動生成makefile文件

初始環境

-bash-3.2# ll

total 48

lrwxrwxrwx 1 root root    18 Sep 24 09:33 libMsuDecoder.a -> libMsuDecoder_64.a

-rw-r--r-- 1 root root 14320 Sep 24 09:29 libMsuDecoder_32.a

-rw-r--r-- 1 root root 19872 Sep 24 09:29 libMsuDecoder_64.a

-rw-r--r-- 1 root root  1304 Sep 24 09:29 msu_decoder.h

-rw-r--r-- 1 root root  4654 Sep 24 09:29 test_decoder.c
 


自動生成步驟

1.      執行autoscan命令

執行autoscan命令後,生成autoscan.logconfigure.scan兩個文件,其中configure.scan文件內容如下:

 

-bash-3.2# more configure.scan

#                                               -*- Autoconf -*-

# Process this file with autoconf to produce a configure script.

 

AC_PREREQ(2.59)

AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)

AC_CONFIG_SRCDIR([test_decoder.c])

AC_CONFIG_HEADER([config.h])

 

# Checks for programs.

AC_PROG_CC

 

# Checks for libraries.

 

# Checks for header files.

AC_HEADER_STDC

AC_CHECK_HEADERS([fcntl.h stdlib.h string.h sys/socket.h unistd.h])

 

# Checks for typedefs, structures, and compiler characteristics.

AC_C_CONST

AC_HEADER_TIME

 

# Checks for library functions.

AC_FUNC_SELECT_ARGTYPES

AC_CHECK_FUNCS([memset select])

AC_OUTPUT

-bash-3.2#


其中:

  • AC_PREREQ指定autoconf版本,確保使用的是足夠新的Autoconf版本。如果用於創建configureAutoconf的版本比version要早,就在標準錯誤輸出打印一條錯誤消息並不會創建configure
  • AC_INIT初始化,定義軟件的基本信息,包括設置包的全稱,版本號以及報告BUG時需要用的郵箱地址
  • AC_CONFIG_SRCDIR用來偵測所指定的源碼文件是否存在,來確定源碼目錄的有效性
  • AC_CONFIG_HEADER用於生成config.h文件,以便autoheader使用

 

將生成的configure.scan改名爲configure.in,並修改其內容,修改結果如下:

-bash-3.2# more configure.in

#                                               -*- Autoconf -*-

# Process this file with autoconf to produce a configure script.

 

AC_PREREQ(2.59)

AC_INIT(test_decoder, 1.0, [email protected])

AC_CONFIG_SRCDIR([test_decoder.c])

AC_CONFIG_HEADER([config.h])

AM_INIT_AUTOMAKE(test_decoder,1.0)

 

 

 

# Checks for programs.

AC_PROG_CC

 

# Checks for libraries.

 

# Checks for header files.

AC_HEADER_STDC

AC_CHECK_HEADERS([fcntl.h stdlib.h string.h sys/socket.h unistd.h])

 

# Checks for typedefs, structures, and compiler characteristics.

AC_C_CONST

AC_HEADER_TIME

 

# Checks for library functions.

AC_FUNC_SELECT_ARGTYPES

AC_CHECK_FUNCS([memset select])

AC_OUTPUT([Makefile])

 

修改動作:

  • 修改AC_INIT裏面的參數: AC_INIT(test_decoder, 1.0, [email protected])
  • 添加宏AM_INIT_AUTOMAKE,它是automake所必備的宏,也同前面一樣,PACKAGE是所要產生軟件套件的名稱,VERSION是版本編號。
  • AC_OUTPUT後添加輸出文件Makefile

 

2.      執行aclocal命令

運行aclocal生成一個“aclocal.m4”文件和一個緩衝文件夾autom4te.cache,該文件主要處理本地的宏定義

 

3.      執行autoconf命令

運行autoconf,生成configure文件

 

4.      執行autoheader命令

運行 autoheader,它負責生成config.h.in文件。該工具通常會從“acconfig.h”文件中複製用戶附加的符號定義,因此此處沒有附加符號定義,所以不需要創建“acconfig.h”文件

 

5.      創建Makefile.am文件

創建一個 Makefile.am.這一步是創建Makefile很重要的一步,automake要用的腳本配置文件是Makefile.am,用戶需要自己創建相應的文件。之後,automake工具轉換成Makefile.in

 

其文件內容如下:

AUTOMAKE_OPTIONS=foreign

 

bin_PROGRAMS=test_decoder

 

test_decoder_SOURCES=test_decoder.c

 

test_decoder_LDADD = -lpthread  -lMsuDecoder -ldl

 

test_decoder_LDFLAGS= -L.


其中:

  • AUTOMAKE_OPTIONS爲設置automake的選項。由於GNU對自己發佈的軟件有嚴格的規範,比如必須附帶許可證聲明文件COPYING等,否則automake執行時會報錯。automake提供了三種軟件等級:foreigngnugnits,讓用戶選擇採用,默認等級爲gnu。在本例使用foreign等級,它只檢測必須的文件。
  • bin_PROGRAMS定義要產生的執行文件名。如果要產生多個執行文件,每個文件名用空格隔開。
  • test_decoder_SOURCES定義“test_decoder”這個執行程序所需要的原始文件。如果” test_decoder”這個程序是由多個原始文件所產生的,則必須把它所用到的所有原始文件都列出來,並用空格隔開。要注意的是,如果要定義多個執行文件,則對每個執行程序都要定義相應的file_SOURCES
  • test_decoder_LDADD定義”test_decoder”這個程序所需的庫
  • test_decoder_LDFLAGS定義”test_decoder”這個程序查找的庫目錄

 

6.      執行automake --add-missing命令

-bash-3.2# automake --add-missing

configure.in: installing `./install-sh'

configure.in: installing `./missing'

Makefile.am: installing `./depcomp'

 

執行automake –add-missing後生成文件Makefile.in

 

7.      執行configure命令

-bash-3.2# ./configure 

checking for a BSD-compatible install... /usr/bin/install -c

checking whether build environment is sane... yes

checking for gawk... gawk

checking whether make sets $(MAKE)... yes

checking for gcc... gcc

checking for C compiler default output file name... a.out

checking whether the C compiler works... yes

checking whether we are cross compiling... no

checking for suffix of executables...

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 ANSI C... none needed

checking for style of include used by make... GNU

checking dependency style of gcc... gcc3

checking for test_decoder in -lMsuDecoder... no

checking how to run the C preprocessor... gcc -E

checking for egrep... grep -E

checking for ANSI C header files... yes

checking for sys/types.h... yes

checking for sys/stat.h... yes

checking for stdlib.h... yes

checking for string.h... yes

checking for memory.h... yes

checking for strings.h... yes

checking for inttypes.h... yes

checking for stdint.h... yes

checking for unistd.h... yes

checking fcntl.h usability... yes

checking fcntl.h presence... yes

checking for fcntl.h... yes

checking for stdlib.h... (cached) yes

checking for string.h... (cached) yes

checking sys/socket.h usability... yes

checking sys/socket.h presence... yes

checking for sys/socket.h... yes

checking for unistd.h... (cached) yes

checking for an ANSI C-conforming const... yes

checking whether time.h and sys/time.h may both be included... yes

checking sys/select.h usability... yes

checking sys/select.h presence... yes

checking for sys/select.h... yes

checking for sys/socket.h... (cached) yes

checking types of arguments for select... int,fd_set *,struct timeval *

checking for memset... yes

checking for select... yes

configure: creating ./config.status

config.status: creating Makefile

config.status: creating config.h

config.status: executing depfiles commands
 

8.      執行make命令

-bash-3.2# make

make  all-am

make[1]: Entering directory `/home/whizchen/ParseMsu'

if gcc -DHAVE_CONFIG_H -I. -I. -I.     -g -O2 -MT test_decoder.o -MD -MP -MF ".deps/test_decoder.Tpo" -c -o test_decoder.o test_decoder.c; \

        then mv -f ".deps/test_decoder.Tpo" ".deps/test_decoder.Po"; else rm -f ".deps/test_decoder.Tpo"; exit 1; fi

gcc  -g -O2   -o test_decoder -L.  test_decoder.o -lpthread  -lMsuDecoder -ldl

make[1]: Leaving directory `/home/chenwb/tianri/ParseMsu'

 

執行make命令後,生成了執行文件test_decoder程序

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