Creation and use of static and dynamic libraries in linux

Source: Internet
Author: User
This article describes how to create and use static and dynamic libraries in linux. For more information, see

Creation and use of static and dynamic databases

1. Basic concepts

Static library: During the link, the library is packaged into an executable file together with the. o file generated by the compilation. In linux/windows, the extensions are. a/. lib.
Dynamic library: the dynamic library is not packaged into an executable file when the link is linked, and the dynamic library is dynamically loaded during execution. In linux/windows, the extensions are. so/. dll.

The main differences are as follows:

A. maintainability.
Merge updates. To update a dynamic library, replace the library. However, static integration requires a new link to generate a new executable program for full update.
Environment dependency. If the static library is included, it is not dependent on the environment after compilation. A dynamic library is dependent and must have a corresponding dynamic library in the environment.
B. size
Space utilization. If the database depends on the same static database, multiple copies exist.
The size of the executable file. The static library file is large.
C. a dynamic library can share resources between processes. it is also called a shared library.

2. create and use static databases

A static library is essentially a group of. o compressed files with the extension. a/. lib.

Copy codeThe code is as follows:
Ar-crv libXXX. a mmmm. o nnnn. o

XXX indicates the database name and mmmm. o nnnn. o indicates the target file to be packaged. Crv parameters, meaning:

C: do not warn if the library had to be created
R: replace existing or insert new file (s) into the archive
V: be verbose

During use, you only need to introduce the header file in the code. when linking, specify the search path (-L option) and static library name (without the lib prefix and. suffix a,-l option ).


Copy codeThe code is as follows:
G ++ test. cpp-L ../StaticLibrary-lXXX

3. dynamic library

The Window format is different from that of the Linux execution file. there are some differences when creating a dynamic library.

In Windows, the execution file is in PE format. the dynamic library requires a DllMain function to initialize the entry. the _ declspec (dllexport) keyword is usually required when exporting the function declaration.
In Linux, the gcc-compiled execution file is in ELF format by default, and does not require initialization entry or special declaration by functions. it is easier to compile.
The name format of the dynamic library in linux is the same as that of the static library in libXXX. so format.

You can create a dynamic library by using the compiler without using a dedicated packaging tool like a static library.

To create a dynamic library, add the-fPIC option during source code compilation and The-shared option when linking.

Copy codeThe code is as follows:
G ++-fPIC-shared-o libXXX. so xxx. cpp

Use the same static library,

Copy codeThe code is as follows:
G ++ test. cpp-L ../DynamicLibrary-lXXX

You only need to set the search path. Default search path:/lib/,/usr/lib.

Set the search path:

Edit/etc/ld. so. conf and set the search path.
Run ldconfig to recreate the/etc/ld. so. cache file. The setting takes effect.
4. related tools
Ldd view the shared library (dynamic library) on which the program depends)

Copy codeThe code is as follows:
Gcc/g ++ parameter-I (uppercase 'I'),-L (uppercase 'L'),-l (lowercase 'L ')

1.-I (uppercase 'I') header file directory
2.-L (uppercase 'L') Library Directory
3.-l (lower case 'L') database name
 
The upper case is the directory and the lower case is the database.

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.