在Ubuntu下使用autotools生成Makefile

来源:互联网 发布:python 安装numpy 编辑:程序博客网 时间:2024/05/18 15:25

原文地址:http://blog.csdn.net/zhengqijun_/article/details/69952410

相信在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命令即可。

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. sudo apt-get install autoconf  
sudo apt-get install autoconf
安装完成之后,使用which命令查看是否安装成功。因为autotools是个系列工具,安装包相互直接存在依赖。

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. zqj@ubuntu:~&nbsp;which&nbsp;aclocal&nbsp;&nbsp;</span></span></li><li class=""><span>/usr/bin/aclocal&nbsp;&nbsp;</span></li><li class="alt"><span>zqj@ubuntu:~ which autoscan  
  2. /usr/bin/autoscan  
  3. zqj@ubuntu:~&nbsp;which&nbsp;autoconf&nbsp;&nbsp;</span></li><li class=""><span>/usr/bin/autoconf&nbsp;&nbsp;</span></li><li class="alt"><span>zqj@ubuntu:~ which autoheader  
  4. /usr/bin/autoheader  
  5. zqj@ubuntu:~&nbsp;which&nbsp;automake&nbsp;&nbsp;</span></li><li class=""><span>/usr/bin/automake&nbsp;&nbsp;</span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="2324182" snippet_file_name="blog_20170410_2_3605719" name="code" class="cpp" style="display: none;">zqj@ubuntu:~ which aclocal/usr/bin/aclocalzqj@ubuntu:~whichautoscan/usr/bin/autoscanzqj@ubuntu:  which autoconf/usr/bin/autoconfzqj@ubuntu:~whichautoheader/usr/bin/autoheaderzqj@ubuntu:  which automake/usr/bin/automake这样就安装完成了,接下来讲讲怎么使用autotools吧!


    二、autotools的使用

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

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

    [cpp] view plain copy
    print?在CODE上查看代码片派生到我的代码片
    1. .  
    2. └── Test  
    3.     ├── include  
    4.     │   └── head.h  
    5.     └── Main  
    6.         └── main.cpp  
    . 
    └── Test
    ├── include
    │   └── head.h
    └── Main
    └── main.cpp
    head.h

    [cpp] view plain copy
    print?在CODE上查看代码片派生到我的代码片
    1. /******************************************* 
    2. Copyright (C) 2017-2018 All rights reserved. 
    3. File name    : head.h 
    4. Version      : v1.0        
    5. Author       : Zhengqijun 
    6. Date         : 2017年04月10日 星期一 17时31分05秒 
    7. Description  : 头文件 
    8. Funcion List :  
    9. *******************************************/  
    10.   
    11. #ifndef HEAD_H  
    12. #define HEAD_H  
    13.   
    14. #include <iostream>  
    15.   
    16. using namespace std;  
    17.   
    18. #endif //HEAD_H  
    /******************************************* 
    Copyright (C) 2017-2018 All rights reserved.
    File name : head.h
    Version : v1.0
    Author : Zhengqijun
    Date : 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

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. /***************************************************** 
  2. Copyright (C) 2017-2018 All rights reserved. 
  3. File name    : main.cpp 
  4. Version      : v1.0        
  5. Author       : Zhengqijun 
  6. Date         : 2017年04月10日 星期一 17时32分20秒 
  7. Description  : 主函数 
  8. Funcion List : main() 
  9. *****************************************************/  
  10.   
  11. #include ”../include/head.h”  
  12.   
  13. int main()  
  14. {  
  15.     cout << ”hello world!” << endl;  
  16.   
  17.     return 0;  
  18. }  
/*****************************************************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两个文件。

[plain] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. zqj@ubuntu:~/2017/0410/Test&nbsp;autoscan&nbsp;&nbsp;</span></span></li><li class=""><span>zqj@ubuntu:~/2017/0410/Test ls  
  2. autoscan.log  configure.scan  include  Main  
zqj@ubuntu:~/2017/0410/Test$ autoscanzqj@ubuntu:~/2017/0410/Test$ lsautoscan.log  configure.scan  include  Main


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

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. zqj@ubuntu:~/2017/0410/Test&nbsp;cp&nbsp;configure.scan&nbsp;configure.ac&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>zqj@ubuntu:~/2017/0410/Test ls  
  2. autoscan.log  configure.ac  configure.scan  include  Main  
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汇报地址。

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

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. AC_INIT(Main, 0.0.1, xxx@xx.com)  
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文件。

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. zqj@ubuntu:~/2017/0410/Test&nbsp;aclocal&nbsp;&nbsp;</span></span></li><li class=""><span>zqj@ubuntu:~/2017/0410/Test ls  
  2. aclocal.m4      autoscan.log  configure.scan  Main  
  3. autom4te.cache  configure.ac  include  
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这个命令需要用到这个配置文件。

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. # Makefile.am  
  2. AUTOMAKE_OPTIONS = foreign  
  3. bin_PROGRAMS     = main  
  4. main_SOURCES     = include/head.h Main/main.cpp  
  5. main_CPPFLAGS    = -I include/  
# Makefile.amAUTOMAKE_OPTIONS = foreignbin_PROGRAMS     = mainmain_SOURCES     = include/head.h Main/main.cppmain_CPPFLAGS    = -I include/


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


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

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. touch NEWS README AUTHORS ChangeLog  
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命令生成其它文件。

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. zqj@ubuntu:~/2017/0410/Test&nbsp;automake&nbsp;-a&nbsp;&nbsp;</span></span></li><li class=""><span>zqj@ubuntu:~/2017/0410/Test ls  
  2. aclocal.m4      ChangeLog     configure.scan  INSTALL      Makefile.in  
  3. AUTHORS         compile       COPYING         install-sh   missing  
  4. autom4te.cache  config.h.in   depcomp         Main         NEWS  
  5. autoscan.log    configure.ac  include         Makefile.am  README  
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文件。

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. zqj@ubuntu:~/2017/0410/Test&nbsp;autoconf&nbsp;&nbsp;</span></span></li><li class=""><span>zqj@ubuntu:~/2017/0410/Test ls  
  2. aclocal.m4      compile         COPYING     Main         README  
  3. AUTHORS         config.h.in     depcomp     Makefile.am  
  4. autom4te.cache  configure       include     Makefile.in  
  5. autoscan.log    configure.ac    INSTALL     missing  
  6. ChangeLog       configure.scan  install-sh  NEWS  
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命令进行测试。

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. zqj@ubuntu:~/2017/0410/Test&nbsp;./configure&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>checking&nbsp;<span class="keyword">for</span><span>&nbsp;a&nbsp;BSD-compatible&nbsp;install...&nbsp;/usr/bin/install&nbsp;-c&nbsp;&nbsp;</span></span></li><li class="alt"><span>checking&nbsp;whether&nbsp;build&nbsp;environment&nbsp;is&nbsp;sane...&nbsp;yes&nbsp;&nbsp;</span></li><li class=""><span>checking&nbsp;<span class="keyword">for</span><span>&nbsp;a&nbsp;</span><span class="keyword">thread</span><span>-safe&nbsp;mkdir&nbsp;-p...&nbsp;/bin/mkdir&nbsp;-p&nbsp;&nbsp;</span></span></li><li class="alt"><span>checking&nbsp;<span class="keyword">for</span><span>&nbsp;gawk...&nbsp;no&nbsp;&nbsp;</span></span></li><li class=""><span>checking&nbsp;<span class="keyword">for</span><span>&nbsp;mawk...&nbsp;mawk&nbsp;&nbsp;</span></span></li><li class="alt"><span>checking&nbsp;whether&nbsp;make&nbsp;sets&nbsp;(MAKE)… yes  
  2. checking whether make supports nested variables… yes  
  3. checking for g++… g++  
  4. checking whether the C++ compiler works… yes  
  5. checking for C++ compiler default output file name… a.out  
  6. checking for suffix of executables…   
  7. checking whether we are cross compiling… no  
  8. checking for suffix of object files… o  
  9. checking whether we are using the GNU C++ compiler… yes  
  10. checking whether g++ accepts -g… yes  
  11. checking for style of include used by make… GNU  
  12. checking dependency style of g++… gcc3  
  13. checking for gcc… gcc  
  14. checking whether we are using the GNU C compiler… yes  
  15. checking whether gcc accepts -g… yes  
  16. checking for gcc option to accept ISO C89… none needed  
  17. checking whether gcc understands -c and -o together… yes  
  18. checking dependency style of gcc… gcc3  
  19. checking that generated files are newer than configure… done  
  20. configure: creating ./config.status  
  21. config.status: creating Makefile  
  22. config.status: creating config.h  
  23. config.status: executing depfiles commands  
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命令生成可执行文件。

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. zqj@Zqj-ubuntu:~/2017/0410/Test&nbsp;make&nbsp;&nbsp;</span></span></li><li class=""><span>make&nbsp;&nbsp;all-am&nbsp;&nbsp;</span></li><li class="alt"><span>make[1]:&nbsp;Entering&nbsp;directory&nbsp;`/home/zqj/2017/0410/Test'&nbsp;&nbsp;</span></li><li class=""><span>g++&nbsp;-DHAVE_CONFIG_H&nbsp;-I.&nbsp;&nbsp;-I&nbsp;include/&nbsp;&nbsp;&nbsp;-g&nbsp;-O2&nbsp;-MT&nbsp;main-main.o&nbsp;-MD&nbsp;-MP&nbsp;-MF&nbsp;.deps/main-main.Tpo&nbsp;-c&nbsp;-o&nbsp;main-main.o&nbsp;`test&nbsp;-f&nbsp;<span class="string">'Main/main.cpp'</span><span>&nbsp;||&nbsp;echo&nbsp;</span><span class="string">'./'</span><span>`Main/main.cpp&nbsp;&nbsp;</span></span></li><li class="alt"><span>mv&nbsp;-f&nbsp;.deps/main-main.Tpo&nbsp;.deps/main-main.Po&nbsp;&nbsp;</span></li><li class=""><span>g++&nbsp;&nbsp;-g&nbsp;-O2&nbsp;&nbsp;&nbsp;-o&nbsp;main&nbsp;main-main.o&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>make[1]:&nbsp;Leaving&nbsp;directory&nbsp;`/home/zqj/2017/0410/Test'&nbsp;&nbsp;</span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre code_snippet_id="2324182" snippet_file_name="blog_20170410_15_2549088" name="code" class="cpp" style="display: none;">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
原创粉丝点击