现在的位置: 首页 > 综合 > 正文

使用autoconf、automake进行外部编译

2017年09月09日 ⁄ 综合 ⁄ 共 4960字 ⁄ 字号 评论关闭

通常linux下编译文件默认会使用内部编译方式,即编译的中间文件与源码放在一起,这样会使代码阅读带来很大的不便。如果使用外部编译将会很好的解决这个问题,下面我们以一个实例来讲解外部编译:

[plain] view
plain
copy

  1. gqw@debian:~/workspace$ tree ./test_hello/  
  2. ./test_hello/  
  3. |-- build  
  4. |   |-- configure.ac  
  5. |   `-- Makefile.am  
  6. |-- include  
  7. |-- lib  
  8. `-- src  
  9.     |-- hello  
  10.     |   |-- hello.cpp  
  11.     |   `-- hello.h  
  12.     |-- main.cpp  
  13.     `-- test  
  14.         |-- hello.cpp  
  15.         `-- hello.h  
  16.   
  17. 6 directories, 7 files  

通过上面的结构,可以看到src目录是源代码存放目录,我们希望在编译过程中保持此目录的干净,即不要将编译生成的中间文件放入此目录下。

build目录是我们手工添加的,其中的configure.ac可通过autoscan得到configure.scan文件修改而得。下面我们看下里面的内容:

[plain] view
plain
copy

  1. gqw@debian:~/workspace$ cat ./test_hello/build/configure.ac   
  2. #                                               -*- Autoconf -*-  
  3. # Process this file with autoconf to produce a configure script.  
  4.   
  5. AC_PREREQ([2.67])  
  6. AC_INIT([test_hello], [0.0.1], [gqwmail@gmail.com])  
  7. AM_INIT_AUTOMAKE  
  8. LT_INIT([disable-static])  
  9. AC_CONFIG_SRCDIR([../src/main.cpp])  
  10. AC_CONFIG_HEADERS([config.h])  
  11. AC_CONFIG_MACRO_DIR([m4])  
  12.   
  13. # Checks for programs.  
  14. #AC_PROG_LIBTOOL  
  15. AC_PROG_CXX  
  16. AC_PROG_CC  
  17.   
  18. # Checks for libr  aries.  
  19.   
  20. # Checks for header files.  
  21.   
  22. # Checks for typedefs, structures, and compiler characteristics.  
  23.   
  24. # Checks for library functions.  
  25. AC_CONFIG_FILES([Makefile])  
  26. AC_OUTPUT  

再看一下Makefile.am内容

[plain] view
plain
copy

  1. gqw@debian:~/workspace$ cat ./test_hello/build/Makefile.am   
  2. ACLOCAL_AMFLAGS=-I m4  
  3. AUTOMAKE_OPTIONS = foreign subdir-objects  
  4.   
  5.   
  6. nobase_include_HEADERS=             \  
  7.     ../src/hello/hello.h            \  
  8.     ../src/test/hello.h       
  9.   
  10. lib_LTLIBRARIES=hello_test/libhello_test.la  
  11. hello_test_libhello_test_la_SOURCES=        \  
  12.     ../src/main.cpp             \  
  13.     ../src/hello/hello.cpp          \  
  14.     ../src/test/hello.cpp         

值得注意的是选项

[plain] view
plain
copy

  1. AUTOMAKE_OPTIONS = foreign subdir-objects  

这个选项中的subdir-objects会为生成的中间文件自动创建与源码一致的目录结构,而不是统一放在同一目录,从而可以解决中间文件名冲突问题,例如上面的hello/hello.cpp与test/hello.cpp。

下面开始我们的工作,先生成编译文件:

[plain] view
plain
copy

  1. gqw@debian:~/workspace$ cd test_hello/build/  
  2. gqw@debian:~/workspace/test_hello/build$ libtoolize --automake  
  3. gqw@debian:~/workspace/test_hello/build$ aclocal  
  4. gqw@debian:~/workspace/test_hello/build$ autoheader  
  5. gqw@debian:~/workspace/test_hello/build$ autoconf  
  6. gqw@debian:~/workspace/test_hello/build$ automake -a  
  7. configure.ac:7: installing `./config.guess'  
  8. configure.ac:7: installing `./config.sub'  
  9. configure.ac:6: installing `./install-sh'  
  10. configure.ac:6: installing `./missing'  
  11. Makefile.am: installing `./depcomp'  
  12. gqw@debian:~/workspace/test_hello/build$ ls  
  13. aclocal.m4      config.h.in  configure.ac  ltmain.sh    Makefile.in  
  14. autom4te.cache  config.sub   depcomp       m4           missing  
  15. config.guess    configure    install-sh    Makefile.am  

好了,到现在为此我们已经得到了configure文件了,下面让我们使用./configure、make、make install来编译与安装我们的文件吧:

[plain] view
plain
copy

  1. gqw@debian:~/workspace/test_hello/build$ mkdir install  
  2. gqw@debian:~/workspace/test_hello/build$ cd install  
  3. gqw@debian:~/workspace/test_hello/build/install$ ../configure --prefix=$(pwd)/../../; make;make install  

这里创建目录install是必须的,但目录名随意,否则中间文件还是会生成到src下去。configure加上--prefix选项是为了安装时能够安装到工程目录下,下面让我们看下最终和文件结构:

[plain] view
plain
copy

  1. gqw@debian:~/workspace$ tree test_hello/  
  2. test_hello/  
  3. |-- build  
  4. |   |-- aclocal.m4  
  5. |   |-- autom4te.cache  
  6. |   |   |-- output.0  
  7. |   |   |-- output.1  
  8. |   |   |-- requests  
  9. |   |   |-- traces.0  
  10. |   |   `-- traces.1  
  11. |   |-- config.guess -> /usr/share/automake-1.11/config.guess  
  12. |   |-- config.h.in  
  13. |   |-- config.sub -> /usr/share/automake-1.11/config.sub  
  14. |   |-- configure  
  15. |   |-- configure.ac  
  16. |   |-- depcomp -> /usr/share/automake-1.11/depcomp  
  17. |   |-- install  
  18. |   |   |-- config.h  
  19. |   |   |-- config.log  
  20. |   |   |-- config.status  
  21. |   |   |-- hello_test  
  22. |   |   |   `-- libhello_test.la  
  23. |   |   |-- libtool  
  24. |   |   |-- Makefile  
  25. |   |   `-- stamp-h1  
  26. |   |-- install-sh -> /usr/share/automake-1.11/install-sh  
  27. |   |-- ltmain.sh -> /usr/share/libtool/config/ltmain.sh  
  28. |   |-- m4  
  29. |   |   |-- libtool.m4 -> /usr/share/aclocal/libtool.m4  
  30. |   |   |-- lt~obsolete.m4 -> /usr/share/aclocal/lt~obsolete.m4  
  31. |   |   |-- ltoptions.m4 -> /usr/share/aclocal/ltoptions.m4  
  32. |   |   |-- ltsugar.m4 -> /usr/share/aclocal/ltsugar.m4  
  33. |   |   `-- ltversion.m4 -> /usr/share/aclocal/ltversion.m4  
  34. |   |-- Makefile.am  
  35. |   |-- Makefile.in  
  36. |   |-- missing -> /usr/share/automake-1.11/missing  
  37. |   `-- src  
  38. |       |-- hello  
  39. |       |   `-- hello.lo  
  40. |       |-- main.lo  
  41. |       `-- test  
  42. |           `-- hello.lo  
  43. |-- include  
  44. |   `-- src  
  45. |       |-- hello  
  46. |       |   `-- hello.h  
  47. |       `-- test  
  48. |           `-- hello.h  
  49. |-- lib  
  50. |   |-- libhello_test.la  
  51. |   |-- libhello_test.so -> libhello_test.so.0.0.0  
  52. |   |-- libhello_test.so.0 -> libhello_test.so.0.0.0  
  53. |   `-- libhello_test.so.0.0.0  
  54. `-- src  
  55.     |-- hello  
  56.     |   |-- hello.cpp  
  57.     |   `-- hello.h  
  58.     |-- main.cpp  
  59.     `-- test  
  60.         |-- hello.cpp  
  61.         `-- hello.h  
  62.   
  63. 16 directories, 43 files  

看到了吗,src目录下是不是很干净?所有的中间文件都在build目录下。

抱歉!评论已关闭.