前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >autoMake工具使用实例(Hello, World!)

autoMake工具使用实例(Hello, World!)

作者头像
李小白是一只喵
发布2020-04-24 12:56:33
1.2K0
发布2020-04-24 12:56:33
举报
文章被收录于专栏:算法微时光

定义

Makefile的 基本结构不是 很复杂,但当一个程序开发人员开始写Makefile时,经常会怀疑自己写的 是 否符合惯例,而且自己写的 Makefile经常和自己的 开发环境相关联,当系统环境变量或路径发生了变化后,Makefile可能还要跟着修改.这样就造成了手工书写Makefile的 诸多问题,automake恰好能很好地帮助我们解决这些问题. 使用automake,程序开发人员只需要写一些简单的 含有预定义宏的 文件,由autoconf根据一个宏文件生成configure,由automake根据另一个宏文件生成Makefile.in,再使用configure依据Makefile.in来生成一个符合惯例的 Makefile.下面我们将详细介绍Makefile的 automake生成方法.

使用automake命令执行顺序

代码语言:javascript
复制
aclocal; 
autoconf; 
automake --add-missing; 
./configure; 
make; 
./helloworld

当然事先要先安装相关的软件库。这里不再多说。 下面用一个例子来说明autoMake使用方法。

关于hello,world.

首先创建个hello, world的代码: 文件名helloworld.c

代码语言:javascript
复制
#include <stdio.h>

int main()
{
    printf("hello, world!\n");
    return 0;
}

执行

代码语言:javascript
复制
$ autoscan
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/\${ <-- HERE [^\}]*}/ at /usr/bin/autoscan line 361.
$ ls
autoscan.log  configure.scan  helloworld.c

将configure.scan复制为configure.ac,并修改其中的内容为:

代码语言:javascript
复制
$ ls
autoscan.log  configure.ac  configure.scan  helloworld.c
$ cat configure.ac 
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_INIT(helloworld.c)
AM_INIT_AUTOMAKE(helloworld, 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)

执行aclocal生成aclocal.m4文件:

代码语言:javascript
复制
$ aclocal
$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure.ac  configure.scan  helloworld.c

执行autoconf生成configure文件:

代码语言:javascript
复制
$ autoconf
$ ls
aclocal.m4  autom4te.cache  autoscan.log  configure  configure.ac  configure.scan  helloworld.c

然后开始创建Makefile.am文件:

代码语言:javascript
复制
$ cat Makefile.am 
UTOMAKE_OPTIONS=foreign
bin_PROGRAMS=helloworld
helloworld_SOURCES=helloworld.c

有了Makefile.am文件,我们就可以使用automake了:

代码语言:javascript
复制
$ $ automake --add-missing
configure.ac:4: warning: AM_INIT_AUTOMAKE: two- and three-arguments forms are deprecated.  For more info, see:
configure.ac:4: http://www.gnu.org/software/automake/manual/automake.html#Modernize-AM_005fINIT_005fAUTOMAKE-invocation

$ ls
aclocal.m4      autoscan.log  configure     configure.scan  depcomp       INSTALL     Makefile.am
autom4te.cache  compile       configure.ac  COPYING         helloworld.c  install-sh  missing

然后在执行configure,生成Makefile:

代码语言:javascript
复制
$ ./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... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
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 whether gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: executing depfiles commands
$ ls
aclocal.m4      compile        configure       COPYING       INSTALL     Makefile.am
autom4te.cache  config.log     configure.ac    depcomp       install-sh  Makefile.in
autoscan.log    config.status  configure.scan  helloworld.c  Makefile    missing

有了Makefile,就可以直接make了:

代码语言:javascript
复制
$ make
gcc -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"helloworld\" -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  
$ ./helloworld 
hello, world!

不得不说,这个工具的强大,当然也生成了许多的文件。不过还是很好的。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 定义
  • 使用automake命令执行顺序
  • 关于hello,world.
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档