用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文件
CODE:
#include <stdio.h>
int main()
{
? ?? ???printf("hello world\n");
? ?? ???return 0;
}
int main()
{
? ?? ???printf("hello world\n");
? ?? ???return 0;
}
在src目錄下建立Makefile.am文件 (src/Makefile.am)
CODE:
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS = hello
hello_SOURCES = hello.c
hello_LDADD = -lpthread (只是測試,實(shí)際不需要連接該庫)
bin_PROGRAMS = hello
hello_SOURCES = hello.c
hello_LDADD = -lpthread (只是測試,實(shí)際不需要連接該庫)
保存退出
退到test目錄
編輯Makefile.am文件 (Makefile.am)
SUBDIRS = src
退出保存
然后
執(zhí)行
autoscan
生成configure.scan文件
按此編輯此文件
CODE:
#? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???-*- 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)
# 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
CODE:
SUBDIRS = lib src
在include目錄下 編寫hello.h
CODE:
extern void print(char *);
在lib目錄下編寫test.c
CODE:
#include <stdio.h>
void print(char *msg)
{
? ?? ???printf("%s\n",msg);
}
void print(char *msg)
{
? ?? ???printf("%s\n",msg);
}
在lib目錄下編寫Makefile.am
CODE:
noinst_LIBRARIES=libhello.a
libhello_a_SOURCES=test.c
libhello_a_SOURCES=test.c
這里noinst_LIBRARIES 的意思是生成的靜態(tài)庫 ,不會被make install 安裝
然后指定libhello.a的源文件test.c
在src目錄下編寫hello.c
CODE:
#include "hello.h"
int main()
{
? ?? ???print("haha");??//這里是靜態(tài)庫里的print函數(shù)
? ?? ???return 0;
}
int main()
{
? ?? ???print("haha");??//這里是靜態(tài)庫里的print函數(shù)
? ?? ???return 0;
}
在src目錄下編寫Makefile.am
CODE:
INCLUDES= -I../include
bin_PROGRAMS=hello
hello_SOURCES=hello.c
hello_LDADD=../lib/libhello.a
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
CODE:
#? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???-*- 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])
# 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..
后面同上...