GNU Autotools (autoconf, automake, libtool) 编译简单示例2
1 基础
先阅读GNU Autotools (autoconf, automake, libtool, etc.) autoconf简单示例并成功执行、理解所说内容。
先提供一个脚本。拷贝所有内容保存为文件atdir.sh并添加可执行权限,然后执行。注意,最好不要以根用户身份执行它。
#!/bin/sh echo "Autotools for autotools multi directories (stands for atdirs)" echo_exit() { echo "ERROR: $@" exit 1 } # 1. prepare directories and dummy files [ ! -e "atdirs" ] || echo_exit "atdirs exists, please remove or rename it" mkdir atdirs && cd atdirs || echo_exit "mkdir -p atdirs or cd atdirs failed" mkdir -p doc build-aux m4 man src/{include/atdirs,lib{1,2},tests} || \ echo_exit "create subdirectory failed" touch AUTHORS BUGS ChangeLog COPYING NEWS README || \ echo_exit "touch dummy files failed" # 2. generates source, header files cat > src/include/atdirs/lib1.h <<EOF #ifndef __ATDIRS_LIB1_H_ #define __ATDIRS_LIB1_H_ void lib1_print(); #endif EOF cat > src/include/atdirs/lib2.h <<EOF #ifndef __ATDIRS_LIB2_H_ #define __ATDIRS_LIB2_H_ void lib2_print(); #endif EOF cat > src/lib1/print.c <<EOF #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <stdio.h> #include <atdirs/lib1.h> void lib1_print() { printf("lib1-print: " PACKAGE_STRING " <lib-1>\n"); } EOF cat > src/lib2/print.c <<EOF #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <stdio.h> #include <atdirs/lib2.h> void lib2_print() { printf("lib2-print: " PACKAGE_STRING " <lib-2>\n"); } EOF cat > src/tests/test.c <<EOF #include <atdirs/lib1.h> #include <atdirs/lib2.h> int main(void) { lib1_print(); lib2_print(); return 0; } EOF # 2. generates Makefil.am files cat > Makefile.am << EOF SUBDIRS = doc man src ACLOCAL_AMFLAGS = -I m4 EXTRA_DIST = ChangeLog BUGS COPYING EOF # doc, man needs an empty Makefile.am touch {doc,man}/Makefile.am # src needs a valid Makefile.am cat > src/Makefile.am << EOF SUBDIRS = lib1 lib2 . tests lib_LTLIBRARIES = libatdirs.la libatdirs_la_SOURCES = libatdirs_la_LIBADD = lib1/libatdirs_lib1.la lib2/libatdirs_lib2.la libatdirs_la_LDFLAGS = -release \$(PACKAGE_VERSION) -version-info 1 include_HEADERS = \$(top_srcdir)/\$(atdirs_includedir)/atdirs/lib1.h \ \$(top_srcdir)/\$(atdirs_includedir)/atdirs/lib2.h EOF cat > src/lib1/Makefile.am << EOF include \$(top_srcdir)/Makefile.rules noinst_LTLIBRARIES = libatdirs_lib1.la libatdirs_lib1_la_SOURCES = print.c EOF cat > src/lib2/Makefile.am << EOF include \$(top_srcdir)/Makefile.rules noinst_LTLIBRARIES = libatdirs_lib2.la libatdirs_lib2_la_SOURCES = print.c EOF cat > src/tests/Makefile.am << EOF include \$(top_srcdir)/Makefile.rules bin_PROGRAMS = test test_SOURCES = test.c test_LDADD = ../libatdirs.la EOF # Makefile.rules: cat > Makefile.rules << EOF || echo_exit "generates Makefile.rules failed" AM_CPPFLAGS = -I\$(top_srcdir)/\$(atdirs_includedir) install-header: \$(MKDIR_P) "\$(DESTDIR)\$(includedir)" || exit $? cp -av \$(top_srcdir)/\$(atdirs_includedir) "\$(DESTDIR)\$(includedir)" \ || exit $? EOF # 3. generates configure.ac cat > configure.ac << EOF # -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_PREREQ([2.67]) AC_INIT([atdirs], [1.0.0], [cppgp@qq.com], [ycc], [http://blog.csdn.net/cppgp]) AC_CONFIG_AUX_DIR([build-aux]) AC_CANONICAL_BUILD AC_CANONICAL_HOST AM_INIT_AUTOMAKE([-Wall -Werror gnu]) LT_INIT AC_COPYRIGHT([GPLv2]) AC_REVISION([\$Revision: 1.0 \$]) AC_CONFIG_SRCDIR([ChangeLog]) AC_CONFIG_HEADERS([config.h]) AC_CONFIG_MACRO_DIR([m4]) # Checks for programs. AC_PROG_CC # Checks for libraries. # Checks for header files. # Checks for library functions. AC_SUBST([atdirs_includedir], [src/include]) AC_CONFIG_FILES([Makefile doc/Makefile man/Makefile src/Makefile src/lib1/Makefile src/lib2/Makefile src/tests/Makefile ]) AC_OUTPUT EOF # 4. generates configure echo "now try to generates configure, it may take some seconds, please wait ..." autoreconf --install -Wall || echo_exit "autoreconf failed" # 5. builds it mkdir -p build && cd build || echo_exit "mkdir -p build && cd build failed" ../configure || echo_exit "configure failed" make || echo_exit "make failed" make DESTDIR=$PWD/install install || echo_exit "install failed" echo "now try to make distcheck, it may take some seconds, please wait ..." make dist && make distcheck || echo_exit "make dist && make distcheck failed" # 6. run test echo "Run test:" ./src/tests/test || echo_exit "run test failed" 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" echo "Autoconf for multi directories tests success over !"
执行方法如下:
~$ chmod +x atdirs.sh && ./atdirs.sh
如果一切正常,你应该看到类似如下的结果:
Autotools for autotools multi directories (stands for atdirs) now try to generates configure, it may take some seconds, please wait ... libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, `build-aux'. libtoolize: copying file `build-aux/ltmain.sh' libtoolize: putting macros in AC_CONFIG_MACRO_DIR, `m4'. libtoolize: copying file `m4/libtool.m4' libtoolize: copying file `m4/ltoptions.m4' libtoolize: copying file `m4/ltsugar.m4' libtoolize: copying file `m4/ltversion.m4' libtoolize: copying file `m4/lt~obsolete.m4' configure.ac:7: installing `build-aux/config.guess' configure.ac:7: installing `build-aux/config.sub' configure.ac:9: installing `build-aux/install-sh' configure.ac:9: installing `build-aux/missing' src/lib1/Makefile.am: installing `build-aux/depcomp' Makefile.am: installing `./INSTALL' checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking for a BSD-compatible install... /usr/bin/install -c <-------------------------------------- 中间很长的一段省略掉了。。。 ---------------------------------------> make[2]: Leaving directory `/home/yanyg/work/git/t410/github.yanyg/misc/auto-tools/example/atdirs/build/ycc-1.0.0/_build' rm -f config.status config.cache config.log configure.lineno config.status.lineno rm -f Makefile make[1]: Leaving directory `/home/yanyg/work/git/t410/github.yanyg/misc/auto-tools/example/atdirs/build/ycc-1.0.0/_build' { 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"; }; } =========================================== ycc-1.0.0 archives ready for distribution: ycc-1.0.0.tar.gz =========================================== Run test: lib1-print: atdirs 1.0.0 <lib-1> lib2-print: atdirs 1.0.0 <lib-2> 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 Autoconf for multi directories tests success over !
libtool编译后生成的atdirs/build/src/tests/test实际上是一个脚本,实际的可执行文件在atdirs/build/src/tests/.libs下。