在Ubuntu下使用autotools生成Makefile

来源:互联网 发布:mysql主键自增长 重置 编辑:程序博客网 时间:2024/05/23 07:24

相信在Linux环境下做过项目的人,都会知道Makefile的重要性。它能够帮助我们完成很多的编译工作,节约我们自己去编译的时间。Makefile的重要性这里就不在强调了!Windows下的IDE都自动生成了Makefile,因此不需要自己再去写Makefile,所以习惯了在Windows下编程的人突然在Linux下编程是不适应的。

既然Windows下能够自动生成Makefile,那么Linux下有没有这样的工具呢?

答案是有的,应该在做一个大的项目的时候,不可能都靠人来写Makefile,这样太麻烦了!很多GNULinux的的软件都是用它生成Makefile的,包括我们非常熟悉的Linux内核源代码。Linux下自动生成Makefile的工具有autotools、qmake等。(玩过QT的人应该对qmake十分熟悉)


一、autotools的安装步骤

下面主要是针对autotools工具来讲,先从安装步骤开始吧。

我的环境是Ubuntu 14.04版本,Ubuntu安装工具十分方便,用apt命令即可。

sudo apt-get install autoconf
安装完成之后,使用which命令查看是否安装成功。因为autotools是个系列工具,安装包相互直接存在依赖。

zqj@ubuntu:~$ which aclocal/usr/bin/aclocalzqj@ubuntu:~$ which autoscan/usr/bin/autoscanzqj@ubuntu:~$ which autoconf/usr/bin/autoconfzqj@ubuntu:~$ which autoheader/usr/bin/autoheaderzqj@ubuntu:~$ which automake/usr/bin/automake
这样就安装完成了,接下来讲讲怎么使用autotools吧!


二、autotools的使用

接下来我们写个测试代码吧!

我写的测试代码结构如下:tree命令查看

.└── Test    ├── include    │   └── head.h    └── Main        └── main.cpp
head.h

/*****************************************************Copyright (C) 2017-2018 All rights reserved.File name    : head.hVersion      : v1.0       Author       : ZhengqijunDate         : 2017年04月10日 星期一 17时31分05秒Description  : 头文件Funcion List : *****************************************************/#ifndef __HEAD_H__#define __HEAD_H__#include <iostream>using namespace std;#endif //__HEAD_H__
main.cpp

/*****************************************************Copyright (C) 2017-2018 All rights reserved.File name    : main.cppVersion      : v1.0       Author       : ZhengqijunDate         : 2017年04月10日 星期一 17时32分20秒Description  : 主函数Funcion List : main()*****************************************************/#include "../include/head.h"int main(){    cout << "hello world!" << endl;    return 0;}


autotools的使用步骤如下:

1、使用autoscan命令生成autoscan.log  configure.scan两个文件。

zqj@ubuntu:~/2017/0410/Test$ autoscanzqj@ubuntu:~/2017/0410/Test$ lsautoscan.log  configure.scan  include  Main


2、使用cp命令生成configure.ac文件。

zqj@ubuntu:~/2017/0410/Test$ cp configure.scan configure.ac zqj@ubuntu:~/2017/0410/Test$ lsautoscan.log  configure.ac  configure.scan  include  Main


3、修改configure.ac文件。

找到这句 AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
其中,FULL-PACKAGE-NAME为程序名称,VERSION为当前版本,BUG-REPORT-ADDRESS为bug汇报地址。

这里我改为:(根据自己的实际情况)

AC_INIT(Main, 0.0.1, xxx@xx.com)
然后在AC_INIT()后面添加一句AM_INIT_AUTOMAKE,在AC_OUTPUT()前面添加一句AC_CONFIG_FILES([Makefile])。如下所示:



4、执行aclocal命令生成aclocal.m4和autom4te.cache文件。

zqj@ubuntu:~/2017/0410/Test$ aclocalzqj@ubuntu:~/2017/0410/Test$ lsaclocal.m4      autoscan.log  configure.scan  Mainautom4te.cache  configure.ac  include


5、制作Makefile.am文件,因为automake这个命令需要用到这个配置文件。

# Makefile.amAUTOMAKE_OPTIONS = foreignbin_PROGRAMS     = mainmain_SOURCES     = include/head.h Main/main.cppmain_CPPFLAGS    = -I include/


6、执行autoheader命令生成config.h.in文件。


7、使用touch命令生成automake必须文件。

touch NEWS README AUTHORS ChangeLog

automake必须文件:
install-sh、missing、INSTALL、NEWS、README、AUTHORS、ChangeLog、COPYING、depcomp 

执行automake -a的时候会自动生成
install-sh、missing、INSTALL、COPYING、depcomp

其它文件就需要自己手动添加了。可以使用touch命令


8、执行automake -a命令生成其它文件。

zqj@ubuntu:~/2017/0410/Test$ automake -azqj@ubuntu:~/2017/0410/Test$ lsaclocal.m4      ChangeLog     configure.scan  INSTALL      Makefile.inAUTHORS         compile       COPYING         install-sh   missingautom4te.cache  config.h.in   depcomp         Main         NEWSautoscan.log    configure.ac  include         Makefile.am  README


9、执行autoconf命令生成configure文件。

zqj@ubuntu:~/2017/0410/Test$ autoconfzqj@ubuntu:~/2017/0410/Test$ lsaclocal.m4      compile         COPYING     Main         READMEAUTHORS         config.h.in     depcomp     Makefile.amautom4te.cache  configure       include     Makefile.inautoscan.log    configure.ac    INSTALL     missingChangeLog       configure.scan  install-sh  NEWS


10、执行./configure命令进行测试。

zqj@ubuntu:~/2017/0410/Test$ ./configure checking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for a thread-safe mkdir -p... /bin/mkdir -pchecking for gawk... nochecking for mawk... mawkchecking whether make sets $(MAKE)... yeschecking whether make supports nested variables... yeschecking for g++... g++checking whether the C++ compiler works... yeschecking for C++ compiler default output file name... a.outchecking for suffix of executables... checking whether we are cross compiling... nochecking for suffix of object files... ochecking whether we are using the GNU C++ compiler... yeschecking whether g++ accepts -g... yeschecking for style of include used by make... GNUchecking dependency style of g++... gcc3checking for gcc... gccchecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ISO C89... none neededchecking whether gcc understands -c and -o together... yeschecking dependency style of gcc... gcc3checking that generated files are newer than configure... doneconfigure: creating ./config.statusconfig.status: creating Makefileconfig.status: creating config.hconfig.status: executing depfiles commands


11、执行make命令生成可执行文件。

zqj@Zqj-ubuntu:~/2017/0410/Test$ makemake  all-ammake[1]: Entering directory `/home/zqj/2017/0410/Test'g++ -DHAVE_CONFIG_H -I.  -I include/   -g -O2 -MT main-main.o -MD -MP -MF .deps/main-main.Tpo -c -o main-main.o `test -f 'Main/main.cpp' || echo './'`Main/main.cppmv -f .deps/main-main.Tpo .deps/main-main.Pog++  -g -O2   -o main main-main.o  make[1]: Leaving directory `/home/zqj/2017/0410/Test'
注意:如果make出错,没有生成可执行文件。那么下次make编译前,先使用make clean清除中间文件,再进行重新编译!(make出错可能是配置文件写的有问题Makefile.am)


虽然第一次使用可能感觉autotools还没有自己写Makefile好用,但是当你熟练掌握它的使用方法的时候,再去做大项目的时候,你会发现autotools的好用之处!


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 蝴蝶的翅膀断了怎么办 培乐多彩泥干了怎么办 ps4被ban机了怎么办 塔吊离建筑物8米怎么办 手表带掉边缘皮怎么办 脸皮肤过敏痒肿怎么办 脸过敏发红发肿怎么办 皮肤过敏又红又肿怎么办 春天脸过敏发红痒怎么办 皮卡车后斗生锈怎么办 没带卸妆的东西怎么办 审车尾气过不了怎么办 违章停车条丢了怎么办 停车被城管贴条怎么办 违停告知单丢了怎么办 交通事故责任认定书不服怎么办 对交通事故认定书有异议怎么办 老婆不让我碰她怎么办 车被城管拖走了怎么办 共享汽车没油了怎么办 车被蹭了人跑了怎么办 遇见碰瓷的人怎么办 遇到老人碰瓷的怎么办 打了碰瓷的怎么办 倒车入库打晚了怎么办 上班停车费太贵怎么办 黄疸回家小孩不爱吃母乳怎么办 黄金棒开关坏了怎么办 在一家莆田系医院上班怎么办 被莆田医院骗了怎么办 痘痘红肿有脓包怎么办 换届选举候选人自动退出竞选怎么办 城管暴力执法导致老百姓受伤怎么办 领导制定方案与政策不一致怎么办 第三方支付存在的金融风险怎么办 貔貅鼻子摔坏了怎么办 貔貅鼻子磕破了怎么办 红警2游戏出错怎么办 猛犸牙上油花了怎么办 吃了细菌的食物怎么办 易拉罐罐头拉环断了怎么办