linux-static link libraries and dynamic link libraries

Source: Internet
Author: User

The "preface" is explained in the blog post:


This article will introduce the static link library and dynamic link library knowledge through the personal tone, in the current time point "June 14, 2017 " under the grasp of the technical level is limited, there may be a lot of knowledge understanding is not deep enough or comprehensive, I hope you point out the problem of common exchange, In the follow-up work and study, if found that the content of this article and the actual situation is biased, will improve the content of this blog post.


This article refers to a reference link:

1, http://developer.51cto.com/art/201107/275783.htm "Dynamic link Library"
2, Http://www.jianshu.com/p/8743a0edb1ee "Good article, explain the dynamic link library"
3, http://c.biancheng.net/cpp/html/2750.html "Good article, explain the dynamic link library"
4, http://www.cnblogs.com/ardar/articles/357321.html "Good article, recommended"
5, http://www.cnblogs.com/catch/p/3857964.html "dynamic link implementation principle under Linux"


Body:


Write in front: the static/dynamic link library is the application of library functions, that is to say they are library functions.

Typically, the link to the library is done at compile time (compile).

In the case of a static library, when compiling a linked executable, the linker combines all the relevant object files with the library of functions involved and other modules of the application to create the final executable file (. EXE file). At run time, the program has nothing to do with the library, because all the required functions have been copied in. So these libraries become static libraries (static Libaray), in other words, the code for functions and procedures is in the program's executable file (EXE), which contains all the code required for the runtime, and when multiple programs call the same function, multiple copies of the function are present in memory. In Linux, usually the file name is "Libxxx.a", and Windows typically has a. LIB extension.

When you publish a product, you only need to publish the executable file, and you do not need to publish the static library that is used. Therefore, if you have multiple programs that call these libraries, there will be multiple copies in memory.



Therefore the reality is that many applications are not a complete executable, and they are split into relatively independent, dynamic-link libraries, DLL files, placed in the system. When the program is actually running, the corresponding DLL file will be called. An application can have multiple DLL files, and a DLL file may be shared by several applications, such that the DLL file is called a shared DLL file.

note: DLL is also a compiled binary program that can be called by other programs, but unlike EXE, dll cannot run independently, The memory must be loaded by another program call. DLL encapsulates a number of functions, so long as you know the entry address of the function, it can be called by other programs.

PS: By using DLLs, programs can be modular, and programs can consist of relatively independent module components. Because the modules are independent of each other, the program does not load all the modules at startup, because the program loads faster, and the module is loaded only when the corresponding function is requested.

Example:
For example, in the Windows operating system, the COMDLG32 DLL performs common functions related to dialog boxes. Therefore, each program can implement the Open dialog box by using the functionality contained in the call DLL. This helps facilitate code reuse and efficient use of memory.


One, "Static link library"


1, the name of the class library is generally libxxx.a
2, the code loading speed, execution speed is also relatively fast, because it will be compiled only the part you need to link in.
3, the application is relatively large, if more than one application to use, will be loaded multiple times, wasting memory.
4, if the static function library changes, then your program must be recompiled.
5, if you have more than one application on the system to use the library, it is recommended to compile it into a dynamic library, so that although the start of the load is relatively slow, but multi-task time will be compared to save memory, if you have only one to two applications on the system using the library, and the use of fewer APIs, compile into a static library General static libraries can also be clipped and compiled, so the application may be larger, but the speed of the startup will be greatly increased.


Second, "Dynamic link library"


1, the name of the class library is generally libxxx.so
2, sharing: Multiple applications can use the same dynamic library, when launching multiple applications, only need to load the dynamic library into memory once;
3, Development module good: The requirements of the design of the functional division of the better.

4, the dynamic function library changes do not affect your program, so the dynamic function library upgrade is more convenient.


Iii. "The difference between a static link library and a dynamic link library":


1, simply said, the static library and the application compiled together, in any case can run, and the dynamic library is a dynamic link, as the name implies when the application is started to link, so, when the user's system does not have the dynamic library, the application will run failure.

2. If the program is loading library files at compile time, the static library is used. If the target code is loaded at run time, it becomes a dynamic library. In other words, if you are using a static library, the static library code is copied to the program's code snippet at compile time, and the volume of the program expands. If you use a dynamic library, the program only retains the name of the library file and the function name, at run time to find the library file and function body, the volume of the program basically changes little.

3.

The principle of static library is "take space to change time", increase program volume, reduce running time;

The dynamic library is "time-changing space", which increases the running time, but reduces the volume of the program itself.

4, "case demonstration, generate dynamic link library file":


The file directory tree is as follows:


libtest/
|--MYJOB.C
|--Myjob.h
|--test.c

"Static Library section"

A. Make A Static library LIBMYJOB.A

$ gcc-c Myjob.c-o MYJOB.O
$ ar-c-r-s LIBMYJOB.A MYJOB.O

B. Links

$ gcc TEST.O libmyjob.a-o test

C. Referencing the library (no information required)

$ LDD Test
Linux-gate.so.1 = (0xffffe000)
libc.so.6 =/lib/libc.so.6 (0xb7e29000)
/lib/ld-linux.so.2 (0xb7f6e000)

"Dynamic Library section"

A. Make A Dynamic library libmyjob.so

$ gcc-wall–fpic-c Myjob.c-o MYJOB.O

$ gcc-shared-o libmyjob.so MYJOB.O


-shared: This option specifies to generate a dynamic connection library (let the connector generate an export symbol table of T > type, and sometimes a weak connection W > type of export symbol) without which the external program cannot connect. Equivalent to an executable file.
-fpic: Code compiled to a location-independent, without this option, the compiled code is location-dependent, so the dynamic >> state load is through the way of code copy to meet the needs of different processes, but not to achieve the purpose of real code segment sharing >.
-L.: Indicates that the library to be connected is in the current directory.
Ld_library_path: This environment variable indicates that the dynamic connector can load the path of the dynamic library.

B. Links
Link method One, copy to the system library and then link, let GCC itself find:

$ CP Libmyjob.so/usr/lib
$ gcc-o Test Test.o-lmyjob
Here we can see the-lmyjob option,-l[lib_name] Specify the library name, he will be active search. Lib[lib_name].so this search path can be found through the GCC--print-search-dirs.

Link method Two, specify the library path manually

$ gcc-o Test Test.o-lmyjob-b/path/to/lib
The-B option adds/path/to/lib to the path of the GCC search. There is no problem with this link, but the manually linked program in method II still needs to specify the library path at execution time (the link and execution are separate). You need to add the system variable Ld_library_path:

1. $ Export Ld_library_path=/path/to/lib
Check this out again. Test Program Library Link status (method I case)
1. $ LDD Test
2. Linux-gate.so.1 = (0xffffe000)
3. libmyjob.so =/usr/lib/libmyjob SO (0xb7f58000)
4. libc.so.6 =/lib/libc.so.6 (0xb7e28000)
5./lib/ld-linux.so.2 (0xb7f6f000)
6.
Is it a libmyjob.so more than a statically linked program? This is the biggest difference between static and dynamic.

Statically, it loads the library directly into the program, while dynamically linking it only preserves the interface, keeping the dynamic library separate from the program code. This can improve the reusability of the code and reduce the coupling degree of the program.
In addition, the runtime, to ensure that the main program can find dynamic libraries, so the dynamic library is generally published to the system directory, or in the main program is relatively fixed path, so no matter the main program in the local when and where to run, can find the dynamic library. While the static library is only used for linking, the static library file does not have any meaning after running the main program.





End:



Thank you for reading, I wish you a rewarding day, thank you!





This article is from the "Breeze Month Blog" blog, please be sure to keep this source http://watchmen.blog.51cto.com/6091957/1936668

linux-static link libraries and dynamic link libraries

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.