flyonok

统计

留言簿(7)

ACE

book

boost

bsd

c study

c++

code download

codeblock

computer clound

Eclipse

embed system

erlang

ET++

gtk

ic card

java

KDE

libevent

linux

linux--MM

mysql

network education

one card

oracle

pcap relation

php

powerbuilder

python

QT

software config

software test

SQL server

UML

wireless

wxwidgets

陈宾

阅读排行榜

评论排行榜

用autotools 制作configure和makefile

我们在linux编程中经常要编写makefile文件,makefile的编写比较底层,不好理解,新手不容易掌握.可是用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 # for test,not necessary
退到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");
  
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
修改configure.in
需要添加一行:AC_PROG_RANLIB

然后执行autoreconf -fiv
执行./configure
make
make install

参考资料:IBM automake and autoconf
automake and c++

posted on 2010-08-26 15:07 flyonok 阅读(500) 评论(0)  编辑 收藏 引用 所属分类: linux


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理