三、下面给一个生成Makefile的复杂例子(带有多级目录和配置文件):
1、工程说明:
renwen是工程的顶级目录,conf内是配置文件,include内是头文件,src内有三个目录,main是主程序,first做静态链接库,second做动态链接库。
renwen
|— — — — conf
| — — librenwen.conf
|— — — — include
|— — first.h
|— — second.h
|— — — — src
|— — main
|— — main.c
|— — first
|— — first.c
|— — second
|— — second.c
2、源文件清单:
*********renwen/conf/librenwen.conf************
/usr/local/lib
*********renwen/include/first.h************#include
void first(void);
*********renwen/include/second.h************
#include
void second(void);
*********renwen/src/main/main.c************
#include
#include "first.h"
#include "second.h"
int main(int argc, char* argv[] )
{
printf("Hello, boys(:>!!!\n");
first();
second();
return 0;
}
*********renwen/src/first/first.c************
#include "first.h"
void first(void)
{
printf("Renwen01,first!!!\n");
}
*********renwen/src/second/second.c************
#include "second.h"
void second(void)
{
printf("Renwen02,second!!!\n");
}
3、configure.ac文件清单# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.65])
AC_INIT([david], [1.0], [Renwen0524@163.com])
AM_INIT_AUTOMAKE(david,1.0.0,Renwen0524@163.com)
AC_CONFIG_SRCDIR([include/first.h])
AC_CONFIG_HEADERS([config.h])
# Checks for programs.
AC_PROG_CC
#静态连接库
#AC_PROG_RANLIB
#动态链接库
AC_PROG_LIBTOOL
# Checks for libraries.
# Checks for header files.
AC_CHECK_HEADERS([stdlib.h string.h])
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_OUTPUT( [Makefile
conf/Makefile
include/Makefile
src/first/Makefile
src/main/Makefile
src/second/Makefile])
3、Makefile.am文件清单
*********renwen/Makefile.am************
AUTOMAKE_OPTIONS = foreign
SUBDIRS = include src/first src/second src/main conf
*********renwen/conf/Makefile.am************
configdir = /etc/ld.so.conf.d //这个数据文件将要移动到的目录
config_DATA = libdavid.conf //数据文件名,如有多个文件则这样写config_DATA = test1.dat test2.dat
*********renwen/include/Makefile.am************helloincludedir=$(includedir)
helloinclude_HEADERS=first.h second.h
*********renwen/src/main/Makefile.am************
bin_PROGRAMS=renwen
renwen_SOURCES=main.c
renwen_LDADD=$(top_srcdir)/src/first/libfirst.a
LIBS=-lsecond
INCLUDES=-I$(top_srcdir)/include
renwen_LDFLAGS=-L$(top_srcdir)/src/second
*********renwen/src/first/Makefile.am************noinst_LIBRARIES=libfirst.a
libfirst_a_SOURCES=first.c
INCLUDES=-I$(top_srcdir)/include
*********renwen/src/second/Makefile.am************
lib_LTLIBRARIES=libsecond.la
libsecond_la_SOURCES=second.c
INCLUDES=-I$(top_srcdir)/include
4、安装新的工具
由于使用了动态链接库,所以需要安装libtool:
new@new-desktop:~/linux/renwen$ sudo apt-get install libtool
有时在执行autoconf时,会出现:
configure.ac:13: error: possibly undefined macro: AC_PROG_LIBTOOL If this token and others are legitimate, please use m4_pattern_allow. See the Autoconf documentation.需要安装 libsysfs-dev,之后问题解决。
new@new-desktop:~/linux/renwen$ sudo apt-get install libsysfs-dev
5、生成Makefile的步骤:
new@new-desktop:~/linux/renwen$ autoheader
new@new-desktop:~/linux/renwen$ aclocal
new@new-desktop:~/linux/renwen$ libtoolize (如果没有动态链接库,该步可以省略)
new@new-desktop:~/linux/renwen$ autoconf
new@new-desktop:~/linux/renwen$ automake --add-missing
new@new-desktop:~/linux/renwen$ ./configure
6、运行生成的程序
new@new-desktop:~/linux/renwen$ make
new@new-desktop:~/linux/renwen$ ./src/main/renwen
Hello, boys(:>!!!
Renwen01,first!!!
Renwen02,second!!!
6、把目标文件安装到系统中
new@new-desktop:~/linux/renwen$ sudo make install
有时提示找不到动态链接文件,按照以下方式去修改
new@new-desktop:~/linux/renwen$ sudo vi /etc/ld.so.conf.d/libc.conf
在此文件中写入你需要用的动态链接库的位置 例如mysql就是/usr/local/mysql/lib/mysql 然后,执行命令就解决了找不到动态链接库的问题。
new@new-desktop:~/linux/renwen$ sudo ldconfig
6、构建deb二进制软件包,使用checkinstall工具
new@new-desktop:~/linux/renwen$ sudo apt-get install checkinstall
new@new-desktop:~/linux/renwen$ sudo checkinstall -D make install
这样目录下就生成了david_1.0.0-1_amd64.deb包
7、安装deb二进制软件包
new@new-desktop:~/linux/renwen$ sudo dpkg -i david_1.0.0-1_amd64.deb
new@new-desktop:~/linux/renwen$ renwen
Hello, boys(:>!!!
Renwen01,first!!!
Renwen02,second!!!
8、卸载deb二进制软件包new@new-desktop:~/linux/renwen$ sudo dpkg -i david
new@new-desktop:~/linux/renwen$ renwen
bash: /usr/local/bin/hello: 没有那个文件或目录
至此,关于automake自动生成Makefile已经介绍完了,如果想了解更高级的应用,清参考automake官方文档!!!