Features of C + + dynamic library and static library and its compiling and using __c++

Source: Internet
Author: User
1 The concept of the library.

A library is a written, existing, mature, reusable code. In reality, every program relies on a lot of underlying libraries.
2 The concept of dynamic library and Static library.

Review the compilation process first:


2.1 Static Library

Static libraries in the link phase, the object files that are generated by the Assembly are packaged into the executable file with the referenced library, and the corresponding link is called a static link. Imagine that a static library is linked to an executable file with an assembly-generated target file (. o file), so the static library must be similar to the. o file format. In fact, a static library can

Simple as an archive collection of a set of target files (. o/.obj files), a file that is formed after many of the target files have been compressed and packaged. Static Library Features Summary:

1. The link of the static library to the function library is completed at compile time.
2, the program in operation and function of the library no longer involved, transplant convenience.
3, waste of space and resources, because all the relevant target files and the function library involved are linked to the synthesis of an executable file.


In addition to the waste of space and resources mentioned above, there is another problem with using static libraries: it can be troublesome to update, deploy, and publish pages of a program. If the static library libtest.a is updated, all applications that use it need to be recompiled and published to the user.


2.2 Dynamic Library

Using dynamic libraries is an effective solution to these problems of static libraries: Dynamic libraries are not connected to the target code when the program is compiled, but are not loaded until the program is run. If different applications call the same library, then only one instance of the shared library is needed in memory to avoid the problem of space wasting. Dynamic libraries are not loaded until the program is run, but they also solve the problem of the static library's updating, deploying, and publishing pages for the program. Users only need to update the dynamic library to update incrementally.


3 creation and use of dynamic libraries

We have written a test procedure for a string type interface, which contains the Mystring.cpp mystring.h mystring_test.cpp three source files, as well as the project makefile files3.1 Creating libstring.so Dynamic librariesThe "g++-shared mystring.cpp mystring.h-o libstring.so" compilation generated a dynamic library, but reported the following error:
"g++ mystring.cpp mystring.h-fpic-shared-o libstring.so" successfully compiled to build a dynamic library:
"NOTE: the pic parameter tells the compiler to produce a position-independent code," the resulting code, without an absolute address, uses all relative addresses, so the code can be loaded into memory anywhere in the position-independent, and can be executed correctly. This is exactly what the shared library requires, when the shared library is loaded, the location of the memory is not fixed. Shared objects may be loaded into different locations by different processes, if the instruction in the shared object uses an absolute address, an external module address, then the shared object must be loaded to adjust the address according to the loading location of the relevant module, that is, modify the address so that it can be accessed correctly in the corresponding process. The segments that are modified cannot implement multiple processes to share a single physical memory, and they must have a copy of the physical memory in each process. The FPIC directive is to allow multiple processes that use the same shared object to share as much physical memory as possible, pulling out of places that involve absolute addresses and external module addresses, ensuring that the content of the code snippet can be shared in multiple processes. 】3.2 Using dynamic libraries in test programsThe makefile of the project are as follows:
"Note: the-l parameter specifies the dynamic library path, the-l parameter dynamic link" uses "LDd mystring" to view the dynamic link of this executable file, and finds that libstring.so this dynamic library is not found because the link is linked to the linker (dynamic linker) found the dynamic library libstring.so, but the dynamic loader (dynamically loader, generally/lib/ld-linux.so.2) was not found.
The error occurred while running the executable mystring executable: Linux provides us with two solutions: 1. You can add the current dynamic library path to the/etc/ Ld.so.conf then run the Ldconfig, or run ldconfig with the current path as the parameter (with root permissions only). 2. Add the current path to the environment variable Ld_library_path here we use the Export command to import the current path directly into the environment variable Ld_library_path, and then run the program to run successfully
4 creation and use of static libraries 4.1 Creating a dynamic libraryCompile the build target file first, the following figure, where the MYSTRING.O is the target file

Create a static library of the target file archive (LIBMYSTRING.A)
4.2 using static library to modify engineering makefile:
Compared to the dynamic Link Compilation command, the static link compilation command is just a "-static" parameter, the Linux default link dynamic library, with the static parameter will link the static library 5 look at the difference between using a dynamic library link and a static link-generated program: Dynamic Link mode:
Static link mode:

Obviously, the executable file generated by the dynamic link method will consume less memory than the static link generated executable file.

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.