Free mind

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

          用autotools制作Makefile 和configure文件

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

          目的 從復雜的工作中簡化出來。

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

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

          現有目錄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 (只是測試,實際不需要連接該庫)

          保存退出

          退到test目錄

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

          SUBDIRS = src

          退出保存

          然后
          執行
          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
          然后執行
          touch NEWS README AUTHORS ChangeLog

          然后執行
          autoreconf -fvi

          至此生成configure文件
          執行configure文件

          生成Makefile文件

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

          完善hello.c這個例子


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

          在當前目錄 編寫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 的意思是生成的靜態庫 ,不會被make install 安裝
          然后指定libhello.a的源文件test.c


          在src目錄下編寫hello.c

          #include "hello.h"

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

          在src目錄下編寫Makefile.am

          INCLUDES= -I../include

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

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


          按照我首篇帖子的方式.
          執行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


          執行autoreconf -fvi

          生成configure.執行configure生成Makefile..


          后面同上...
          主站蜘蛛池模板: 四会市| 遵义县| 白朗县| 新竹市| 城市| 呼图壁县| 千阳县| 朝阳县| 岑巩县| 永昌县| 鸡泽县| 鄂托克前旗| 集贤县| 余姚市| 东山县| 余干县| 西华县| 贵阳市| 桃源县| 蚌埠市| 辉南县| 临沂市| 上犹县| 广灵县| 新和县| 类乌齐县| 郯城县| 陈巴尔虎旗| 麦盖提县| 县级市| 天柱县| 建平县| 镇远县| 车险| 广州市| 铜川市| 乡宁县| 莲花县| 宜宾市| 岚皋县| 旬邑县|