Free mind

          Be fresh and eager every morning, and tired and satisfied every night.
          posts - 39, comments - 2, trackbacks - 0, articles - 0
             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          用autotools制作Makefile 和configure文件

          Posted on 2007-02-13 22:55 morphis 閱讀(278) 評論(0)  編輯  收藏 所屬分類: 2. System

          目的 從復(fù)雜的工作中簡化出來。

          網(wǎng)上有一些制作Makfile的文章,只停留在Makefile而已。用autotools的工具相對來說要簡單的多,其它一些介紹autotools文章又有很多漏洞,而且步驟煩瑣。

          制作一個最簡單的helloworld程序:

          現(xiàn)有目錄test

          mkdir src 建立src目錄存放 源代碼
          在src下。
          編輯hello.c文件

          #include <stdio.h>

          int main()
          {
          ? ?? ???printf("hello world\n");
          ? ?? ???return 0;
          }

          在src目錄下建立Makefile.am文件 (src/Makefile.am)

          AUTOMAKE_OPTIONS=foreign
          bin_PROGRAMS = hello
          hello_SOURCES = hello.c
          hello_LDADD = -lpthread (只是測試,實(shí)際不需要連接該庫)

          保存退出

          退到test目錄

          編輯Makefile.am文件 (Makefile.am)

          SUBDIRS = src

          退出保存

          然后
          執(zhí)行
          autoscan
          生成configure.scan文件

          按此編輯此文件

          #? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???-*- Autoconf -*-
          # Process this file with autoconf to produce a configure script.

          AC_PREREQ(2.59)
          AC_INIT(hello,1.0, [miaoquan@nou.com.cn])
          AM_INIT_AUTOMAKE
          AC_CONFIG_SRCDIR([src/hello.c])
          AC_CONFIG_HEADER([config.h])

          # Checks for programs.
          AC_PROG_CC

          # Checks for libraries.
          # FIXME: Replace `main' with a function in `-lpthread':
          AC_CHECK_LIB([pthread], [main])

          # Checks for header files.

          # Checks for typedefs, structures, and compiler characteristics.

          # Checks for library functions.

          #AC_CONFIG_FILES([Makefile
          #? ?? ?? ?? ?? ???src/Makefile])
          AC_OUTPUT(Makefile src/Makefile)

          退出保存
          將此文件更名 mv configure.scan configure.in
          然后執(zhí)行
          touch NEWS README AUTHORS ChangeLog

          然后執(zhí)行
          autoreconf -fvi

          至此生成configure文件
          執(zhí)行configure文件

          生成Makefile文件

          make
          make install
          make uninstall
          make dist
          試驗(yàn)一下吧。
          繼續(xù)完善這個例子,論壇里有人問,如何生成靜態(tài)庫,并連接.

          完善hello.c這個例子


          當(dāng)前目錄
          ? ???|-??src 目錄
          ? ?? ?? ?? ?|-??hello.c 文件
          ? ???|-??include 目錄
          ? ?? ?? ?? ?|-??hello.h文件
          ? ???|-??lib 目錄
          ? ?? ?? ?? ?|-??test.c文件 此文件用來生成 libhello.a

          在當(dāng)前目錄 編寫Makefile.am

          SUBDIRS = lib src

          在include目錄下 編寫hello.h

          extern void print(char *);

          在lib目錄下編寫test.c

          #include <stdio.h>

          void print(char *msg)
          {
          ? ?? ???printf("%s\n",msg);
          }

          在lib目錄下編寫Makefile.am

          noinst_LIBRARIES=libhello.a
          libhello_a_SOURCES=test.c

          這里noinst_LIBRARIES 的意思是生成的靜態(tài)庫 ,不會被make install 安裝
          然后指定libhello.a的源文件test.c


          在src目錄下編寫hello.c

          #include "hello.h"

          int main()
          {
          ? ?? ???print("haha");??//這里是靜態(tài)庫里的print函數(shù)
          ? ?? ???return 0;
          }

          在src目錄下編寫Makefile.am

          INCLUDES= -I../include

          bin_PROGRAMS=hello
          hello_SOURCES=hello.c
          hello_LDADD=../lib/libhello.a

          首先指定頭文件的位置 ../include
          然后指定要生成執(zhí)行文件 hello
          然后指定源代碼文件 hello.c
          最后添加靜態(tài)庫的位置 ../lib/libhello.a


          按照我首篇帖子的方式.
          執(zhí)行autoscan 生成configure.scan

          修改該文件

          按照首篇帖子修改.

          然后不同之處
          需要添加一行:AC_PROG_RANLIB

          #? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???-*- Autoconf -*-
          # Process this file with autoconf to produce a configure script.

          AC_PREREQ(2.59)
          AC_INIT(hello,1.1,[miaoquan@nou.com.cn])
          AM_INIT_AUTOMAKE
          AC_CONFIG_SRCDIR([src/hello.c])
          AC_CONFIG_HEADER([config.h])

          # Checks for programs.
          AC_PROG_CC


          # Checks for libraries.
          AC_PROG_RANLIB
          # Checks for header files.

          # Checks for typedefs, structures, and compiler characteristics.

          # Checks for library functions.

          #AC_CONFIG_FILES([Makefile
          #? ?? ?? ?? ?? ? lib/Makefile
          ??#? ?? ?? ?? ?? ?src/Makefile])
          AC_OUTPUT([Makefile
          ? ?? ?? ?? ?? ?? ?? ?? ?lib/Makefile
          ? ?? ?? ?? ?? ?? ?? ?? ?src/Makefile])

          mv configure.scan configure.in


          touch NEWS README AUTHORS ChangeLog


          執(zhí)行autoreconf -fvi

          生成configure.執(zhí)行configure生成Makefile..


          后面同上...

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 九龙城区| 东乌珠穆沁旗| 寿光市| 汶川县| 龙山县| 栖霞市| 辽宁省| 聂拉木县| 宜兴市| 舒城县| 潮州市| 德保县| 改则县| 佛学| 旌德县| 梅河口市| 大新县| 长沙市| 临清市| 南汇区| 河曲县| 太谷县| 晋宁县| 洛宁县| 三河市| 兴宁市| 集贤县| 宁乡县| 米泉市| 湖州市| 天峻县| 平湖市| 重庆市| 建湖县| 城口县| 二手房| 手机| 仙游县| 江陵县| 绥阳县| 陇西县|