在Ubuntu下使用autotools生成Makefile

原文地址: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命令即可。

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

  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/aclocal zqj@ubuntu:~ whichautoscan/usr/bin/autoscanzqj@ubuntu:  which autoconf /usr/bin/autoconf zqj@ubuntu:~ whichautoheader/usr/bin/autoheaderzqj@ubuntu:  which automake /usr/bin/automake这样就安装完成了,接下来讲讲怎么使用autotools吧!


    二、autotools的使用

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

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

    1. .  
    2. └── Test  
    3.     ├── include  
    4.     │   └── head.h  
    5.     └── Main  
    6.         └── main.cpp  
    . 
    └── Test
    ├── include
    │   └── head.h
    └── Main
    └── main.cpp
    head.h

    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

  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.cpp
Version      : v1.0       
Author       : Zhengqijun
Date         : 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两个文件。

  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$ autoscan
zqj@ubuntu:~/2017/0410/Test$ ls
autoscan.log  configure.scan  include  Main


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

  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$ ls
autoscan.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汇报地址。

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

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



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

  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$ aclocal
zqj@ubuntu:~/2017/0410/Test$ ls
aclocal.m4      autoscan.log  configure.scan  Main
autom4te.cache  configure.ac  include


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

  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.am
AUTOMAKE_OPTIONS = foreign
bin_PROGRAMS     = main
main_SOURCES     = include/head.h Main/main.cpp
main_CPPFLAGS    = -I include/


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


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

  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命令生成其它文件。

  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 -a
zqj@ubuntu:~/2017/0410/Test$ ls
aclocal.m4      ChangeLog     configure.scan  INSTALL      Makefile.in
AUTHORS         compile       COPYING         install-sh   missing
autom4te.cache  config.h.in   depcomp         Main         NEWS
autoscan.log    configure.ac  include         Makefile.am  README


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

  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$ autoconf
zqj@ubuntu:~/2017/0410/Test$ ls
aclocal.m4      compile         COPYING     Main         README
AUTHORS         config.h.in     depcomp     Makefile.am
autom4te.cache  configure       include     Makefile.in
autoscan.log    configure.ac    INSTALL     missing
ChangeLog       configure.scan  install-sh  NEWS


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

  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 -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for g++... g++
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 g++ accepts -g... yes
checking for style of include used by make... GNU
checking dependency style of g++... gcc3
checking for gcc... gcc
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 whether gcc understands -c and -o together... yes
checking dependency style of gcc... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands


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

  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 make make all-am make[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.cpp mv -f .deps/main-main.Tpo .deps/main-main.Po g++ -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的好用之处!


猜你喜欢

转载自blog.csdn.net/gzj2013/article/details/71603453