自动创建Makefile入门(实战2:deep目录结构)
项目目标:
自动创建Makefile,生成可执行文件crm,版本3.0。
deep目录结构:
crmpro3
|--include
| |--defuser.h
| |--f1.h
| |--f2.h
|--lib
| |--user.c
| |--admin.c
|--src
| |--main.c
| |--f1.c
| |--f2.c
顶级目录crmpro3,该目录下存在三个目录src、lib和include。
include目录:
defuser.h头文件声明了list_user()、list_admin()方法;
f1.h头文件声明了func_on()方法
f2.h头文件声明了func_two()方法
lib目录:
user.c中实现了list_user()方法,
admin.c中实现了list_admin()方法;
src目录:
f1.c文件实现了func_on()方法
f2.c文件实现了func_two()方法
main.c中的main调用这两个方法。
头文件:def1.h def2.h defuser.h
定义如下:
/*def1.h*/
void func_one();
/*def2.h*/
void func_two();
/*defuser.h*/
void list_user();
void list_admin();
源码文件有: main.c、user.c 、admin.c 、f1.c 、f2.c
/*main.c*/
#include "stdio.h"
#include "../include/def1.h"
#include "../include/def2.h"
#include "../include/defuser.h"
int main()
{
printf("main() isruning!\n");
func_one();
func_two();
list_user();
list_admin();
return 1;
}
/*user.c*/
#include "stdio.h"
#include "../include/defuser.h"
void list_user()
{
printf("This islist_user(),it is in user.c!,and user.c is libuser.a\n");
}
/*admin.c*/
#include "stdio.h"
#include "../include/defuser.h"
void list_admin()
{
printf("This islist_admin(),it is in user.c!,and admin.c is libadmin.a\n");
}
/*f1.c*/
#include "stdio.h"
#include "../include/def1.h"
void func_one()
{
printf("This isfunc_one(),it is in f1.c!\n");
}
/*f2.c*/
#include "stdio.h"
#include "../include/def2.h"
void func_two()
{
printf("This isfunc_two(),it is in f2.c!\n");
}
执行步骤:
1) 执行autoscan命令, 产生configure.scan文件
[root@localhost crmpro3]# pwd
/root/crmpro3
[root@localhost crmpro3]# ls
include lib src
[root@localhost crmpro3]# autoscan
[root@localhost crmpro3]# ls
autoscan.log configure.scan include lib src
2) 将configure.scan 文件重命名为configure.in,并修改configure.in文件
[root@localhost crmpro3]# mv configure.scan configure.in
[root@localhost crmpro3]# vim configure.in
# -*- Autoconf -*-
# Process this file withautoconf to produce a configure script.
AC_PREREQ(2.59)
AC_INIT(FULL-PACKAGE-NAME,VERSION, BUG-REPORT-ADDRESS)
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADER([config.h])
AM_INIT_AUTOMAKE(crm,3.0)
# Checks for programs.
AC_PROG_CC
AC_PROG_RANLIB
AC_PROG_MAKE_SET
# Checks for libraries.
# Checks for header files.
# Checks for typedefs,structures, and compiler characteristics.
# Checks for libraryfunctions.
AC_OUTPUT(Makefile include/Makefilelib/Makefile src/Makefile)
3) 执行aclocal命令
[root@localhost crmpro3]# aclocal
[root@localhost crmpro3]# ls
aclocal.m4 autom4te.cache autoscan.log configure.in include lib src
4) 执行autoconf命令
[root@localhost crmpro3]# autoconf
[root@localhost crmpro3]# ls
aclocal.m4 autoscan.log configure.in lib
autom4te.cache configure include src
5)新建Makefile.am文件
在crmpro3文件夹下创建Makefile.am文件内容如下:
AUTOMAKE_OPTIONS=foreign
SUBDIRS= include lib src
在include文件夹下创建Makefile.am文件内容如下:
EXTRA_DIST=defuser.h def1.h def2.h
在lib文件夹下创建Makefile.am文件内容如下:
AUTOMAKE_OPTIONS=foreign
INCLUDES=-I../include
noinst_LIBRARIES=liblog.a
liblog_a_SOURCES= user.c admin.c
在src文件夹下创建Makefile.am文件内容如下:
AUTOMAKE_OPTIONS=foreign
INCLUDES= -I../include
bin_PROGRAMS=crm
crm_SOURCES=main.c f1.c f2.c
crm_LDADD=../lib/liblog.a
6) 执行autoheader命令
[root@localhost crmpro3]# autoheader
7) 执行automake --add-missing命令
[root@localhost crmpro3]# automake--add-missing
[root@localhost crmpro3]# ls
aclocal.m4 config.h.in depcomp lib missing
autom4te.cache configure include Makefile.am src
autoscan.log configure.in install-sh Makefile.in
8) 执行./confiugre脚本
[root@localhost crmpro3]# ./configure
[root@localhost crmpro3]# ls
aclocal.m4 config.h.in configure.in lib missing
autom4te.cache config.log depcomp Makefile src
autoscan.log config.status include Makefile.am stamp-h1
config.h configure install-sh Makefile.in
9)执行“make”命令来编译、生成可执行程序crm;运行程序./crm
[root@localhost crmpro3]# make
[root@localhost crmpro3]# src/crm
main() is runing!
This is func_one(),it is inf1.c!
This is func_two(),it is inf2.c!
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 crmpro3]# make install
root@localhost crmpro3]# make install
[root@localhost crmpro3]# ls
aclocal.m4 config.h.in crm-3.0.tar.gz Makefile src
AUTHORS config.log depcomp Makefile.am stamp-h1
autom4te.cache config.status include Makefile.in
autoscan.log configure INSTALL missing
ChangeLog configure.in install-sh NEWS
config.h COPYING lib README
自动创建Makefile实战

834

被折叠的 条评论
为什么被折叠?



