在linux下利用autotools创建makefile文件

首先利用which命令查看是否安装了aclocal、autoscan、autoconf、autoheader、automake工具————eg:#which aclocal

创建过程

1、#autocsan

运行后会生成configure.scan与autoscan.log如下图:

然后将configure.scan修改为configure.in

并将configure.in文件修改如下图:

改前:

改后

修改/添加以下三句:

AC_INIT(hello,1.0, BUG-REPORT-ADDRESS)
AM_INIT_AUTOMAKE(hello,1.0)

AC_CONFIG_FILES([makefile])

其他都不用改

2、#aclocal

执行后会生成 autom4te.cache  aclocal.m4

3、#autoconf

执行后会生成configure

4、#autoheader

执行后会生成config.h.in

5、新建一个makefile.am文件,并输入如下

其中,bin_PROGRAMS = hello 的hello是要产生的执行文件名,而hello_SOURCES = hello.c test.c test.h test2.c test2.h 表明的是这个“hello”这个执行程序所需要的原始文件

6、#automaker -a (或者 automake --add-missing)

你会发现此时多了一个configure.in 文件

注意:当运行此步出现以下问题时出现以下问题

则需要把整个目录移动到linux系统下的目录,不要在linux-XP共享目录下创建

详解:http://hi.baidu.com/huaan031/blog/item/11849039056fc6c4d5622595.html


7、#./configure 

以上的是默认是用gcc编译器编译的,如果想要交叉编译器编译则第7步要改为:

(参考:http://blog.chinaunix.net/space.php?uid=7492248&do=blog&id=2623623

#./configure CC=/opt/FriendlyARM/toolschain/4.5.1/bin/arm-linux-gcc --host=arm --target=arm

用户可以通过给configure传递命令行参数而指定系统类型。在交叉编译中必须这样做。

在大多数交叉编译的复杂情况下,要涉及到三种系统类型。用于指定他们的选项是:

--build = build-type 对包进行配置和编译的系统类型(很少用到); --host = host-type包

运行的系统类型; --target = target-type 包中任何编译器将生成的代码的系统类型。


8、#make

生成可执行文件hello,这样你就可以执行这一程序

生成的makefile文件还有一下功能

1、#make install

把改程序安装到系统目录中,可达到如下目的

     #make uninstall (卸载)

2、#make clean (清除可执行文件与*.o文件)

3、make dist

将程序和相关文档打包为一个压缩文档,如下

附件:

hello.c

#include"test.h"
#include"test2.h"
void main()
{
        printf("Hello,Linux world\n");
        test("test is successful");
        test2();
}

test.c

#include"test.h"
void test(char *p)
{
        printf("%s\n",p);
}

test.h

#ifndef __TEST_H__
#define __TEST_H__
#include<stdio.h>
void test(char *p);
#endif

test2.c

#include<stdio.h>
#include"test2.h"
void test2()
{
        int i,m = 10;
        i = rand();
        printf("test2: %d\n",m+i);
}

test2.h

#ifndef __TEST2_H__
#define __TEST2_H__
#include<math.h>
void test2();
#endif


更加详细讲解的地址:http://www.ibm.com/developerworks/cn/linux/l-makefile/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值