Linux dynamic library and static Library Loading Basics

Source: Internet
Author: User

Linux dynamic library and static Library Loading Basics
Static Library
In Linux, use the AR command to create a static library file. The following is a command option:
D ----- delete an object from the specified static library file
M ----- move the file to the specified static library file
P ----- output the specified file in the static library file to the standard output
Q ----- quickly append a file to a static library file
R ----- Insert the file into the static library file
T ----- display the list of files in the static library file
X ----- Extract files from static library files
There are also multiple modifiers to modify the above basic options. For details, see man AR below to list three:
A ----- after adding the new target file (*. O) to the static library file
B -----************************************** * Before
V ----- the command line format of the Ar command in verbose mode is as follows: Ar [-] {dmpqrtx} [abcfilnopssuvv] [membername] [count] archive files... eg: Ar-CRS hello. a hello. c
Dynamic library
1. Create a shared library
Gcc-C error. c
Gcc-C errorlog. c
Gcc-shared-O libapue. So error. O errorlog. o
In this way, a shared library is created!
2. Compile the Shared Library
Assume that the shared library is located in the current directory (that is, the same directory as the program file)
Gcc-O test-L.-lapue test. c
This will compile the executable file that does not contain the function code, but you will find that the Linux dynamic loader does not have the libapue. So file.
You can run the LDD command to view the shared libraries that the executable files depend on:
LDD Test
How can the dynamic loader discover library files? There are two solutions:
LD_LIBRARY_PATH environment variable
/Etc/lD. So. conf file
1. Environment Variables
Export LD_LIBRARY_PATH = "dir $ LD_LIBRARY_PATH"
2. Modify the/etc/lD. So. conf file. The file is located in/etc/lD. So. conf.
Generally, the library file of an application is not in the same directory as the system library file. Generally, the shared library file of the application is stored under/usr/local/lib, create a new directory named apue, and run libapue. so just copy it.
At the same time, add a line in/etc/lD. So. conf:
/Usr/local/lib/apue
Instance analysis:
Test. c
# Include
# Include "print. H"

Int main (void ){
Printf ("function: Main/N ");
Print ();
Printf ("out main/N ");

Return 0;
}

Print. c
# Include
# Include
Int print (void ){

Printf ("function: print/N ");
Printf ("Hello World/N ");
Printf ("out print/N ");
Return 0;
}
1: create a dynamic library:
Gcc-O libprint. So-FPIC-rdynamic-shared print. c
2: Create an executable file
Gcc-O test-I $ (inc_path)-L $ (lib_path)-lprint test. c
3: If the following error occurs
./Test: Error while loading shared libraries: libprint. So: cannot open shared object file: no such file or directory
Solution:
1: Export LD_LIBRARY_PATH = dir: $ LD_LIBRARY_PATH
2: Modify the/etc/lD. So. config file, add your library file directory to the last line, and then refresh the lD. So. cache file in ldconfig. You can run the strings command to check whether LD. So. cache is refreshed: strings/etc/lD. So. cache | grep print

4: LDD command to view the dynamic Connection Library

Linux-gate.so.1 = & gt; (0xb7f0e000)
Libprint. So => not found
Libc. so.6 =>/lib/tls/i686/cmov/libc. so.6 (0xb7d82000)
/Lib/ld-linux.so.2 (0xb7ef4000)
5: Nm view the symbols in the program
      
049f18 D _ dynamic
08049ff4 D _ global_offset_table _
080485cc R _ io_stdin_used
W_jv_registerclasses
08049f08 D _ ctor_end __
08049f04 D _ ctor_list __
08049f10 D _ dtor_end __
08049f0c D _ dtor_list __
080485ec R _ frame_end __
08049f14 D _ jcr_end __
08049f14 D _ jcr_list __
0804a018 A _ bss_start
0804a010 D _ data_start
08048580 T _ do_global_ctors_aux
08048450 T _ do_global_dtors_aux
0804a014 D _ dso_handle
W _ gmon_start __
0804857a T _ i686.get _ pc_thunk.bx
08049f04 D _ init_array_end
08049f04 D _ init_array_start
08048510 T _ libc_csu_fini
08048520 T _ libc_csu_init
U _ libc_start_main @ glibc_2.0
0804a018 A _ edata
0804a020 A _ end
080485ac T _ fini
080485c8 R _ fp_hw
0804839c T _ init
08048420 T _ start
0804a018 B completed.6625
0804a010 W data_start
0804a01c B dtor_idx.6627
080484b0 t frame_dummy
080484d4 t Main
U print
U puts @ glibc_2.0
6: Strip remove the symbols in the program
7: strings view the text information in the executable file

Reference: http://www.kuqin.com/linux/20081103/25444.html
Http://hi.baidu.com/li_zhongnan/blog/item/1d9bf3c2e13a9f32e4dd3b4f.html





This article is from the chinaunix blog. If you want to view the original text, click:Http://blog.chinaunix.net/u3/94960/showart_1973740.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.