- 浏览: 800233 次
- 性别:
- 来自: 哈尔滨
文章分类
最新评论
-
doloveme:
$sudo cp MONACO_Linux.ttf /usr/ ...
terminator 的配置 -
腾讯rep:
lowser
RSA加密算法c++简单实现 -
csuhanyong:
好YD啊,好YD
一图流:机械键盘各轴区别 -
doloveme:
1。其中:extman ,extmail下载http://ww ...
Ubuntu下安装postfix+extmail邮件服务器 -
doloveme:
repo sync出现“fatal: '../platform ...
Android 编译环境配置搭建(Ubuntu 12.04)及环境导致编译错误QA
作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便。一般情况下,大家都是手工写一个简单Makefile,如果要想写出一个符合自由软件惯例的Makefile就不那么容易了。
在本文中,将给大家介绍如何使用autoconf和automake两个工具来帮助我们自动地生成符合自由软件惯例的Makefile,这样就可以象常
见的GNU程序一样,只要使用“./configure”,“make”,“make
instal”就可以把程序安装到Linux系统中去了。这将特别适合想做开放源代码软件的程序开发人员,又或如果你只是自己写些小的Toy程序,那么这
个文章对你也会有很大的帮助。
一、Makefile介绍
Makefile是用于自动编译和链接的,一个工程有很多文件组成,每一个文件的改变都会导致工程的重新链接,但是不是所有的文件都需要重新编译,Makefile中纪录有文件的信息,在make时会决定在链接的时候需要重新编译哪些文件。
Makefile的宗旨就是:让编译器知道要编译一个文件需要依赖其他的哪些文件。当那些依赖文件有了改变,编译器会自动的发现最终的生成文件已经过时,而重新编译相应的模块。
Makefile的基本结构不是很复杂,但当一个程序开发人员开始写Makefile时,经常会怀疑自己写的是否符合惯例,而且自己写的
Makefile经常和自己的开发环境相关联,当系统环境变量或路径发生了变化后,Makefile可能还要跟着修改。这样就造成了手工书写
Makefile的诸多问题,automake恰好能很好地帮助我们解决这些问题。
使用automake,程序开发人员只需要写一些
简单的含有预定义宏的文件,由autoconf根据一个宏文件生成configure,由automake根据另一个宏文件生成Makefile.in,
再使用configure依据Makefile.in来生成一个符合惯例的Makefile。下面我们将详细介绍Makefile的automake生成
方法。
二、使用的环境
本文所提到的程序是基于Linux发行版本:Fedora Core release 1,它包含了我们要用到的autoconf,automake。
三、从helloworld入手
我们从大家最常使用的例子程序helloworld开始。
下面的过程如果简单地说来就是:
新建三个文件:
helloworld.c
configure.in
Makefile.am
然后执行:
aclocal; autoconf; automake --add-missing; ./configure; make; ./helloworld |
就可以看到Makefile被产生出来,而且可以将helloworld.c编译通过。
很简单吧,几条命令就可以做出一个符合惯例的Makefile,感觉如何呀。
现在开始介绍详细的过程:
1、建目录
在你的工作目录下建一个helloworld目录,我们用它来存放helloworld程序及相关文件,如在/home/my/build下:
$ mkdir helloword $ cd helloworld |
2、 helloworld.c
然后用你自己最喜欢的编辑器写一个hellowrold.c文件,如命令:vi helloworld.c。使用下面的代码作为helloworld.c的内容。
int main(int argc, char** argv) { printf("Hello, Linux World!\n"); return 0; } |
完成后保存退出。
现在在helloworld目录下就应该有一个你自己写的helloworld.c了。
3、生成configure
我们使用autoscan命令来帮助我们根据目录下的源代码生成一个configure.in的模板文件。
命令:
$ autoscan $ ls configure.scan helloworld.c |
执行后在hellowrold目录下会生成一个文件:configure.scan,我们可以拿它作为configure.in的蓝本。
现在将configure.scan改名为configure.in,并且编辑它,按下面的内容修改,去掉无关的语句:
============================configure.in内容开始========================================= # -*- 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) ============================configure.in内容结束========================================= |
然后执行命令aclocal和autoconf,分别会产生aclocal.m4及configure两个文件:
$ aclocal $ls aclocal.m4 configure.in helloworld.c $ autoconf $ ls aclocal.m4 autom4te.cache configure configure.in helloworld.c |
大家可以看到configure.in内容是一些宏定义,这些宏经autoconf处理后会变成检查系统特性、环境变量、软件必须的参数的shell脚本。
autoconf 是用来生成自动配置软件源代码脚本(configure)的工具。configure脚本能独立于autoconf运行,且在运行的过程中,不需要用户的干预。
要生成configure文件,你必须告诉autoconf如何找到你所用的宏。方式是使用aclocal程序来生成你的aclocal.m4。
aclocal根据configure.in文件的内容,自动生成aclocal.m4文件。aclocal是一个perl 脚本程序,它的定义是:“aclocal - create aclocal.m4 by scanning configure.ac”。
autoconf从configure.in这个列举编译软件时所需要各种参数的模板文件中创建configure。
autoconf需要GNU m4宏处理器来处理aclocal.m4,生成configure脚本。
m4是一个宏处理器。将输入拷贝到输出,同时将宏展开。宏可以是内嵌的,也可以是用户定义的。除了可以展开宏,m4还有一些内建的函数,用来引用文件,执行命令,整数运算,文本操作,循环等。m4既可以作为编译器的前端,也可以单独作为一个宏处理器。
4、新建Makefile.am
新建Makefile.am文件,命令:
$ vi Makefile.am |
内容如下:
AUTOMAKE_OPTIONS=foreign bin_PROGRAMS=helloworld helloworld_SOURCES=helloworld.c |
automake会根据你写的Makefile.am来自动生成Makefile.in。
Makefile.am中定义的宏和目标,会指导automake生成指定的代码。例如,宏bin_PROGRAMS将导致编译和连接的目标被生成。
5、运行automake
命令:
$ automake --add-missing configure.in: installing `./install-sh' configure.in: installing `./mkinstalldirs' configure.in: installing `./missing' Makefile.am: installing `./depcomp' |
automake会根据Makefile.am文件产生一些文件,包含最重要的Makefile.in。
6、执行configure生成Makefile
$ ./configure checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for gawk... gawk checking whether make sets $(MAKE)... yes checking for gcc... gcc checking for C compiler default output... 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 ANSI C... 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 $ ls -l Makefile -rw-rw-r-- 1 yutao yutao 15035 Oct 15 10:40 Makefile |
你可以看到,此时Makefile已经产生出来了。
7、使用Makefile编译代码
$ make if gcc -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" - DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"helloworld\" -DVERSION=\"1.0\" -I. -I. -g -O2 -MT helloworld.o -MD -MP -MF ".deps/helloworld.Tpo" \ -c -o helloworld.o `test -f 'helloworld.c' || echo './'`helloworld.c; \ then mv -f ".deps/helloworld.Tpo" ".deps/helloworld.Po"; \ else rm -f ".deps/helloworld.Tpo"; exit 1; \ fi gcc -g -O2 -o helloworld helloworld.o |
运行helloworld
$ ./helloworld Hello, Linux World! |
这样helloworld就编译出来了,你如果按上面的步骤来做的话,应该也会很容易地编译出正确的helloworld文件。你还可以试着使用一些其
他的make命令,如make clean,make install,make
dist,看看它们会给你什么样的效果。感觉如何?自己也能写出这么专业的Makefile,老板一定会对你刮目相看。
评论
这个ibm的例子也挺好的,但是工程下载不了
发表评论
-
linux生成指定大小的文件
2015-08-26 16:02 1654# dd if=/dev/zero of=50M.file ... -
恢复 git reset -hard 的误操作
2014-08-30 15:10 855有时候使用Git工作得小心翼翼,特别是涉及到一些高级操作,例如 ... -
安装下载jamesAMES邮件服务器
2014-08-22 14:24 774UBUNTU下的JAMES邮件服务器 下载文件james- ... -
ubuntu james 2.3 部署
2014-08-22 14:23 791参考文章 1、需要用到很多权限和1024下的端口,用roo ... -
E: Sub-process /usr/bin/dpkg returned an error code (1)
2014-08-21 10:40 827Sub-process /usr/bin/dpkg retu ... -
Ubuntu系统如何设置vpn
2014-06-20 09:28 221因为一些原因,我们可能需要使用vpn,很 ... -
部署Exchange2007
2014-06-06 10:02 522Exchange邮件服务器是一个不错的通信消息平台, ... -
ubuntu 挂载(感觉挂载iso相当舒服)
2014-06-03 17:20 916当然首先在/media目录下先新建个文件夹当挂载点先以挂载 ... -
InputStream、OutputStream、String的相互转换
2014-06-03 17:19 5461、字符串转inputStream Java ... -
Windows XP格式化U盘为NTFS的方法
2014-06-03 10:41 739大于4G的U盘,最好将其格式化为NTFS格式,因为FAT3 ... -
google hosts
2014-05-31 11:29 250127.0.0.1localhost 127.0.1.1h ... -
google hosts
2014-05-30 18:03 4#Google Services START203.208.4 ... -
terminator 的配置
2014-05-15 09:15 1372让termintor 文字背景和ubuntu 一样 ... -
Ubuntu下安装postfix+extmail邮件服务器
2014-05-10 12:19 1771系统:ubuntu-10.04.3-server-amd64 ... -
Discuz X2.5 取消登陆密码错误15分钟限制的方法
2014-05-09 15:15 1246个人不推荐取消登录密码错误15分钟后再试的限制,就现在有限制 ... -
Ubuntu下搭建FTP服务器
2014-05-08 19:24 783后台一直提示wordperss版本和插件有更新,看着碍眼,决 ... -
Ubuntu 使用 Discuz搭建论坛
2014-05-08 19:22 7541. 安装LAMP 具体参考http://www.linu ... -
ubuntu 12.04卸载 vmware
2014-05-06 14:27 652在ubuntu 安装玩VMware 9 后,始终卸载不掉,一 ... -
ssh打洞
2014-05-04 22:41 1023目的:要求能在家里或其他外网环境访问在公司内网中我的工作电脑网 ... -
ubuntu无root权限,如何将用户添加到sudo组中去?
2014-05-04 17:21 1861最近下载了debian6.0,在vmware中安装完成之后发 ...
相关推荐
Linux下Makefile的automake生成全攻略
将给大家介绍如何使用autoconf和automake两个工具来帮助我们自动地生成符合自由软件惯例的Makefile,这样就可以象常见的GNU程序一样,只要使用“./configure”,“make”,“make instal”就可以把程序安装到Linux...
用make命令来编译自己写的程序确实是很方便。一般情况下,大家都是手工写一个简单Makefile,如果要想写出一个符合自由软件惯例的Makefile就不那么容易了.
使用AutoMake轻松生成Makefile
网上给的例子,都是针对一个文件来生成makefile. 但在实际工程中不可能只有一个文件,所以我给出一个3个文件的例子.Myfirst.cpp 主程序,Hello.h,Hello.cpp类的实现. 功能:利用现有工具,生成makefile,生成执行...
例解 autoconf 和 automake 生成 Makefile 文件.
automake 是linux下的自动生成makefile的工具。需要配合auroconf使用 版本:automake-1.9.6.tar.tar
运用Autoconf和Automake生成Makefile.
使用AutoMake轻松生成Makefile.doc使用AutoMake轻松生成Makefile.doc使用AutoMake轻松生成Makefile.doc使用AutoMake轻松生成Makefile.doc使用AutoMake轻松生成Makefile.doc使用AutoMake轻松生成Makefile.doc使用...
linux下makefile文件编写 linux下makefile文件编写 linux下makefile文件编写
Linux下makefile教程,很详细,是不可多得的好教程。
[免费免费]Makefile的automake生成。以c代码为例,示范如何生成Makefile。
使用Automake,Autoconf生成Makefile,介绍软件打包发布
以一个CPP简单项目为线索,详细讲解Makefile生成的具体步骤与细节
Automake,Autoconf自动生成Makefile linux c程序自动生成Makefile的详细过程
Linux下Makefile文件的编写 Linux编程
linux内核Makefile详解 linux内核Makefile详解 linux内核Makefile详解 linux内核Makefile详解 linux内核Makefile详解