C + + static libraries and dynamic libraries (introduction)

Source: Internet
Author: User

C + + static libraries and dynamic libraries

this share. Purpose Yes-let's learn to create and use static libraries, dynamic libraries, know the difference between static libraries and dynamic libraries, and know how to choose when you use them. Not in-depth introduction of static library, dynamic library of the underlying format, memory layout, etc., interested students, recommend a book "Programmer self-cultivation-link, load and library."

What is a library

A library is a well-written, existing, mature code that can be reused. in reality, every program relies on many underlying libraries, and it is not possible for everyone's code to start from scratch, so the existence of a library is extraordinary .

In essence, a library is a binary form of executable code that can be loaded into memory by the operating system. There are two types of libraries: static libraries (. A,. lib) and dynamic libraries (.so,.dll ).

The so-called static, dynamic refers to the link. Review the steps to compile a program into an executable program:

Static Library

The reason for being a "static library" is because in the link phase, the assembly-generated target file . O The link to the referenced library is packaged in the executable file. Therefore, the corresponding link method is called static link.

imagine that the static library is linked to the executable file with the assembly-generated target file, then the static library must follow The . o file format is similar. In fact, a static library can be simply seen as a set of target files (. O/.obj files) , That is, many of the target files are compressed and packaged to form a file. Static Library Features Summary:

L the static library link to the library is done at compile time.

L The program is no longer associated with the library at runtime and is easy to transplant.

L A waste of space and resources, as all related target files are linked to an executable file with the involved library of functions.

Dynamic Library

Through the above introduction to find the static library, easy to use and understand, but also to achieve the purpose of code reuse, then why do you need a dynamic library?

Why do I need a dynamic library?

Why the need of dynamic library, in fact, is also the characteristics of static library caused.

L space Waste is a problem of static library.

L Another problem is that static libraries can cause problems with updating, deploying, and publishing pages for programs. If the static library liba.lib is updated, the application that uses it needs to be recompiled, published to the user (a small change for the player, which causes the entire program to be re-downloaded, and the whole volume updated ).

A dynamic library is not connected to the target code when it is compiled, but is loaded only when the program is running. if different applications call the same library, then only one instance of the shared library is needed in memory , which avoids the problem of space wasting. Dynamic libraries are loaded only when the program is running, and also solve the problem of static libraries updating, deploying, and publishing pages. Users only need to update the dynamic library to update incrementally .

Dynamic Library Features Summary:

L The Dynamic Library puts the link loading of some library functions into a period of delay until the program runs.

L You can implement resource sharing between processes. (So dynamic libraries are also known as shared libraries)

L It's easy to upgrade some programs.

L It is even possible to actually do a link loading completely controlled by the programmer in the program code ( display Call ).

Window with Linux The execution file format is different, there are some differences when creating the dynamic library.

l   on windows pe format, dynamic library requires a dllmain function to initialize the portal, usually in the declaration of the exported function need to have _declspec (dllexport) keywords .

L The executable file compiled by the Linux gcc is in the ELF format by default and does not require the initialization of the portal , and does not require the function to make a special declaration, writing is more convenient.

Unlike creating a static library, you do not need a packaging tool ( ar,lib.exe ), you can create a dynamic library by using the compiler directly.

This article was selected from:http://www.cnblogs.com/skynet/p/3372855.html

C + + static libraries and dynamic libraries (introduction)

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.