Linux入职基础-7.10_自动创建Makefile入门(实战2:deep目录结构实战)

自动创建Makefile实战

自动创建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

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值