自动创建Makefile入门(实战3: shallow目录结构)
项目目标:
自动创建Makefile,生成可执行文件crm,版本2.0。
shallow目录结构:
crmpro2
|-- main.c
|--include
| |--defuser.h
| |--user.c
| |--admin.c
顶级目录crmpro2:
该目录下存在一个主文件main.c和一个目录include,其中main.c的main()调用defuser.h头文件声明的两个方法。
include目录:
defuser.h头文件声明了list_user()、list_admin()方法;
user.c中实现了list_user()方法;
admin.c中实现了list_admin()方法。
头文件:defuser.h
/*defuser.h*/
void list_user();
void list_admin();
源码文件有: main.c、user.c 、admin.c
代码清单如下:
/*main.c*/
#include"stdio.h"
#include "./include/defuser.h"
int main()
{
printf("main() isruning!\n");
list_user();
list_admin();
return 1;
}
/*user.c*/
#include"stdio.h"
#include"../include/defuser.h"
void list_user()
{
printf("This is list_user(),it isin user.c!,and user.c is liblog.a\n");
}
/*admin.c*/
#include"stdio.h"
#include"../include/defuser.h"
void list_admin()
{
printf("This is list_admin(),it is inuser.c!,and admin.c is liblog.a\n");
}
执行步骤:
1) 执行autoscan命令, 产生configure.scan文件
[root@localhost crmpro2]# ls
include main.c
[root@localhost crmpro2]# autoscan
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1
[root@localhost crmpro2]# ls
autoscan.log configure.scan include main.c
2) 将configure.scan 文件重命名为configure.in,并修改configure.in文件
[root@localhost crmpro2]# mv configure.scanconfigure.in
[root@localhost crmpro2]# vim configure.in
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.59)
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADER([config.h])
AM_INIT_AUTOMAKE(crm,2.0)
# Checks for programs.
AC_PROG_CC
#使用静态库编译,需要此宏定义
AC_PROG_RANLIB
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_OUTPUT(Makefile include/Makefile)
[root@localhost crmpro2]# ls
autoscan.log configure.in include main.c
3) 执行aclocal命令
[root@localhost crmpro2]# aclocal
[root@localhost crmpro2]# ls
aclocal.m4 autom4te.cache autoscan.log configure.in include main.c
4) 执行autoconf命令
[root@localhost crmpro2]# autoconf
[root@localhost crmpro2]# ls
aclocal.m4 autoscan.log configure.in main.c
autom4te.cache configure include
5)新建Makefile.am文件
在crmpro2目录下Makefile.am文件内容:
[root@localhost crmpro2]# vim Makefile.am
AUTOMAKE_OPTIONS=foreign
SUBDIRS=include
bin_PROGRAMS=crm
crm_SOURCES=main.c
#指链接时所需要的库文件
crm_LDADD=include/liblog.a
[root@localhost crmpro2]# ls
aclocal.m4 autoscan.log configure.in main.c
autom4te.cache configure include Makefile.am
在crmpro2/include目录下Makefile.am文件内容:
[root@localhost include]# vim Makefile.am
AUTOMAKE_OPTIONS=foreign
#只编译库文件,库文件不安装到系统
noinst_LIBRARIES=liblog.a
liblog_a_SOURCES=defuser.h user.c admin.c
[root@localhost include]# ls
admin.c defuser.h Makefile.am user.c
6) 执行autoheader命令
[root@localhost crmpro2]# autoheader
[root@localhost crmpro2]# ls
aclocal.m4 autoscan.log configure include Makefile.am
autom4te.cache config.h.in configure.in main.c
7) 执行automake --add-missing命令
[root@localhost crmpro2]# automake --add-missing
configure.in: installing`./install-sh'
configure.in: installing`./missing'
include/Makefile.am:installing `./depcomp'
[root@localhost crmpro2]# ls
aclocal.m4 config.h.in depcomp main.c missing
autom4te.cache configure include Makefile.am
autoscan.log configure.in install-sh Makefile.in
8) 执行./confiugre脚本
[root@localhost crmpro2]# ./configure
[root@localhost crmpro2]# ls
aclocal.m4 config.h.in configure.in main.c missing
autom4te.cache config.log depcomp Makefile stamp-h1
autoscan.log config.status include Makefile.am
config.h configure install-sh Makefile.in
[root@localhost crmpro2]#ls include/
admin.c defuser.h Makefile Makefile.am Makefile.in user.c
9)执行“make”命令来编译、生成可执行程序crm;运行程序./crm
[root@localhost crmpro2]# make
[root@localhost crmpro2]# ls
aclocal.m4 config.h.in configure.in install-sh Makefile.am
autom4te.cache config.log crm main.c Makefile.in
autoscan.log config.status depcomp main.o missing
config.h configure include Makefile stamp-h1
[root@localhost crmpro2]#ls include/
admin.c defuser.h Makefile Makefile.in user.o
admin.o liblog.a Makefile.am user.c
[root@localhost crmpro2]# ./crm
main() is runing!
This is list_user(),it isin user.c!,and user.c is liblog.a
This is list_admin(),it isin user.c!,and admin.c is liblog.a
符合程序预期结果。
10)安装软件crm1:make install;打包发布软件
[root@localhost crmpro2]# make install
[root@localhost crmpro2]# find /usr -name crm
/usr/local/bin/crm
[root@localhost crmpro2]# make dist
[root@localhost crmpro2]# ls
aclocal.m4 config.log crm-2.0.tar.gz main.o stamp-h1
autom4te.cache config.status depcomp Makefile
autoscan.log configure include Makefile.am
config.h configure.in install-sh Makefile.in
config.h.in crm main.c missing