生成Makefile自动化编译文件

 makefile带来的好处就是——“自动化编译”,一旦写好,只需要一个make命令,整个工程完全自动编译,极大的提高了软件开发的效率。make是一个命令工具,是一个解释makefile中指令的命令工具,
一般来说,大多数的IDE都有这个命令,比如:Delphi的make,Visual C++的nmake,Linux下GNU的make。可见,makefile都成为了一种在工程方面的编译方法。

    那么如何才能生成Makefile文件呢??好吧,让我们一起进入今天的正题吧!

 1、首先生成一个目录:mkdir /root/helloworld
2、生成一个文件:touch helloworld.c ,编译vi helloworld.c
#include<stdio.h>

  int main(int argc,char**argv){

     printf("hello,yuchao!!\n");
     return 0;
}

 

执行后在hellowrold目录下会生成一个文件:configure.scan,我们可以拿它作为configure.in的蓝本。
[root@localhost helloworld]# autoscan
[root@localhost helloworld]# ls
autoscan.log  configure.scan  helloworld.c
现在将configure.scan改名为configure.in,并且编辑它,按下面的内容修改,去掉无关的语句:
[root@localhost helloworld]# mv configure.scan  configure.in
[root@localhost helloworld]# ls
autoscan.log  configure.in  helloworld.c
编译configure.in文件:vi configure.in
#-*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_INIT(helloworld.c)
AM_INIT_AUTOMAKE(helloworld.c,1.0)

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT(Makefile)
[root@localhost helloworld]# aclocal
[root@localhost helloworld]# ls
aclocal.m4  autom4te.cache  autoscan.log  configure.in  helloworld.c
  aclocal根据configure.in文件的内容,自动生成aclocal.m4文件。aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”。
  m4是一个宏处理器。将输入拷贝到输出,同时将宏展开。宏可以是内嵌的,也可以是用户定义的。除了可以展开宏,m4还有一些内建的函数,用来引用文件,执行命令,整数运算,文本操作,
循环等。m4既可以作为编译器的前端,也可以单独作为一个宏处理器。 [root@localhost helloworld]# ls aclocal.m4 autom4te.cache autoscan.log configure configure.
in helloworld.c configure.in内容是一些宏定义,这些宏经autoconf处理后会变成检查系统特性、环境变量、软件必须的参数的shell脚本。 autoconf 是用来生成自动配置软件源代码脚本(configure)的工具。configure脚本能独立于autoconf运行,且在运行的过程中,不需要用户的干预。 要生成configure文件,你必须告诉autoconf如何找到你所用的宏。方式是使用aclocal程序来生成你的aclocal.m4。 [root@localhost helloworld]# touch Makefile.am [root@localhost helloworld]# vi Makefile.am
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=helloworld.c
automake会根据你写的Makefile.am来自动生成Makefile.in
Makefile.am中定义的宏和目标,会指导automake生成指定的代码。例如,宏bin_PROGRAMS将导致编译和连接的目标被生成。 [root@localhost helloworld]# automake --add-missing configure.in:5: installing `./missing' configure.in:5: installing `./install-sh' Makefile.am: installing `./depcomp' [root@localhost helloworld]# ./configure checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for a thread-safe mkdir -p... /bin/mkdir -p checking for gawk... gawk checking whether make sets $(MAKE)... yes checking for gcc... gcc checking for C compiler default output file name... a.out checking whether the C compiler works... yes checking whether we are cross compiling... no checking for suffix of executables... checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether gcc accepts -g... yes checking for gcc option to accept ISO C89... none needed checking for style of include used by make... GNU checking dependency style of gcc... gcc3 configure: creating ./config.status config.status: creating Makefile config.status: executing depfiles commands [root@localhost helloworld]# ls aclocal.m4 config.log configure.in install-sh Makefile.in autom4te.cache config.status depcomp Makefile missing autoscan.log configure helloworld.c Makefile.am [root@localhost helloworld]# make gcc-DPACKAGE_NAME=\"FULL-PACKAGE-NAME\" -DPACKAGE_TARNAME=\"full-package-name\"
-DPACKAGE_VERSION=\"VERSION\" -DPACKAGE_STRING=\"FULL-PACKAGE-NAME\ VERSION\"
-DPACKAGE_BUGREPORT=\"BUG-REPORT-ADDRESS\" -DPACKAGE=\"helloworld.c\" -DVERSION=\"1.0\" -I.
-g -O2 -MT helloworld.o -MD -MP -MF .deps/helloworld.Tpo -c -o helloworld.o helloworld.c
mv -f .deps/helloworld.Tpo .deps/helloworld.Po gcc -g -O2 -o helloworld helloworld.o [root@localhost helloworld]# ls aclocal.m4 config.log configure.in helloworld.c Makefile missing autom4te.cache config.status depcomp helloworld.o Makefile.am autoscan.log configure helloworld install-sh Makefile.in
编译可执行的文件:
[root@localhost helloworld]# ./helloworld hello,yuchao!! [root@localhost helloworld]#

 

转载于:https://www.cnblogs.com/yu0312chao/p/3799588.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值