C language static link library and dynamic link library

Source: Internet
Author: User
Tags function prototype

1: Static link library

A static link library is a relatively early occurrence. The static library is actually the commercial company will own the function library source code through compiles not to connect to form. O's target file, and then use the AR tool to archive the. o File as an archive file for. A (an archive of. A is also called a static link library file). Commercial companies provide static libraries to customers by publishing. A library files and. h header files. After the customer gets the. A and. h files, they learn the prototype of the library function in the library through the. h header file, and then call these library files directly in their. c files, and the linker will go to the. A file to take out the compiled. O binary of the called function. The code snippet is linked in to form the final executable program.


2: Dynamic Link library

dynamic Link cubby The static link library appears later, more efficient, and is an improved type. Now we are generally using dynamic libraries. The static library links the code snippet of the function in the called Library into the final executable program when the user links to its own executable program, so the benefits are enforceable and the downside is too much of a place. In particular, when more than one application uses this library function, there is actually a piece of code for the library function in the executable program that is finally generated by multiple applications. When these applications run in memory at the same time, there is actually more than one code snippet for this library function in memory, which is completely duplicated. The dynamic link library itself does not connect the code snippet chain of the library function to the executable program, but only makes a mark. Then, when the application executes in memory, the runtime environment discovers that it invokes a library function in a dynamic library, loads the dynamic library into memory , and then, regardless of how many applications call the library, the function jumps to the place where it was first loaded (not repeated)


3: Comparison of static link libraries and dynamic link libraries

A static library is a code snippet that a user has linked to a library function that has been called to in a link to a byte's executable program, and the benefit is that such executables can be executed anywhere, and the downside is that the library functions take up a lot of space, making the final executable program of the link get very large.

When using a dynamic library, the function itself compiles links to the executable program, and does not link the library function to the executable program, but instead marks which library functions are used by the function, and the operating system automatically loads the libraries into memory when the executable program runs.

Comparison:

As you can see from the above analysis, the static library is linked to the final executable program, while the dynamic library does not link the library function to the executable program. from the point of view of a single executable program , the system will load the library functions into memory when running in the middle of the memory, so that the two executables ultimately occupy the same amount of space in memory when they run. However, from a number of programs, it is different, such as a program and B programs are used in the printf function, the use of static libraries, both executable program contains the printf function, so this time in memory contains two printf functions, while using the dynamic library, The system will only load one copy of the printf function, and when other functions are also used in the printf function, as long as it is called to the address of the loaded printf function, it does not need to be loaded again, so when multiple programs run, the static library is significantly more memory than the dynamic library.


4: Use of library functions in library functions

(1) The compiler link program in GCC uses dynamic libraries by default, and static links need to be explicitly forced to use-static to force static links.

(2) The use of library functions need to pay attention to 3 points: first, including the corresponding header file; Second, the function prototype should be paid attention to when the library function is called; Thirdly, some library functions need to use-LXXX to specify the link, and the third, if it is a dynamic library, note-l specifies the address of the dynamic library.





C language static link library and dynamic link library

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.