Static Library and Dynamic library

Source: Internet
Author: User

Related file suffixes (for example, Linux system) in C + + programming:

. A: Static library (archive)

. c/.cpp:c/c++ source Program

. h/.hpp:c/c++ header file for source program

. I: Pre-processed C/C + + source program

. O: Object file

. S: Assembly language Code

. So: Dynamic link library

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 (the Linux operating system is suffixed with. A, and a. lib suffix under the Windows operating system) and a dynamic library (the Linux operating system is suffixed with. So, and a. dll is suffixed under the Windows operating system).

A static library is a compiler-generated. O Collection of object files, static libraries another name is the archive file (archive), the tool that manages this archive is called AR.

Common compile commands (command-line compile C source program using GCC, compile C + + source program using g++, here to g++ compile C + + source program for example):

Assuming that the source program you need to compile has hello.cpp and world.cpp, then

1. $g + +-wall Hello.cpp This is the simplest compile command,-wall is the abbreviation for warn all, which is all the warning messages displayed during the compilation process. The compiler compiles the source code file to generate the object file. O, the linked object file gets the executable file, and the object file is deleted. Because the executable program's file name is not specified here, the compiler defaults output a.out

2. $g + +-wall Hello.cpp-o Hello here use-o to specify the name of the output executable file,-O is for outputs.

3. $g + +-c-wall hello.cpp-o Hello option-c means compile, which instructs the compiler to only complete the compilation process to generate the object file, without performing the linking process.

4. $g + +-c-wall hello.cpp world.cpp This command can generate multiple object files at once

5. $g + +-c-wall hello.cpp world.cpp-o Hello this command compiles two source files into an object file and links it as an executable hello

6. $g +-e hello.cpp-o hello.i option-e This is the compiler only precompiled processing

7. $g + S Hello.cpp-o hello.s This command generates assembly code

Create and use a static library in a Linux environment:

To create a static library, first compile the object files that are needed in the library:

1. $g + +-c-wall hello.cpp world.cpp

Command ar mate parameter-crv You can create a new library and insert a previously created object file. If the library does not exist, the parameter-R creates a new library and adds the object module to the archive file. The following command creates a static library named LIBHELLOWORLD.A that contains both of the above object files:

2. $ar-CRV libhelloworld.a hello.o world.o a larger project would write makefile files to generate a static library.

Linux Static library naming specification, must be lib[your_library_name].a, where Lib is the prefix, the middle is a static library name, the extension is. A

3. Using static libraries under Linux, simply specify the search path (-l option) for the static library at compile time, specify the static library name (without the Lib prefix and. A suffix,-l option), assuming that the LIBHELLOWORLD.A Static library is placed in the. /statilibrary directory, the following:

$g + + teststaticlibrary.cpp-l. /STATICLIBRARY-L HelloWorld Compilation Build executable file

Create and use a dynamic library in a Linux environment:

1. Why use a dynamic library? because: 1. Using a static library can result in wasted space. 2. The static library is troublesome to update, deploy and release the program, and if the static library libhelloworld.a is updated, all applications that use it need to be recompiled and published to the user.

Dynamic libraries are not linked to the target file at compile time, but are loaded when the program is run. 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.

2. The dynamic link library is named lib[your_library_name].so, the prefix is lib, and the suffix is. So. In the file system, Your_Library_Name is only a link to the actual dynamic library. For dynamic libraries, each library actually has another name for the compiler, which is a link file to the library image file lib[your_library_name].so

3. In the process of creating a dynamic library, the target file is generated first, and the compiler option is added-fpic

$g + +-fpic-c Hello.cpp World.cpp-fpic creates an address-independent compiler (pic:position independent code) to be able to be shared among multiple applications.

4. Generate the dynamic library, add linker option at this time-shared

$g + +-shared-o libhelloworld.so hello.o world.o

5. Refer to a dynamic library to compile into a workable file (as in a static library file):
$g + + teststaticlibrary.cpp-l. /staticlibrary-l HelloWorld

Found an error!!! So how do you locate a shared library file at the time of execution?

1) When the system loads executable code, it can know the name of the library it depends on, but it also needs to know the absolute path. In this case, the system dynamic loader (linker/loader)is required.

2)        for elf format executable program, is done by ld-linux.so* , it has searched elf file dt_ RPATH Segment-environment variable ld_library_path-/etc/ld.so.cache file List-/ Lib/,/usr/lib The catalog finds the library file and loads it into memory.

How to enable the system to find it:

L If installed under /lib or /usr/lib , then the ld Can be found by default, no additional action is required.

L If you are installing in a different directory, you need to add it to the /etc/ld.so.cache file by following these steps:

N to edit the /etc/ld.so.conf file, add the path to the directory where the library file resides

N Run ldconfig and the command rebuilds the /etc/ld.so.cache file

We'll copy the created dynamic library to /usr/lib below, and then run the test program.

Static Library and Dynamic 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.