魔鬼autotools升华使用

2019-07-13 08:22发布

 

    这几天没怎么出去跑宣讲会,因为那些公司我不感兴趣,感兴趣的公司说这两天会给我电话面试,等了N久,就是没有,真搞不懂我问题倒底出在哪,找工作归找工作; 我自己的感兴趣的嵌入式还得学,也够郁闷的,白天上自习看模拟数字,晚上会宿舍研究嵌入式linux,自从我成功把vivi移植后,一直想研究vivi怎么写,呵呵,倒好,发现N多问题,比如这么多文件,怎么编译,那么多的makefile,还有很多文件不是很清楚,怎么办,继续奋斗呗,我把图书关于嵌入式linux的书都翻遍了,发现讲这方面的实在太少了,真的想说那句话,linux真应该从娃娃抓起。好在发现在《嵌入式系统与应用开发》中有一小段篇幅是利用autotools来处理很多.c的软件,先前前面我讲过怎样处理一个.c的文件,呵呵,今天来玩玩多个.c的makefile的编写,好了,开始:

首先新建一个hello的文件夹,然后在hello下新建add和sub两个文件夹,在add下编写一个add.c,内容如下:
int add(int x,int y)
{
        return x+y;
}
在sub下编写一个sub.c的文件,内容如下:
int sub(int x,int y)
{
        return x-y;
}
在hello下编写main.c的文件,内容如下:
#include
                                                                               
int main(void)
{
        int sub_x,sub_y,add_x,add_y;
        printf("sub_x: ");
        scanf("%d",&sub_x);
        printf("/nsub_y: ");
        scanf("%d",&sub_y);
        add_x=sub(sub_x,sub_y);
        printf("/nadd_x=sub(%d, %d)= %d",sub_x,sub_y,add_x);
        printf("/nadd_y: ");
        scanf("%d",&add_y);
        printf("/n%d",add(add_x,add_y));
}


呵呵,这些弄完后,准备开始了哦,呵呵!先说下步骤:
1> 首先运行autoscan,原因我也不知道
2> 修改configure.scan。除了要添加AM_INIT_AUTOMAKE,还要加上一行AC_PROG_LIBTOOL,这一个宏打开对Libtool的处理过程,为后面的Libtool做准备,AC_OUTPUT输出三个makefile,呵呵,AC_OUTPUT(Makefile add/Makefile sub/Makefile),最后要将configure.scan保存为configure.in
3> 运行libtoolize生成一些支持文件
4> 运行aclocal和autoscanf,还要运行autoheader,这一项是书上没有说的,但是我发现如果不运行autoheader,那么在后面的make会产生一个错误,说找不到configure.h.in
5> 在add下编写Makefile.am,内容如下:
lib_LTLIBRARIES=libadd.la
libadd_la_SOURCES=add.c
在sub下也要编写Makefile.am,内容如下:
lib_LTLIBRARIES=libsub.la
libsub_la_SOURCES=sub.c
在hello下也要编写Makefile.am,内容如下:
UTOMAKE_OPTIONS=foreign
SUBDIRS=add sub
bin_PROGRAMS=main
main_SOURCES=main.c
main_LDADD=add/libadd.la sub/libsub.la
6> 写好了Makefile.am,运行automake -a
7> 然后执行 ./configure
8> 最后make
[root@localhost root]# cd hello
[root@localhost hello]# ls
add  main.c  sub
[root@localhost hello]# autoscan   
[root@localhost hello]# ls
add  autoscan.log  configure.scan  main.c  sub
[root@localhost hello]# vim configure.in
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
                                                                               
AC_PREREQ(2.57)
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AM_INIT_AUTOMAKE(main,1.0)
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADER([config.h])
                                                                               
# Checks for programs.
AC_PROG_CC
                                                                               
# Checks for libraries.
AC_PROG_LIBTOOL
                                                                               
# Checks for header files.
                                                                               
# Checks for typedefs, structures, and compiler characteristics.
                                                                               
# Checks for library functions.
                                                                               
AC_OUTPUT(Makefile add/Makefile sub/Makefile)

[root@localhost hello]# libtoolize
You should add the contents of `/usr/share/aclocal/libtool.m4' to `aclocal.m4'.
[root@localhost hello]# ls
add           config.guess  configure.in    ltmain.sh  sub
autoscan.log  config.sub    configure.scan  main.c
[root@localhost hello]# aclocal
[root@localhost hello]# autoconf
[root@localhost hello]# autoheader
[root@localhost hello]# ls
aclocal.m4      autoscan.log  config.sub    configure.scan  sub
add             config.guess  configure     ltmain.sh
autom4te.cache  config.h.in   configure.in  main.c
//接下来编写3个Makefile.am,这个过程我就不细说啦。
[root@localhost hello]# ls
aclocal.m4      autoscan.log  config.sub    configure.scan  Makefile.am
add             config.guess  configure     ltmain.sh       sub
autom4te.cache  config.h.in   configure.in  main.c
[root@localhost hello]# ls ./add
add.c  Makefile.am
[root@localhost hello]# ls ./sub
Makefile.am  sub.c
[root@localhost hello]# automake -a
configure.in:8: `automake requires `AM_CONFIG_HEADER', not `AC_CONFIG_HEADER'
configure.in: installing `./install-sh'
configure.in: installing `./mkinstalldirs'
configure.in: installing `./missing'
Makefile.am: installing `./INSTALL'
Makefile.am: required file `./NEWS' not found
Makefile.am: required file `./README' not found
Makefile.am: installing `./COPYING'
Makefile.am: required file `./AUTHORS' not found
Makefile.am: required file `./ChangeLog' not found
Makefile.am: installing `./depcomp'
[root@localhost hello]# ls
aclocal.m4      config.guess  configure.in    INSTALL     Makefile.am    sub
add             config.h.in   configure.scan  install-sh  Makefile.in
autom4te.cache  config.sub    COPYING         ltmain.sh   missing
autoscan.log    configure     depcomp         main.c      mkinstalldirs
//又多了几个东东
[root@localhost hello]# ./configure   //这个过程比较长
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI C... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking for ld used by GCC... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for /usr/bin/ld option to reload object files... -r
checking for BSD-compatible nm... /usr/bin/nm -B
checking for a sed that does not truncate output... /bin/sed
checking whether ln -s works... yes
checking how to recognise dependent libraries... pass_all
checking command to parse /usr/bin/nm -B output... ok
checking how to run the C preprocessor... gcc -E
checking for egrep... grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking dlfcn.h usability... yes
checking dlfcn.h presence... yes
checking for dlfcn.h... yes
checking for ranlib... ranlib
checking for strip... strip
checking for objdir... .libs
checking for gcc option to produce PIC... -fPIC
checking if gcc PIC flag -fPIC works... yes
checking if gcc static flag -static works... yes
checking if gcc supports -c -o file.o... yes
checking if gcc supports -c -o file.lo... yes
checking if gcc supports -fno-rtti -fno-exceptions... yes
checking whether the linker (/usr/bin/ld) supports shared libraries... yes
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking dynamic linker characteristics... GNU/Linux ld.so
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes
checking whether -lc should be explicitly linked in... no
creating libtool
configure: creating ./config.status
config.status: creating Makefile   //看到这三个Makefile了没有,呵呵,它已经建好了
config.status: creating add/Makefile
config.status: creating sub/Makefile
config.status: creating config.h
config.status: executing depfiles commands
[root@localhost hello]# make   //这个也有点花时间
cd . && /bin/sh ./config.status config.h
config.status: creating config.h
config.status: config.h is unchanged
make  all-recursive
make[1]: Entering directory `/root/hello'
Making all in add
make[2]: Entering directory `/root/hello/add'
source='add.c' object='add.lo' libtool=yes /
depfile='.deps/add.Plo' tmpdepfile='.deps/add.TPlo' /
depmode=gcc3 /bin/sh ../depcomp /
/bin/sh ../libtool --mode=compile gcc -DHAVE_CONFIG_H -I. -I. -I..     -g -O2 -c -o add.lo `test -f 'add.c' || echo './'`add.c
rm -f .libs/add.lo
gcc -DHAVE_CONFIG_H -I. -I. -I.. -g -O2 -c add.c -MT add.lo -MD -MP -MF .deps/add.TPlo  -fPIC -DPIC -o .libs/add.lo
gcc -DHAVE_CONFIG_H -I. -I. -I.. -g -O2 -c add.c -MT add.lo -MD -MP -MF .deps/add.TPlo -o add.o >/dev/null 2>&1
mv -f .libs/add.lo add.lo
/bin/sh ../libtool --mode=link gcc  -g -O2   -o libadd.la -rpath /usr/local/lib
 add.lo
rm -fr .libs/libadd.la .libs/libadd.* .libs/libadd.*
gcc -shared  add.lo   -Wl,-soname -Wl,libadd.so.0 -o .libs/libadd.so.0.0.0
(cd .libs && rm -f libadd.so.0 && ln -s libadd.so.0.0.0 libadd.so.0)
(cd .libs && rm -f libadd.so && ln -s libadd.so.0.0.0 libadd.so)
ar cru .libs/libadd.a  add.o
ranlib .libs/libadd.a
creating libadd.la
(cd .libs && rm -f libadd.la && ln -s ../libadd.la libadd.la)
make[2]: Leaving directory `/root/hello/add'
Making all in sub
make[2]: Entering directory `/root/hello/sub'
source='sub.c' object='sub.lo' libtool=yes /
depfile='.deps/sub.Plo' tmpdepfile='.deps/sub.TPlo' /
depmode=gcc3 /bin/sh ../depcomp /
/bin/sh ../libtool --mode=compile gcc -DHAVE_CONFIG_H -I. -I. -I..     -g -O2 -c -o sub.lo `test -f 'sub.c' || echo './'`sub.c
rm -f .libs/sub.lo
gcc -DHAVE_CONFIG_H -I. -I. -I.. -g -O2 -c sub.c -MT sub.lo -MD -MP -MF .deps/sub.TPlo  -fPIC -DPIC -o .libs/sub.lo
gcc -DHAVE_CONFIG_H -I. -I. -I.. -g -O2 -c sub.c -MT sub.lo -MD -MP -MF .deps/sub.TPlo -o sub.o >/dev/null 2>&1
mv -f .libs/sub.lo sub.lo
/bin/sh ../libtool --mode=link gcc  -g -O2   -o libsub.la -rpath /usr/local/lib
 sub.lo
rm -fr .libs/libsub.la .libs/libsub.* .libs/libsub.*
gcc -shared  sub.lo   -Wl,-soname -Wl,libsub.so.0 -o .libs/libsub.so.0.0.0
(cd .libs && rm -f libsub.so.0 && ln -s libsub.so.0.0.0 libsub.so.0)
(cd .libs && rm -f libsub.so && ln -s libsub.so.0.0.0 libsub.so)
ar cru .libs/libsub.a  sub.o
ranlib .libs/libsub.a
creating libsub.la
(cd .libs && rm -f libsub.la && ln -s ../libsub.la libsub.la)
make[2]: Leaving directory `/root/hello/sub'
make[2]: Entering directory `/root/hello'
source='main.c' object='main.o' libtool=no /
depfile='.deps/main.Po' tmpdepfile='.deps/main.TPo' /
depmode=gcc3 /bin/sh ./depcomp /
gcc -DHAVE_CONFIG_H -I. -I. -I.     -g -O2 -c `test -f 'main.c' || echo './'`main.c
/bin/sh ./libtool --mode=link gcc  -g -O2   -o main  main.o add/libadd.la sub/libsub.la
mkdir .libs
gcc -g -O2 -o .libs/main main.o  add/.libs/libadd.so sub/.libs/libsub.so -Wl,--rpath -Wl,/usr/local/lib
creating main   //看到它了没有,它是可执行的文件,运行它就可以看到结果拉
cd . && /bin/sh ./config.status config.h
config.status: creating config.h
config.status: config.h is unchanged
make[2]: Leaving directory `/root/hello'
make[1]: Leaving directory `/root/hello'
[root@localhost hello]# ls
aclocal.m4      config.h.in    configure.scan  ltmain.sh    Makefile.in
add             config.log     COPYING         main         missing
autom4te.cache  config.status  depcomp         main.c       mkinstalldirs
autoscan.log    config.sub     INSTALL         main.o       sub
config.guess    configure      install-sh      Makefile
config.h        configure.in   libtool         Makefile.am
[root@localhost hello]# ./main
sub_x: 6
 
sub_y: 5
 
add_x=sub(6, 5)= 1
add_y: 4
 
5[root@localhost hello]#

呵呵,完毕!这东东要多练才能熟练。