Linux static link library and dynamic link library detailed

Source: Internet
Author: User

Ext.: http://bbs.chinaunix.net/thread-1281954-1-1.html

Features and advantages of two dynamic link libraries
First let's take a look at the benefits of deferring library functions to the time of program Runtime:
1. Resource sharing between processes can be achieved.
What's the concept? That is, when a program is running to invoke a dynamic link library function, the operating system will first look at all running programs to see if there is a copy of this library function in memory. If so, let it share that copy; only the link is loaded. This mode, while bringing some "dynamic link" additional overhead, greatly saves the system's memory resources. C's standard library is the dynamic link library, which means that all running programs in the system share the same C standard library code snippet.
2. It is easy to upgrade some programs. Users only need to upgrade the dynamic link library, without recompiling the link to other legacy code to complete the entire program upgrade. Windows is a good example.
3. It is even possible to actually sit into the link loader completely controlled by the programmer in the program code.
When programmers write programs, they can clearly indicate when or under what circumstances the link loads into which dynamic link library function. You can have a fairly large piece of software, but each time it runs, only a small number of programs are loaded into memory due to different operational requirements. All functions are based on the principle of "need to be transferred in", thus greatly saving system resources. Today's software, for example, typically opens several different types of files, which are often implemented using dynamic-link libraries. In one run, only one type of file will normally be opened. So until the program knows the type of the file and then loads the corresponding read-write function, instead of loading all the read and write functions from the beginning, it is not known that they are used in the whole program at all.

Third, the creation of static dynamic link library
Because of the shared nature of the dynamic link library functions, they are not copied into the executable file. At compile time, the compiler will only do some checks such as function names. When the program is running, the called Dynamic link library function is placed somewhere in the memory, and all the programs that call it will point to this code snippet. Therefore, the code must be practical relative to the address, not the absolute address. At compile time, we need to tell the compiler that these object files are used to do the dynamic link library, so the address is not irrelevant code (Position Independent Codes (PIC)).
The source code is as follows:
[Email protected]:~/c_pram/practice$ cat FUN.C
#include <stdio.h>
void Mylib1 ()
{
printf ("Library1 routine called\n");
}
void Mylib2 ()
{
printf ("Library2 routine called\n");
}
[Email protected]:~/c_pram/practice$ cat CALL.C
Main ()
{
MYLIB1 ();
Mylib2 ();
return 0;
}
Static link library creation:
[Email protected]:~/c_pram/practice$ gcc-c FUN.C
Zj[email protected]:~/c_pram/practice$ ar cqs libfun.a fun.o
[Email protected]:~/c_pram/practice$ gcc call.c-static-l.-lfun-o fun_static_call
[Email protected]:~/c_pram/practice$./fun_static_call
Library1 routine called
Library2 routine called

Dynamic Link library creation:
/* Generate a dynamic-link library */
[Email protected]:~/c_pram/practice$ gcc fun.c-fpic-shared-o libfun.so
/*-L specifies the path to find the dynamic link library,-lfun is actually looking for libfun.so*/
[Email protected]:~/c_pram/practice$ gcc call.c-l.-lfun-o fun_dyn_call
/* No environment variable specified ld_library_path*/
[Email protected]:~/c_pram/practice$./fun_dyn_call
./fun_dyn_call:error while loading shared libraries:libfun.so:cannot open Shared object file:no such file or directory
[Email protected]:~/c_pram/practice$ export ld_library_path= $LD _library_path:~/c_pram/practice
[Email protected]:~/c_pram/practice$./fun_dyn_call
Library1 routine called
Library2 routine called



Static, which means to create a static link library, shared tag tells the compiler that this is to establish a dynamic link library. This is very different from the establishment of a static link library, which uses the AR command. Also note that the static link library has the name "libxxx.a" suffix named ". A", and the dynamic link library has the name "libxxx.so" suffix named ". So".


With dynamic-link libraries, you first need to have the compiler check for some syntax and definitions during compilation.
This is essentially the same as the utility of a static library, with-lpath and-lxxx tags. Such as:
GCC call.c-l.-lfun-o fun_dyn_call

The compiler searches for the libxxx.so file under the path folder and, if not found, continues to search for libxxx.a (Static library).

Iv. setting of Ld_library_path environment variables
You also need to tell the system where to find your dynamic link library files while the program is running. Under UNIX, this is done by defining an environment variable named Ld_library_path. You simply assign the path to this variable.
There are three ways to successfully find a dynamic library in order to execute the program:
(1) Copy the library to the/usr/lib and/lib directories.
(2) Add the path of the library to the LD_LIBRARY_PATH environment variable. For example, dynamic library libhello.so in the/home/ting/lib directory, in bash, for example, use the command:
$export ld_library_path= $LD _library_path:~/c_pram/practice
Add ~/c_pram/practice after the environment variable Ld_library_path
(3) Modify the/etc/ld.so.conf file, add the path where the library is located to the end of the file, and perform sudo ldconfig refresh (requires Superuser privileges). In this way, all library files in the joined directory are visible.
Under Ubuntu:
[Email protected]:~/c_pram/practice$ cat/etc/ld.so.conf
Include/etc/ld.so.conf.d/*.conf
[Email protected]:~/c_pram/practice$ ls/etc/ld.so.conf.d/
I486-linux-gnu.conf libc.conf
Original
[Email protected]:~/c_pram/practice$ cat/etc/ld.so.conf.d/libc.conf
# libc Default configuration
/usr/local/lib
After
[Email protected]:~/c_pram/practice$ cat/etc/ld.so.conf.d/libc.conf
# libc Default configuration
/usr/local/lib
/home/zj/c_pram/practice
Of course, since ld.so.conf contains/etc/ld.so.conf.d/*.conf, you can create a new file vi/etc/ld.so.conf.d/myownlib.conf yourself and enter/home/zj/c_pram/in it. Practice. This method can be implemented after the reboot should also be able, I have not reboot after restarting to tell you, or you can tell me.

Five. View symbols in the library
After everything is arranged, use the LDD command to see which libraries the executable depends on, and for dynamic
[Email protected]:~/c_pram/practice$ ldd Fun_static_call
Not a dynamic executable
[Email protected]:~/c_pram/practice$ ldd Fun_dyn_call
Linux-gate.so.1 = (0xb7f06000)
libfun.so =/home/zj/c_pram/practice/libfun.so (0xb7f02000)
libc.so.6 =/lib/tls/i686/cmov/libc.so.6 (0xb7da1000)
/lib/ld-linux.so.2 (0xb7f07000)

Linux static link library and dynamic link library detailed

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.