2009年11月25日水曜日

【autotools】9(US 10) GNU Libtool入門 (2)


** 9(US 10) GNU Libtool入門
- Libtoolを単に利用するだけではなく、中身も探って
みよう、シェルプロンプトからLibtoolを使ってみよ
う、というお話。
- ここで、ざっと読んでみるが困難を感じる。
- GNU Libtool Infoをちらっと見てみる。やはり困難
を感じる。
- Linkers & Loadersで勉強。完了。
- GNU Libtool Infoもざっとやる。問題なし。
- そしてこの章に戻ってきた。
- 内容はわかるが、libtoolの利用例が古いlibtoolの
もので、そのままでは動かない。。。
- 最新のlibtoolでの操作に置き換えてみる。

- 訳書P93
- ltconfigによるホスト用libtoolの生成方法が記載
されている。しかし、現状のlibtoolではltconfig
は廃止されており、configureにてホスト用
libtoolを生成するようである。
- Ubuntuでは、Ubuntu用のlibtoolが生成済みで配布
されており、この作業にはソースパッケージが必
要である。
- ここでは割愛する。
- 訳書P97
- ここはlibtoolのI/Fが変更になっているところ。
- 現在は次のとおり。

9-US10 $ libtool --mode=compile gcc -c hello.c
libtool: compile: gcc -c hello.c -fPIC -DPIC -o .libs/hello.o
libtool: compile: gcc -c hello.c -o hello.o >/dev/null 2>&1
9-US10 $ ls
hello.c hello.lo hello.o
9-US10 $ libtool --mode=link gcc -rpath /usr/local/lib -o libhello.la hello.lo
libtool: link: gcc -shared .libs/hello.o -Wl,-soname -Wl,libhello.so.0 -o .libs/libhello.so.0.0.0
libtool: link: (cd ".libs" && rm -f "libhello.so.0" && ln -s "libhello.so.0.0.0" "libhello.so.0")
libtool: link: (cd ".libs" && rm -f "libhello.so" && ln -s "libhello.so.0.0.0" "libhello.so")
libtool: link: ar cru .libs/libhello.a hello.o
libtool: link: ranlib .libs/libhello.a
libtool: link: ( cd ".libs" && rm -f "libhello.la" && ln -s "../libhello.la" "libhello.la" )
9-US10 $ ls
hello.c hello.lo hello.o libhello.la
9-US10 $

- 次の部分は、本文にコンパイルとあるのでmodeを
compileにしているが、rpathスイッチがあるとい
うことはリンクすべきではないか。

9-US10 $ libtool --mode=compile c89 -rpath /usr/local/lib -c hello.c
libtool: compile: unable to infer tagged configuration
libtool: compile: specify a tag with `--tag'
9-US10 $

- 訳書P98

9-US10 $ libtool --mode=link gcc -static -o libhello.la hello.lo
libtool: link: ar cru .libs/libhello.a hello.o
libtool: link: ranlib .libs/libhello.a
libtool: link: ( cd ".libs" && rm -f "libhello.la" && ln -s "../libhello.la" "libhello.la" )
9-US10 $ ls
hello.c hello.lo hello.o libhello.la
9-US10 $

- 訳書P100

9-US10 $ libtool --mode=link gcc -o libtrim.la trim.lo
libtool: link: ar cru .libs/libtrim.a .libs/trim.o
libtool: link: ranlib .libs/libtrim.a
libtool: link: ( cd ".libs" && rm -f "libtrim.la" && ln -s "../libtrim.la" "libtrim.la" )
9-US10 $

- 訳書P100からP101へのまたぎ

9-US10 $ libtool --mode=link gcc -o libgraphics.la -lpng -ltiff -ljpeg -lz
libtool: link: ar cru .libs/libgraphics.a
libtool: link: ranlib .libs/libgraphics.a
libtool: link: ( cd ".libs" && rm -f "libgraphics.la" && ln -s "../libgraphics.la" "libgraphics.la" )
9-US10 $

- 訳書P101からP102へのまたぎ
- 実行可能ファイルのリンクをする前の仮定が省略
されているのでそこもやっておく。

hello $ ls
Makefile.am bootstrap configure.ac hello.c main.c
hello $ libtool --mode=compile gcc -c hello.c
libtool: compile: gcc -c hello.c -fPIC -DPIC -o .libs/hello.o
libtool: compile: gcc -c hello.c -o hello.o >/dev/null 2>&1
hello $ libtool --mode=link gcc -rpath /usr/local/lib -o libhello.la hello.lo
libtool: link: gcc -shared .libs/hello.o -Wl,-soname -Wl,libhello.so.0 -o .libs/libhello.so.0.0.0
libtool: link: (cd ".libs" && rm -f "libhello.so.0" && ln -s "libhello.so.0.0.0" "libhello.so.0")
libtool: link: (cd ".libs" && rm -f "libhello.so" && ln -s "libhello.so.0.0.0" "libhello.so")
libtool: link: ar cru .libs/libhello.a hello.o
libtool: link: ranlib .libs/libhello.a
libtool: link: ( cd ".libs" && rm -f "libhello.la" && ln -s "../libhello.la" "libhello.la" )
hello $ ls
Makefile.am bootstrap configure.ac hello.c hello.lo hello.o libhello.la main.c
hello $ libtool --mode=link gcc -o hello main.c libhello.la
libtool: link: gcc -o .libs/hello main.c ./.libs/libhello.so
hello $ ls
Makefile.am configure.ac hello.c hello.o main.c
bootstrap hello hello.lo libhello.la
hello $ ./hello
Hello, World!
hello $

- 訳書P102

hello $ libtool --mode=link gcc -static -o hello main.c libhello.la
libtool: link: gcc -o hello main.c ./.libs/libhello.a
hello $

- 訳書P103

- この例は、loader.oが存在しないので実行できな
い。

- 訳書P105

intro-hello $ rm hello *.a *.o
intro-hello $ libtool --mode=compile gcc -c trim.c
libtool: compile: gcc -c trim.c -fPIC -DPIC -o .libs/trim.o
libtool: compile: gcc -c trim.c -o trim.o >/dev/null 2>&1
intro-hello $ libtool --mode=link gcc -rpath /usr/local/lib -o libtrim.la trim.lo
libtool: link: gcc -shared .libs/trim.o -Wl,-soname -Wl,libtrim.so.0 -o .libs/libtrim.so.0.0.0
libtool: link: (cd ".libs" && rm -f "libtrim.so.0" && ln -s "libtrim.so.0.0.0" "libtrim.so.0")
libtool: link: (cd ".libs" && rm -f "libtrim.so" && ln -s "libtrim.so.0.0.0" "libtrim.so")
libtool: link: ar cru .libs/libtrim.a trim.o
libtool: link: ranlib .libs/libtrim.a
libtool: link: ( cd ".libs" && rm -f "libtrim.la" && ln -s "../libtrim.la" "libtrim.la" )
intro-hello $

- 訳書P106

intro-hello $ libtool --mode=compile gcc -c hello.c
libtool: compile: gcc -c hello.c -fPIC -DPIC -o .libs/hello.o
libtool: compile: gcc -c hello.c -o hello.o >/dev/null 2>&1
intro-hello $ libtool --mode=link gcc -rpath /usr/local/lib -o libhello.la hello.lo libtrim.la
libtool: link: gcc -shared .libs/hello.o -Wl,-rpath -Wl,/home/aka/scratch/autotools/9-US10/hello-2.0/intro-hello/.libs ./.libs/libtrim.so -Wl,-soname -Wl,libhello.so.0 -o .libs/libhello.so.0.0.0
libtool: link: (cd ".libs" && rm -f "libhello.so.0" && ln -s "libhello.so.0.0.0" "libhello.so.0")
libtool: link: (cd ".libs" && rm -f "libhello.so" && ln -s "libhello.so.0.0.0" "libhello.so")
libtool: link: ar cru .libs/libhello.a hello.o
libtool: link: ranlib .libs/libhello.a
libtool: link: ( cd ".libs" && rm -f "libhello.la" && ln -s "../libhello.la" "libhello.la" )
intro-hello $ ls
hello.c hello.lo hello.o libhello.la libtrim.la main.c trim.c trim.lo trim.o
intro-hello $ libtool --mode=link gcc -o hello main.c libhello.la
libtool: link: gcc -o .libs/hello main.c ./.libs/libhello.so /home/aka/scratch/autotools/9-US10/hello-2.0/intro-hello/.libs/libtrim.so
main.c: In function 'main':
main.c:7: warning: incompatible implicit declaration of built-in function 'exit'
intro-hello $ ./hello
Hello, World!
intro-hello $

- 訳書P107

intro-hello $ libtool --mode=link gcc -o hello-again -static main.c libhello.la
libtool: link: gcc -o hello-again main.c ./.libs/libhello.a /home/aka/scratch/autotools/9-US10/hello-2.0/intro-hello/.libs/libtrim.a
main.c: In function 'main':
main.c:7: warning: incompatible implicit declaration of built-in function 'exit'
intro-hello $ ls
hello hello.c hello.o libtrim.la trim.c trim.o
hello-again hello.lo libhello.la main.c trim.lo
intro-hello $ ./hello-again
Hello, World!
intro-hello $

intro-hello $ libtool --mode=link gcc -o hello main.c libhello.la
libtool: link: gcc -o .libs/hello main.c ./.libs/libhello.so -Wl,-rpath -Wl,/home/aka/scratch/autotools/9-US10/hello-2.0/intro-hello/_inst
main.c: In function 'main':
main.c:7: warning: incompatible implicit declaration of built-in function 'exit'
intro-hello $ ./hello
Hello, World!
intro-hello $

- 訳書P109

intro-hello $ libtool --mode=execute gdb hello
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu"...
(gdb) run
Starting program: /home/aka/local/work/scratch/autotools/9-US10/hello-2.0/intro-hello/.libs/lt-hello
Hello, World!

Program exited normally.
(gdb)

- 訳書P109
- libhelloはインストールできるが、libtrimはイン
ストールできない。なぜか。

intro-hello $ sudo libtool --mode=install cp libtrim.la /usr/local/lib/
libtool: install: cp .libs/libtrim.lai /usr/local/lib/libtrim.la
cp: cannot stat `.libs/libtrim.lai': No such file or directory
intro-hello $ sudo libtool --mode=install install -c libtrim.la /usr/local/lib/
libtool: install: install -c .libs/libtrim.lai /usr/local/lib/libtrim.la
install: cannot stat `.libs/libtrim.lai': No such file or directory
intro-hello $ sudo libtool --mode=install install -c libhello.la /usr/local/lib/
libtool: install: install -c .libs/libhello.so.0.0.0 /usr/local/lib/libhello.so.0.0.0
libtool: install: (cd /usr/local/lib && { ln -s -f libhello.so.0.0.0 libhello.so.0 || { rm -f libhello.so.0 && ln -s libhello.so.0.0.0 libhello.so.0; }; })
libtool: install: (cd /usr/local/lib && { ln -s -f libhello.so.0.0.0 libhello.so || { rm -f libhello.so && ln -s libhello.so.0.0.0 libhello.so; }; })
libtool: install: install -c .libs/libhello.lai /usr/local/lib/libhello.la
libtool: install: install -c .libs/libhello.a /usr/local/lib/libhello.a
libtool: install: chmod 644 /usr/local/lib/libhello.a
libtool: install: ranlib /usr/local/lib/libhello.a
libtool: install: warning: remember to run `libtool --finish /home/aka/scratch/autotools/9-US10/hello-2.0/intro-hello/_inst'
intro-hello $

- そうか。コンビニエンスライブラリの流れ
で、-rpathを一度/usr/local/libから外している
から駄目なんだろうな。通常のライブラリでやり
直していれてみる。

intro-hello $ rm hello hello-again *.la *.lo *.o
intro-hello $ ls
hello.c main.c trim.c
intro-hello $ libtool --mode=compile gcc -c trim.c
libtool: compile: gcc -c trim.c -fPIC -DPIC -o .libs/trim.o
libtool: compile: gcc -c trim.c -o trim.o >/dev/null 2>&1
intro-hello $ libtool --mode=link gcc -rpath /usr/local/lib -o libtrim.la trim.lo
libtool: link: rm -fr .libs/libtrim.a .libs/libtrim.la
libtool: link: gcc -shared .libs/trim.o -Wl,-soname -Wl,libtrim.so.0 -o .libs/libtrim.so.0.0.0
libtool: link: (cd ".libs" && rm -f "libtrim.so.0" && ln -s "libtrim.so.0.0.0" "libtrim.so.0")
libtool: link: (cd ".libs" && rm -f "libtrim.so" && ln -s "libtrim.so.0.0.0" "libtrim.so")
libtool: link: ar cru .libs/libtrim.a trim.o
libtool: link: ranlib .libs/libtrim.a
libtool: link: ( cd ".libs" && rm -f "libtrim.la" && ln -s "../libtrim.la" "libtrim.la" )
intro-hello $ libtool --mode=compile gcc -c hello.c
libtool: compile: gcc -c hello.c -fPIC -DPIC -o .libs/hello.o
libtool: compile: gcc -c hello.c -o hello.o >/dev/null 2>&1
intro-hello $ libtool --mode=link gcc -rpath /usr/local/lib -o libhello.la hello.lo libtrim.la
libtool: link: rm -fr .libs/libhello.a .libs/libhello.la .libs/libhello.lai .libs/libhello.so .libs/libhello.so.0 .libs/libhello.so.0.0.0
libtool: link: gcc -shared .libs/hello.o -Wl,-rpath -Wl,/home/aka/scratch/autotools/9-US10/hello-2.0/intro-hello/.libs ./.libs/libtrim.so -Wl,-soname -Wl,libhello.so.0 -o .libs/libhello.so.0.0.0
libtool: link: (cd ".libs" && rm -f "libhello.so.0" && ln -s "libhello.so.0.0.0" "libhello.so.0")
libtool: link: (cd ".libs" && rm -f "libhello.so" && ln -s "libhello.so.0.0.0" "libhello.so")
libtool: link: ar cru .libs/libhello.a hello.o
libtool: link: ranlib .libs/libhello.a
libtool: link: ( cd ".libs" && rm -f "libhello.la" && ln -s "../libhello.la" "libhello.la" )
intro-hello $ libtool --mode=link gcc -o hello main.c libhello.la
libtool: link: gcc -o .libs/hello main.c ./.libs/libhello.so /home/aka/scratch/autotools/9-US10/hello-2.0/intro-hello/.libs/libtrim.so
main.c: In function 'main':
main.c:7: warning: incompatible implicit declaration of built-in function 'exit'
intro-hello $ ./hello
Hello, World!
intro-hello $ ls .libs
hello libhello.la libhello.so.0 libtrim.la libtrim.so.0 trim.o
hello.o libhello.lai libhello.so.0.0.0 libtrim.lai libtrim.so.0.0.0
libhello.a libhello.so libtrim.a libtrim.so lt-hello
intro-hello $ sudo libtool --mode=install cp libtrim.la /usr/local/lib
[sudo] password for aka:
libtool: install: cp .libs/libtrim.so.0.0.0 /usr/local/lib/libtrim.so.0.0.0
libtool: install: (cd /usr/local/lib && { ln -s -f libtrim.so.0.0.0 libtrim.so.0 || { rm -f libtrim.so.0 && ln -s libtrim.so.0.0.0 libtrim.so.0; }; })
libtool: install: (cd /usr/local/lib && { ln -s -f libtrim.so.0.0.0 libtrim.so || { rm -f libtrim.so && ln -s libtrim.so.0.0.0 libtrim.so; }; })
libtool: install: cp .libs/libtrim.lai /usr/local/lib/libtrim.la
libtool: install: cp .libs/libtrim.a /usr/local/lib/libtrim.a
libtool: install: chmod 644 /usr/local/lib/libtrim.a
libtool: install: ranlib /usr/local/lib/libtrim.a
libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin:/sbin" ldconfig -n /usr/local/lib
----------------------------------------------------------------------
Libraries have been installed in:
/usr/local/lib

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
intro-hello $

- 入った。

- 訳書P109

intro-hello $ sudo libtool --mode=execute cp hello /usr/local/bin
intro-hello $

- helloではなく、lt-helloとして入る。
- ライブラリパスをみるとローカルになっている。
なんでだろう。

intro-hello $ readelf -d /usr/local/bin/lt-hello

Dynamic section at offset 0xe10 contains 24 entries:
Tag Type Name/Value
0x0000000000000001 (NEEDED) Shared library: [libhello.so.0]
0x0000000000000001 (NEEDED) Shared library: [libtrim.so.0]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
0x000000000000000f (RPATH) Library rpath: [/home/aka/scratch/autotools/9-US10/hello-2.0/intro-hello/.libs]
0x000000000000000c (INIT) 0x400568
0x000000000000000d (FINI) 0x4007b8
0x0000000000000004 (HASH) 0x400278
0x000000006ffffef5 (GNU_HASH) 0x4002b8
0x0000000000000005 (STRTAB) 0x4003f8
0x0000000000000006 (SYMTAB) 0x4002f0
0x000000000000000a (STRSZ) 213 (bytes)
0x000000000000000b (SYMENT) 24 (bytes)
0x0000000000000015 (DEBUG) 0x0
0x0000000000000003 (PLTGOT) 0x600fe8
0x0000000000000002 (PLTRELSZ) 72 (bytes)
0x0000000000000014 (PLTREL) RELA
0x0000000000000017 (JMPREL) 0x400520
0x0000000000000007 (RELA) 0x400508
0x0000000000000008 (RELASZ) 24 (bytes)
0x0000000000000009 (RELAENT) 24 (bytes)
0x000000006ffffffe (VERNEED) 0x4004e8
0x000000006fffffff (VERNEEDNUM) 1
0x000000006ffffff0 (VERSYM) 0x4004ce
0x0000000000000000 (NULL) 0x0
intro-hello $


- 訳書P115
- lt-helloが謎だ。

intro-hello $ sudo libtool --mode=execute cp hello /usr/local/bin
intro-hello $ sudo libtool --mode=uninstall rm -f /usr/local/bin/hello
intro-hello $ ls /usr/local/bin
gauche-cesconv gauche-config gauche-install gauche-package gosh lt-hello
intro-hello $ sudo libtool --mode=uninstall rm -f /usr/local/lib/libhello.la
libtool: uninstall: rm -f /usr/local/lib/libhello.la /usr/local/lib/libhello.so.0.0.0 /usr/local/lib/libhello.so.0 /usr/local/lib/libhello.so /usr/local/lib/libhello.a
intro-hello $ sudo libtool --mode=uninstall rm -f /usr/local/bin/lt-hello
libtool: uninstall: rm -f /usr/local/bin/lt-hello
intro-hello $ sudo libtool --mode=uninstall rm -f /usr/local/lib/libtrim.la
libtool: uninstall: rm -f /usr/local/lib/libtrim.la /usr/local/lib/libtrim.so.0.0.0 /usr/local/lib/libtrim.so.0 /usr/local/lib/libtrim.so /usr/local/lib/libtrim.a
intro-hello $ ls /usr/local/lib
eclipse gauche libgauche.so libgauche.so.0 libgauche.so.0.8.14 python2.5 site_ruby
intro-hello $


こつこつ。

0 件のコメント: