test on Dynamically Linked "Shared Object" Libraries: (.so)

来源:互联网 发布:如何投诉淘宝小二 编辑:程序博客网 时间:2024/05/18 14:45

Reference link: http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html


1) code and c file for test

ubuntu:/local/dynamic-lib-test$ cat test.h void test();ubuntu:/local/dynamic-lib-test$ cat test.c #include <stdio.h>void test(){printf("test!!");}ubuntu:/local/dynamic-lib-test$ cat main.c #include "test.h"int main(){test();return 0;}


2) compile libtest.so and execute file app

gaow@gaow-ubuntu:/local/dynamic-lib-test$ gcc  -shared test.c -o libtest.so -fPIC -vgaow@gaow-ubuntu:/local/dynamic-lib-test$ gcc -Wall -I/local/dynamic-lib-test -L/local/dynamic-lib-test main.c -ltest  -o appgaow@gaow-ubuntu:/local/dynamic-lib-test$ lsapp  libtest.so  main.c  test.c  test.h


3) using ldd and nm check the dependence and symbol

gaow@gaow-ubuntu:/local/dynamic-lib-test$ ldd libtest.so linux-vdso.so.1 =>  (0x00007fff6009d000)libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fedb5f40000)/lib64/ld-linux-x86-64.so.2 (0x00007fedb6522000)gaow@gaow-ubuntu:/local/dynamic-lib-test$ ldd applinux-vdso.so.1 =>  (0x00007fff07bfc000)libtest.so => not found    <======  LD_LIBRARY_PATH not set so can not find libtest.so!!!libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f891c93d000)/lib64/ld-linux-x86-64.so.2 (0x00007f891cd1d000)gaow@gaow-ubuntu:/local/dynamic-lib-test$ nm libtest.so 0000000000201038 B __bss_start0000000000201038 b completed.6973                 w __cxa_finalize@@GLIBC_2.2.500000000000005e0 t deregister_tm_clones0000000000000650 t __do_global_dtors_aux0000000000200e08 t __do_global_dtors_aux_fini_array_entry0000000000201030 d __dso_handle0000000000200e18 d _DYNAMIC0000000000201038 D _edata0000000000201040 B _end00000000000006dc T _fini0000000000000690 t frame_dummy0000000000200e00 t __frame_dummy_init_array_entry0000000000000768 r __FRAME_END__0000000000201000 d _GLOBAL_OFFSET_TABLE_                 w __gmon_start__0000000000000578 T _init                 w _ITM_deregisterTMCloneTable                 w _ITM_registerTMCloneTable0000000000200e10 d __JCR_END__0000000000200e10 d __JCR_LIST__                 w _Jv_RegisterClasses                 U printf@@GLIBC_2.2.50000000000000610 t register_tm_clones00000000000006c5 T test          <========test function is here and flag as "T". means OK0000000000201038 d __TMC_END__gaow@gaow-ubuntu:/local/dynamic-lib-test$ nm app 0000000000601040 B __bss_start0000000000601040 b completed.69730000000000601030 D __data_start0000000000601030 W data_start00000000004005d0 t deregister_tm_clones0000000000400640 t __do_global_dtors_aux0000000000600e08 t __do_global_dtors_aux_fini_array_entry0000000000601038 D __dso_handle0000000000600e18 d _DYNAMIC0000000000601040 D _edata0000000000601048 B _end0000000000400724 T _fini0000000000400660 t frame_dummy0000000000600e00 t __frame_dummy_init_array_entry0000000000400858 r __FRAME_END__0000000000601000 d _GLOBAL_OFFSET_TABLE_                 w __gmon_start__0000000000400540 T _init0000000000600e08 t __init_array_end0000000000600e00 t __init_array_start0000000000400730 R _IO_stdin_used                 w _ITM_deregisterTMCloneTable                 w _ITM_registerTMCloneTable0000000000600e10 d __JCR_END__0000000000600e10 d __JCR_LIST__                 w _Jv_RegisterClasses0000000000400720 T __libc_csu_fini00000000004006b0 T __libc_csu_init                 U __libc_start_main@@GLIBC_2.2.5000000000040068d T main0000000000400600 t register_tm_clones00000000004005a0 T _start                 U test   <============= test function is here but flag as "U", means currently can not find definition0000000000601040 D __TMC_END__gaow@gaow-ubuntu:/local/dynamic-lib-test$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/local/dynamic-lib-testgaow@gaow-ubuntu:/local/dynamic-lib-test$ ldd app    linux-vdso.so.1 =>  (0x00007fff5df3c000)    libtest.so (0x00007f9fa57b9000)  <===  after set LD_LIBRARY_PATH, the so can be found!!!    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9fa53db000)    /lib64/ld-linux-x86-64.so.2 (0x00007f9fa59bd000)


4) execute:

gaow@gaow-ubuntu:/local/dynamic-lib-test$ ./apptest!!



0 0
原创粉丝点击