找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3738|回复: 0

GNU Autotools (autoconf, automake, libtool, etc.) 自动化编译 (2)

[复制链接]
发表于 2012-2-16 10:38:42 | 显示全部楼层 |阅读模式
0. 基础
    先阅读GNU Autotools (autoconf, automake, libtool, etc.) 自动化编译 (1) 简单示例并成功执行、理解所说内容。
1. 先提供一个脚本。拷贝所有内容保存为文件atdir.sh并添加可执行权限,然后执行。注意,最好不要以根用户身份执行它。
  1. #!/bin/sh
  2. echo "Autotools for autotools multi directories (stands for atdirs)"
  3. echo_exit()
  4. {
  5.         echo "ERROR: $@"
  6.         exit 1
  7. }
  8. # 1. prepare directories and dummy files
  9. [ ! -e "atdirs" ] || echo_exit "atdirs exists, please remove or rename it"
  10. mkdir atdirs && cd atdirs || echo_exit "mkdir -p atdirs or cd atdirs failed"
  11. mkdir -p doc build-aux m4 man src/{include/atdirs,lib{1,2},tests} || \
  12.         echo_exit "create subdirectory failed"
  13. touch AUTHORS BUGS ChangeLog COPYING NEWS README || \
  14.         echo_exit "touch dummy files failed"
  15. # 2. generates source, header files
  16. cat > src/include/atdirs/lib1.h <<EOF
  17. #ifndef __ATDIRS_LIB1_H_
  18. #define __ATDIRS_LIB1_H_
  19. void lib1_print();
  20. #endif
  21. EOF
  22. cat > src/include/atdirs/lib2.h <<EOF
  23. #ifndef __ATDIRS_LIB2_H_
  24. #define __ATDIRS_LIB2_H_
  25. void lib2_print();
  26. #endif
  27. EOF
  28. cat > src/lib1/print.c <<EOF
  29. #ifdef HAVE_CONFIG_H
  30. #include <config.h>
  31. #endif
  32. #include <stdio.h>
  33. #include <atdirs/lib1.h>
  34. void lib1_print()
  35. {
  36.         printf("lib1-print: " PACKAGE_STRING " <lib-1>\n");
  37. }
  38. EOF
  39. cat > src/lib2/print.c <<EOF
  40. #ifdef HAVE_CONFIG_H
  41. #include <config.h>
  42. #endif
  43. #include <stdio.h>
  44. #include <atdirs/lib2.h>
  45. void lib2_print()
  46. {
  47.         printf("lib2-print: " PACKAGE_STRING " <lib-2>\n");
  48. }
  49. EOF
  50. cat > src/tests/test.c <<EOF
  51. #include <atdirs/lib1.h>
  52. #include <atdirs/lib2.h>
  53. int main(void)
  54. {
  55.         lib1_print();
  56.         lib2_print();
  57.         return 0;
  58. }
  59. EOF
复制代码
# 2. generates Makefil.am files
  1. cat > Makefile.am << EOF
  2. SUBDIRS = doc man src
  3. ACLOCAL_AMFLAGS = -I m4
  4. EXTRA_DIST = ChangeLog BUGS COPYING
  5. EOF
  6. # doc, man needs an empty Makefile.am
  7. touch {doc,man}/Makefile.am
  8. # src needs a valid Makefile.am
  9. cat > src/Makefile.am << EOF
  10. SUBDIRS = lib1 lib2 . tests
  11. lib_LTLIBRARIES = libatdirs.la
  12. libatdirs_la_SOURCES =
  13. libatdirs_la_LIBADD = lib1/libatdirs_lib1.la lib2/libatdirs_lib2.la
  14. libatdirs_la_LDFLAGS = -release \$(PACKAGE_VERSION) -version-info 1
  15. include_HEADERS = \$(top_srcdir)/\$(atdirs_includedir)/atdirs/lib1.h \
  16.                   \$(top_srcdir)/\$(atdirs_includedir)/atdirs/lib2.h
  17. EOF
  18. cat > src/lib1/Makefile.am << EOF
  19. include \$(top_srcdir)/Makefile.rules
  20. noinst_LTLIBRARIES = libatdirs_lib1.la
  21. libatdirs_lib1_la_SOURCES = print.c
  22. EOF
  23. cat > src/lib2/Makefile.am << EOF
  24. include \$(top_srcdir)/Makefile.rules
  25. noinst_LTLIBRARIES = libatdirs_lib2.la
  26. libatdirs_lib2_la_SOURCES = print.c
  27. EOF
  28. cat > src/tests/Makefile.am << EOF
  29. include \$(top_srcdir)/Makefile.rules
  30. bin_PROGRAMS = test
  31. test_SOURCES = test.c
  32. test_LDADD = ../libatdirs.la
  33. EOF
  34. # Makefile.rules:
  35. cat > Makefile.rules << EOF || echo_exit "generates Makefile.rules failed"
  36. AM_CPPFLAGS = -I\$(top_srcdir)/\$(atdirs_includedir)
  37. install-header:
  38.         \$(MKDIR_P) "\$(DESTDIR)\$(includedir)" || exit $?
  39.         cp -av \$(top_srcdir)/\$(atdirs_includedir) "\$(DESTDIR)\$(includedir)" \
  40.                 || exit $?
  41. EOF
复制代码
# 3. generates configure.ac
  1. cat > configure.ac << EOF
  2. #                                               -*- Autoconf -*-
  3. # Process this file with autoconf to produce a configure script.
  4. AC_PREREQ([2.67])
  5. AC_INIT([atdirs], [1.0.0], [cppgp@qq.com], [ycc], [http://blog.csdn.net/cppgp])
  6. AC_CONFIG_AUX_DIR([build-aux])
  7. AC_CANONICAL_BUILD
  8. AC_CANONICAL_HOST
  9. AM_INIT_AUTOMAKE([-Wall -Werror gnu])
  10. LT_INIT
  11. AC_COPYRIGHT([GPLv2])
  12. AC_REVISION([\$Revision: 1.0 \$])
  13. AC_CONFIG_SRCDIR([ChangeLog])
  14. AC_CONFIG_HEADERS([config.h])
  15. AC_CONFIG_MACRO_DIR([m4])
  16. # Checks for programs.
  17. AC_PROG_CC
  18. # Checks for libraries.
  19. # Checks for header files.
  20.              
  21. # Checks for library functions.
  22. AC_SUBST([atdirs_includedir], [src/include])
  23. AC_CONFIG_FILES([Makefile
  24.                  doc/Makefile
  25.                  man/Makefile
  26.                  src/Makefile
  27.                  src/lib1/Makefile
  28.                  src/lib2/Makefile
  29.                  src/tests/Makefile
  30.                  ])
  31. AC_OUTPUT
  32. EOF
复制代码
# 4. generates configure
  1. echo "now try to generates configure, it may take some seconds, please wait ..."
  2. autoreconf --install -Wall || echo_exit "autoreconf failed"
复制代码
# 5. builds it
  1. mkdir -p build && cd build || echo_exit "mkdir -p build && cd build failed"
  2. ../configure || echo_exit "configure failed"
  3. make || echo_exit "make failed"
  4. make DESTDIR=$PWD/install install || echo_exit "install failed"
  5. echo "now try to make distcheck, it may take some seconds, please wait ..."
  6. make dist && make distcheck || echo_exit "make dist && make distcheck failed"
复制代码
# 6. run testecho "Run test:"
  1. ./src/tests/test || echo_exit "run test failed"
  2. echo "If you want run test positioned at install, you should set LD_LIBRARY_PATH, or else you need install it in standard directory with root privilege"
  3. echo "Autoconf for multi directories tests success over !"
  4. 执行方法如下:
  5. chmod +x atdirs.sh && ./atdirs.sh
复制代码

如果一切正常,你应该看到类似如下的结果:Autotools for autotools multi directories (stands for atdirs)
  1. now try to generates configure, it may take some seconds, please wait ...
  2. libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, `build-aux'.
  3. libtoolize: copying file `build-aux/ltmain.sh'
  4. libtoolize: putting macros in AC_CONFIG_MACRO_DIR, `m4'.
  5. libtoolize: copying file `m4/libtool.m4'
  6. libtoolize: copying file `m4/ltoptions.m4'
  7. libtoolize: copying file `m4/ltsugar.m4'
  8. libtoolize: copying file `m4/ltversion.m4'
  9. libtoolize: copying file `m4/lt~obsolete.m4'
  10. configure.ac:7: installing `build-aux/config.guess'
  11. configure.ac:7: installing `build-aux/config.sub'
  12. configure.ac:9: installing `build-aux/install-sh'
  13. configure.ac:9: installing `build-aux/missing'
  14. src/lib1/Makefile.am: installing `build-aux/depcomp'
  15. Makefile.am: installing `./INSTALL'
  16. checking build system type... x86_64-unknown-linux-gnu
  17. checking host system type... x86_64-unknown-linux-gnu
  18. checking for a BSD-compatible install... /usr/bin/install -c
  19. <--------------------------------------
  20. 中间很长的一段省略掉了。。。
  21. --------------------------------------->
  22. make[2]: Leaving directory `/home/yanyg/work/git/t410/github.yanyg/misc/auto-tools/example/atdirs/build/ycc-1.0.0/_build'
  23. rm -f config.status config.cache config.log configure.lineno config.status.lineno
  24. rm -f Makefile
  25. make[1]: Leaving directory `/home/yanyg/work/git/t410/github.yanyg/misc/auto-tools/example/atdirs/build/ycc-1.0.0/_build'
  26. { test ! -d "ycc-1.0.0" || { find "ycc-1.0.0" -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr "ycc-1.0.0"; }; }
  27. ===========================================
  28. ycc-1.0.0 archives ready for distribution:
  29. ycc-1.0.0.tar.gz
  30. ===========================================
  31. Run test:
  32. lib1-print: atdirs 1.0.0 <lib-1>
  33. lib2-print: atdirs 1.0.0 <lib-2>
  34. If you want run test positioned at install, you should set LD_LIBRARY_PATH, or else you need install it in standard directory with root privilege
  35. Autoconf for multi directories tests success over !
复制代码
需要提醒的一点:libtool编译后生成的atdirs/build/src/tests/test实际上是一个脚本,实际的可执行文件在atdirs/build/src/tests/.libs下。


2. configure.ac 分析说明
3. Makefile.am 分析说明
4. autoconf 生成的 Makefile 提供的几种常用目标
5. 资源推荐
   ( 待续。。。)

作者:cppgp 发表于2012-2-15 23:55:49 原文链接

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-4 06:06 , Processed in 0.018974 second(s), 7 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表