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