Chinaunix首页 | 论坛 | 博客
  • 博客访问: 134693
  • 博文数量: 20
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 247
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-12 22:24
个人简介

学霸

文章分类

全部博文(20)

文章存档

2016年(1)

2015年(11)

2014年(8)

我的朋友

分类: LINUX

2015-08-10 00:53:22

source file app.c fun.c fun.h(source codes are in the attachment)
----------static library(method 1)
gcc -c fun.c
gcc -c app.c
ar cr libfun.a fun.o
gcc -o app app.o -L. -lfun
app size is 7453

ldd app
linux-gate.so.1 =>  (0xb7792000)
    libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb75db000)
    /lib/ld-linux.so.2 (0xb7793000)

----------dynamic library(method 2)
gcc -fPIC -c fun.c
gcc -c app.c
gcc -shared -o libfun.so fun.o
gcc -o app app.o -L. -lfun -Wl,-rpath,.
app size is 7145

ldd app
linux-gate.so.1 =>  (0xb7763000)
    libfun.so => ./libfun.so (0xb775e000)
    libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb75a9000)
    /lib/ld-linux.so.2 (0xb7764000)

test 1:
get app by method 1
./app
output---xiaohei
now,change file fun.c,simple to use heidan instead of xiaohei
update the libfun.a
gcc -c fun.c
ar cr -o libfun.a fun.o
./app
output---xiaohei(unchanged)

test 2
get app by method 2
./app
output---xiaohei
now,change file fun.c,simple to use heidan instead of xiaohei
update the libfun.so
gcc -fPIC -c fun.c
gcc -shared -o libfun.so fun.o
./app
output---heidan(changed)

test 3
when libfun.a and libfun.so are in the same folder
just execute gcc -o app app.o -L. -lfun
ldd app
linux-gate.so.1 =>  (0xb7721000)
    libfun.so => not found
    libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb756a000)
    /lib/ld-linux.so.2 (0xb7722000)
there are two solution for it
solution 1:
use static library:gcc -static -o app app.o -L. -lfun
after this step:all Library Dependencies of app are static
solution 2:
use dynamic library:gcc -o app app.o -L. -lfun -Wl,-rpath,.or use LD_LIBRARY_PATH


advantage of shared library(disadvantage of static library)
1.it saves space on the system where the program is installed.
  If you are installing 10 programs,they all make use of the same shared library.
  If you used a static archive instead, the archive is included in all 10 programs.
2.users can upgrade the libraries with-out upgrading all the programs that depend on them.
  referce to test 1 and 2.

disadvantage of shared library(advantage of static library)
1.cause of the item 2 in advantage of shared library.
  if you’re developing mission-critical software, you might rather link
to a static archive so that an upgrade to shared libraries on the system won’t affect
your program.

caution:
  Suppose that both libfun.a and libfun.so are available and in the same path the -L
  point to.in this case linker will choose the shared library version referce to test 3

stc_dyn.zip
阅读(1659) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~