自动创建Makefile入门(实战1:Flat目录结构)
项目目标:
自动创建Makefile,生成可执行文件crm,版本1.0。
Flat目录结构:
crmpro1
|-- def1.h
|-- main.c
|-- f1.c
项目的顶级目录crmpro1,该目录下存在三个文件。def1.h头文件声明了func_one()方法;f1.c中实现了func_one ()方法;main.c中的main调用了func_one ()方法。
头文件:def1.h
定义如下:
/*def1.h*/
void func_one();
源码文件有:f1.cmain.c
其定义如下:
/*f1.c*/
#include "stdio.h"
#include "def1.h"
void func_one()
{
printf("This isfunc_one(),it is in f1.c!\n");
}
/*mian.c*/
#include "stdio.h"
#include "def1.h"
int main()
{
printf("main() isruning!\n");
func_one();
return 1;
}
执行步骤
1) 执行autoscan命令
[root@localhost crmpro1]# pwd
/root/crmpro1
[root@localhost crmpro1]# ls
def1.h f1.c main.c
[root@localhost crmpro1]# autoscan
autom4te: configure.ac: no such file or directory
autoscan: /usr/bin/autom4te failed with exit status: 1
[root@localhost crmpro1]# ls
autoscan.log configure.scan def1.h f1.c main.c
2) 将configure.scan 文件重命名为configure.in,并修改configure.in文件
[root@localhost crmpro1]# mv configure.scanconfigure.in
[root@localhost crmpro1]# ls
autoscan.log configure.in def1.h f1.c main.c
##修改configure.in文件,如下:
#修改AC_CONFIG_SRCDIR([main.c])
#加入:AM_INIT_AUTOMAKE(crm,1.0)
#修改:AC_OUTPUT([Makefile )
[root@localhost crmpro1]# vim configure.in
# -*- Autoconf -*-
# Process this file withautoconf to produce a configure script.
AC_PREREQ(2.59)
#定义软件的基本信息,全称,版本号以及报告BUG时的邮箱地址
AC_INIT(FULL-PACKAGE-NAME,VERSION, BUG-REPORT-ADDRESS)
# 所指定的源码文件是否存在,来确定源码目录的有效性
AC_CONFIG_SRCDIR([main.c])
# 用于生成config.h文件,以便autoheader使用
AC_CONFIG_HEADER([config.h])
AM_INIT_AUTOMAKE(crm,1.0)
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
# Checks for header files.
# Checks for typedefs,structures, and compiler characteristics.
# Checks for libraryfunctions
# 创建输出文件
AC_OUTPUT(Makefile)
3) 执行aclocal命令
[root@localhost crmpro1]# aclocal
[root@localhost crmpro1]# ls
aclocal.m4 autom4te.cache autoscan.log configure.in def1.h f1.c main.c
4) 执行autoconf命令
[root@localhost crmpro1]# autoconf
[root@localhost crmpro1]# ls
aclocal.m4 autoscan.log configure.in f1.c
autom4te.cache configure def1.h main.c
5) 在crmpro1目录下新建Makefile.am文件
[root@localhost crmpro1]# vim Makefile.am
AUTOMAKE_OPTIONS=foreign
#如果有多个执行程序,用空格分开
bin_PROGRAMS=crm
#定义crm1所需源文件,多个可执行文件分别定义
crm_SOURCES=main.c f1.c def1.h
6) 执行autoheader命令
[root@localhost crmpro1]# touch NEWS README ChangeLog AUTHORS
[root@localhost crmpro1]# cp /usr/share/automake-1.9/depcomp ./
[root@localhost crmpro1]# cp /usr/share/automake-1.9/compile ./
[root@localhost crmpro1]# autoheader
[root@localhost crmpro1]#ls
aclocal.m4 autoscan.log config.h.in def1.h main.c README
AUTHORS ChangeLog configure depcomp Makefile.am
autom4te.cache compile configure.in f1.c NEWS
7) 执行automake --add-missing命令
[root@localhost crmpro1]# automake --add-missing
configure.in: installing`./install-sh'
configure.in: installing`./missing'
Makefile.am:3: variable`hello_SOURCES' is defined but no program or
Makefile.am:3: library has`hello' as canonic name (possible typo)
[root@localhost crmpro1]# ls
aclocal.m4 ChangeLog configure.in install-sh missing
AUTHORS compile def1.h main.c NEWS
autom4te.cache config.h.in depcomp Makefile.am README
autoscan.log configure f1.c Makefile.in
8) 执行./confiugre脚本
[root@localhost crmpro1]# ./configure
checking for aBSD-compatible install... /usr/bin/install -c
checking whether buildenvironment is sane... yes
checking for gawk... gawk
checking whether make sets$(MAKE)... yes
checking for gcc... gcc
checking for C compilerdefault output file name... a.out
checking whether the Ccompiler works... yes
checking whether we arecross compiling... no
checking for suffix ofexecutables...
checking for suffix ofobject files... o
checking whether we areusing the GNU C compiler... yes
checking whether gccaccepts -g... yes
checking for gcc option toaccept ANSI C... none needed
checking for style ofinclude used by make... GNU
checking dependency styleof gcc... gcc3
checking for ranlib...ranlib
configure: creating./config.status
config.status: creating Makefile
config.status: creatingconfig.h
config.status: executingdepfiles commands
[root@localhost crmpro1]# ls
aclocal.m4 ChangeLog config.log def1.h main.c missing
AUTHORS compile config.status depcomp Makefile NEWS
autom4te.cache config.h configure f1.c Makefile.am README
autoscan.log config.h.in configure.in install-sh Makefile.in stamp-h1
9)执行“make”命令来编译、生成可执行程序crm;运行程序./crm
[root@localhost crmpro1]# make
[root@localhost crmpro1]# ls
aclocal.m4 config.h crm install-sh missing
AUTHORS config.h.in crm1-1.0.tar.gz main.c NEWS
autom4te.cache config.log def1.h main.o README
autoscan.log config.status depcomp Makefile stamp-h1
ChangeLog configure f1.c Makefile.am
compile configure.in f1.o Makefile.in
[root@localhost crmpro1]# ./crm
main() is runing!
This is func_one(),it is inf1.c!
10)安装软件crm1:make install;打包发布软件
[root@localhost crmpro1]# make install
make[1]: Entering directory`/root/crmpro1'
test -z"/usr/local/bin" || mkdir -p -- "/usr/local/bin"
/usr/bin/install -c 'crm''/usr/local/bin/crm'
make[1]: Nothing to be donefor `install-data-am'.
make[1]: Leaving directory`/root/crmpro1'
[root@localhost crmpro1]# find /usr/local -name crm
/usr/local/bin/crm #默认安装目录
[root@localhost crmpro1]# make dist
[root@localhost crmpro1]# ls
aclocal.m4 config.h crm install-sh missing
AUTHORS config.h.in crm-1.0.tar.gz main.c NEWS
autom4te.cache config.log def1.h main.o README
autoscan.log config.status depcomp Makefile stamp-h1
ChangeLog configure f1.c Makefile.am
compile configure.in f1.o Makefile.in
// crm-1.0.tar.gz就是我们要对外发布的打包软件