Comparison between Unix dynamic library and static library

Source: Internet
Author: User
Title: Unix dynamic library and static library comparison. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Differences between static and dynamic Databases

File Preview

The file directory tree is as follows, which is very simple as you can see.

1. libtest/2. | -- lt. c 3. | -- lt. h 4. '-- test. c # lt. c 1. 4. 5. # include 6. 7. void myprint (void) 8. {9. printf ("Linux library test! \ N "); 10 .} # lt. h 1. 4. 5. void myprint (void); # test. c 1. 4. 5. # include "lt. h "6. 7. int main (void) 8. {9. myprint (); 10. return 0; 11 .}

First look at the static library

First, make the static library liblt..

1. $ gcc-o lt. o-c lt. c 2. $ ar cqs liblt. a lt. o, Link, 1. $ gcc test. o liblt. a-o test is used to check the referenced database.

1. $ ldd test 2. linux-gate.so.1 => (0xffffe000) 3. libc. so.6 =>/lib/libc. so.6 (0xb7e29000) 4./lib/ld-linux.so.2 (0xb7f6e000)

Dynamic library

Liblt. so is a dynamic library.

1. $ gcc-o lt. o-c lt. c 2. $ gcc-shared-Wall-fPIC-o liblt. so lt. o-shared this option specifies to generate a dynamic Connection Library (let the connector generate a T-type export symbol table, sometimes also generate weak connection W type export symbols ), external programs cannot connect without this flag. Equivalent to an executable file-fPIC: code that is compiled into a separate location, without this option, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, rather than truly sharing code segments.

-L.: indicates that the library to be connected is in the current directory-ltest: the compiler has implicit naming rules when searching for the dynamic connected library, that is, adding lib before the given name, followed. so to determine the library name LD_LIBRARY_PATH: This environment variable indicates that the dynamic connector can load the path of the dynamic library.

Link Method I. Copy it to the system repository and then link it to gcc. 1. $ sudo cp liblt. so/usr/lib 2. $ gcc-o test. o-llt here we can see the-llt option,-l [lib_name] specifies the library name, he will actively search lib [lib_name]. so. The search path can be searched through gcc -- print-search-dirs.

Link Method II: manually specify the library path 1. $ cc-o test. o-llt-B/path/to/lib the-B option here adds/path/to/lib to the gcc search path. In this way, the link is okay, but the program manually linked in method II still needs to specify the library path during execution (the link and execution are separated ). System to be added

Unified variable LD_LIBRARY_PATH:

1. $ export LD_LIBRARY_PATH =/path/to/lib

At this time, check the database connection status of the test Program (method I)

1. $ ldd test 2. linux-gate.so.1 => (0xffffe000) 3. liblt. so =>/usr/lib/liblt. so (0xb7f58000) 4. libc. so.6 =>/lib/libc. so.6 (0xb7e28000) 5. /lib/ld-linux.so.2 (0xb7f6f000) en, is it a liblt more than the static link program. so? Well, this is the biggest difference between static and dynamic. In static scenarios, he loads the library directly into the program, while in dynamic connections, he only keeps the interface, separate the dynamic library from the program code. In this way, the code reusability can be improved and the coupling degree of the program can be reduced.

In addition, when running, ensure that the main program can find the dynamic library, so the dynamic library is generally released to the system directory, or it is in a relatively fixed path with the main program, in this way, the dynamic library can be found no matter when and where the main program runs on the local machine. When the static library only acts on the link and runs the main program, the static library file does not make sense.

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/iejwduan/archive/2009/03/26/4027808.aspx

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.