linux automake

linux下写程序,编译程序,熟练了也是很惬意的事,但总有不熟练的时候,那就是总觉得什么都麻烦。比如说写Makefile文件。写Makefile文件,文件一多自然是个体力活。于是有了automake这个工具。下面介绍下如何使用此工具。网上文章已经有很多,不过大多是单目录下的,或是多目录但又是基于库形式的编译,可实际情况,有时候我们并不需要库,只是想编译个程序出来而已。于是详说之。

  条件:没有安装automake,请先装之。

  源码目录结构:

-- src
    |-- logic
    |   |-- logic.c
    |   `-- logic.h
    |-- main
    |   `-- main.c
    `-- ui
        |-- ui.c
        `-- ui.h


        main目录下的main.c调用logic和ui目录下的相关函数。

  main.c代码

#include <stdio.h>
#include "ui.h"
#include "logic.h"
int main(int argc, char *argv[])
{
	int a;
	a = com_add(1, 3);
	printf("a = %d\n", a);
	show_window("this is a test");
	return 0;
}
 logic.c的代码

#include "logic.h"
int com_add(int a, int b)
{
	return a*a + b*b;
}

  logic.h的代码

#ifndef __LOGIC_H
#define __LOGIC_H
int com_add(int a, int b);
#endif

ui.c的代码

#include <stdio.h>
#include "ui.h"
void show_window(char *title)
{
	if (title){
		printf("%s\n", title);
	}
}

ui.h的代码

#ifndef __UI_H
#define __UI_H
void show_window(char *title);
#endif

--------------------------------

步骤1 autoscan

   在src目录下运行autoscan命令,目录下生成两个文件autoscan.log  configure.scan。将configure.scan重命名为configure.in。并进行编辑。

configure.in原内容为:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([main/main.c])
AC_CONFIG_HEADERS([config.h])

# 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

修改其中内容如下:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.65])
AC_INIT(maxwell, 0.1, askeyes@csdn.net)
AC_CONFIG_SRCDIR([main/main.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE(maxwell,0.1)
# 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)


其中AM_INIT_AUTOMAKE(maxwell,0.1)是为关键,表明将用automake生成Makefile文件。

步骤2 aclocal

在src目录下运行aclocal命令。此时会生成aclocal.m4和autom4te.cache目录。

步骤3 autoconf

在src目录下运行autoconf命令,生成autoscan.log  configure

步骤4 autoheader

在src目录下运行autoheader命令,生成config.h.in

步骤5 创建Makefile.am文件

在src目录下创建Makefile.am文件,内容下:

AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS= maxwell
maxwell_SOURCES= main/main.c logic/logic.c ui/ui.c
INCLUDES = -Ilogic -Iui

其中bin_PROGRAMS表明生成的应用程序名,main_SOURCES中的前导main是为程序名,根据bin_PROGRAMS名变化而修改。main_SOURCES所对应为应用程序生成所依赖的源文件。此中依赖为main/main.c logic/lobic.c ui/ui.c。INCLUDES 为头文件信息。另有其他变量可以查询相关资料以获得更为详细的信息。

步骤6 automake

在src目录下运行automake,生成Makefile.in文件。一般会提示用automake --add-missing命令来生成相应的缺失文件,如还提示缺少文件则相应创建相应文件如AUTHOR文件等。


步骤7 ./configure

在src目录下运行./configure, 很熟悉了,一般下载下来的源码包运行下./configure就可以生成Makefile文件了。此处相同。

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 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 for style of include used by make... GNU
checking dependency style of gcc... gcc3
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands

步骤8 make
(CDPATH="${ZSH_VERSION+.}:" && cd . && /bin/sh /home/askeyes/cp/src/missing --run autoheader)
rm -f stamp-h1
touch config.h.in
cd . && /bin/sh ./config.status config.h
config.status: creating config.h
config.status: config.h is unchanged
make  all-am
make[1]: Entering directory `/home/askeyes/cp/src'
gcc  -g -O2   -o maxwell main.o logic.o ui.o  
make[1]: Leaving directory `/home/askeyes/cp/src'

最终生成maxwell 文件,执行之。





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值