Linux uses static libraries and dynamic libraries

Source: Internet
Author: User
Tags mul

Linux uses static libraries and dynamic libraries (a) The concept of the library

Libraries are reusable code, and libraries are often used in large projects.
In essence , a library is a binary form of executable code that can be loaded into memory by the operating system.
Generally speaking, the library is said two kinds:
Static libraries: Linux under. A files, Windows. lib files
Dynamic library: Linux under. So files, Windows. dll files
Recently spent some time on Linux under the compilation, link and so on, as a rookie record and share a clam.

(ii) Static library and dynamic library

The program's compilation runs through the following steps:

1. source files (. h. cpp, etc.)
2. Pre-compilation
3. Compiling
4. Assembly (. o file)
5. Links
6. Running

In the above process, the Static library is at the link stage and will assemble the generated target file. O is packaged into an executable file with the referenced library , which is called a static link. A static library is actually a set of target files (. o/.obj), which is a file that is formed after many of the target files have been compressed and packaged, and there is a claim that the archive is the archive merged together. Static libraries are packaged together in the executable during the linking process, so the program is not associated with the library at run time, and it is convenient to migrate, but also because of this cause it wastes space resources because all the relevant target files and libraries involved are linked to an executable file. If the 10 100 executables use the same static library, it can be wasteful, and since static libraries need to be linked into the application, once the static libraries are modified, the entire program needs to be recompiled.

Unlike static libraries, dynamic libraries are not packaged into target code, but are loaded when the program is running. this solves the shortcoming of the static library wasted space, the dynamic library puts the link loading of some library functions into the time when the program runs, only one copy exists in the memory, which can realize the resource sharing between the processes.

The concept of static and dynamic libraries under Linux and Windows differs from the way you use them, and then you learn to share them slowly.

(iii) Use of static libraries under Linux

First you know a concept, the. h header file, the relationship of the static library. A file:
The. h header file is required at compile time, declaring the function interface, and compiling the function declaration by referencing the. h file.
. A files are suffixes of a static library file under Linux, usually in the form of libxxxx.a, which is essential when linking.

Write a simple arithmetic library below:
The first is the declaration section:

//staticmath.h#pragma onceclassstaticmath{ Public: Staticmath (void); ~staticmath (void);Static DoubleAddDoubleADoubleb);Static DoubleSubDoubleADoubleb);Static DoubleMulDoubleADoubleb);Static DoubleDivDoubleADoubleb);voidPrint ();};

The corresponding. cpp file is then written to implement the above interface declaration:

//StaticMath.cpp#include "StaticMath.h"double StaticMath::add(double a,double b){    return a+b;}

Since the static library is a collection of target files, we now need to precompile the above files with the compile-and - build steps to generate the corresponding. o files, with the g++ --help command to know that the parameters -c are described as:


So execute the command:

g++-cStaticMath.cpp

Compiling code files into a target file staticmath.o
Then, use the AR tool to package the target file into a static library file of. A, where ar can actually be understood as a shorthand for archive:

ar -crv libstaticmath.a StaticMath.o

This generates the static library file LIBSTATICMATH.A.

PS: In the previously done embedded projects, static library generation and use of the majority through some automation tools such as Autogen, CMake, etc., has not understood its true principle, this is also a study of a clam.

Finally write the test code using the static library

//test.cpp#include <iostream>#include "StaticMath.h"int main(){    double a=1.2;    double b=2.4;    std::cout"a+b="<<StaticMath::add(a,b)<<std::endl;    return0;}

Because in the process of linking the use of static libraries, where the compilation needs to be included in the static library directory and the name of the Static library as a compilation option, otherwise there will be problems:

g++-o test test.-L./-lstaticmath

The last executable file test performs normally:

(iv) Use of dynamic libraries under Linux

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.

The same first write the library file.
Statement:

//dynamicmath.h   #pragma once  class  dynamicmath{public : Dynamicmath (); ~dynamicmath (); static  double  Add (double  A, double  b); static  double  Sub (double  A, double  b); static  double  mul (double  A, double  b); static  double  div (double  A, double  b); 

Realize:

//DynamicMath.cpp#include "DynamicMath.h"double DynamicMath::add(double a,double b){    return a+b;}

Generate a dynamic library libxxxx.so

g++ DynamicMath.-fPIC-shared-o libdynamicmath.so

-fpic creates an address-independent compiler (pic,position independent code) to be able to be shared among multiple applications.
-shared Specifies that the dynamic link library is generated.

Using the dynamic library, test the code:

//test.cpp#include <iostream>#include "DynamicMath.h"int main(){    double a=1.2;    double b=2.4;    std::cout"a+b =" << DynamicMath::add(a,b)<<std::endl;    return0;}

Similar to the static library, compile the test file with the appropriate compilation options, generate the executable file, and ldd view the dynamic libraries that the application relies on with commands and their related information:

The use of the last dynamic library has one place to note:
A dynamic library is loaded at run time, and a dynamic linker/loader is required to know the absolute path of the dynamic library.
For the 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.
What do you mean? Is that there is a bunch of configuration files that can be configured to dynamically load the path of a dynamic library, and LD will look for a dependent dynamic library in these paths, so there are two workarounds:
1. Modify the configuration file (/etc/ld.so.conf) to add the directory of the dynamic library we created
2. Copy the dynamic library (. So) we created to the default dynamic library directory/lib or/usr/lib

On the 1th above, we can do this in the terminal

添加动态库目录 echo /home/zhangxiao/learnlib/dynamiclib/ >> /etc/ld.so.conf配置生效ldconfig

For the 2nd above, copy the generated dynamic library directly to the/usr/lib directory.

Reference:

Http://www.cnblogs.com/skynet/p/3372855.html
http://blog.csdn.net/yusiguyuan/article/details/12649737

Linux uses static libraries and dynamic libraries

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.