Linux static link library and dynamic Connection library

Source: Internet
Author: User
Tags mul naming convention save file

(0) Folder

Installing Ubuntu's blood-vomiting experience under VMware

0 Basic Learning Shell programming

The magical magic of makefile under Linux

Linux debugging artifact--GDB

10 minutes Learn the basic types of Python

Linux static link library and dynamic Connection library


One: The application of static link library three steps away ~ ~ ~


# #g + C StaticMath.cpp

# #ar-CRV libstaticmath.a STATICMATH.O

# #g + o run test_a.cpp-l.-lstaticmath

#[@sjs_37_33 lib_a_so]#./run
A + b = 12
A-B = 8
A * b = 20
A/b = 5
**********************
**********************
Sh:pause:command not found
**********************

Two: Go the Detour ~ ~ ~

#[@sjs_37_33 lib_a_so]# g++-o run test_a.cpp//missing dependent library file
/tmp/ccx8rzph.o:in function ' main ':
Test_a.cpp: (. text+0x39): Undefined reference to ' Staticmath::add (double, double) '
Test_a.cpp: (. text+0x76): Undefined reference to ' staticmath::sub (double, double) '
Test_a.cpp: (. text+0xb3): Undefined reference to ' Staticmath::mul (double, double) '
Test_a.cpp: (. text+0xf0): Undefined reference to ' Staticmath::d IV (double, double) '
Test_a.cpp: (. text+0x12a): Undefined reference to ' Staticmath::staticmath () '
Test_a.cpp: (. text+0x136): Undefined reference to ' Staticmath::p rint () '
Test_a.cpp: (. text+0x151): Undefined reference to ' Staticmath::~staticmath () '
Test_a.cpp: (. text+0x16d): Undefined reference to ' Staticmath::~staticmath () '
Collect2:ld μ?? 1

#[@sjs_37_33 lib_a_so]# g++-o run test_a.cpp-l.-lstaticmath//Missing dependent libraries? Not also. The missing implementation function is. CPP does not implement the. h file
/tmp/cczojb8v.o:in function ' main ':
Test_a.cpp: (. text+0x12a): Undefined reference to ' Staticmath::staticmath () '
Test_a.cpp: (. text+0x151): Undefined reference to ' Staticmath::~staticmath () '
Test_a.cpp: (. text+0x16d): Undefined reference to ' Staticmath::~staticmath () '
Collect2:ld μ?

? 1

Three: Application of dynamic Library two-step walk ~~

# # # g++-fpic-shared-o libdynmath.so StaticMath.cpp----Generate-o libdynmath.so dynamic Library


Referencing a dynamic library compiles to a executable file (as with a static library):

# # #g + testdynamiclibrary.cpp-l. /dynamiclibrary-ldynmath
Then execute:./a.out, found unexpectedly error!

。!



#[@sjs_37_33 lib_a_so]#./a.out

./a.out:error while loading shared libraries:libdynmath.so:cannot open Shared object file:no such file or directory

So how do you locate a shared library file when you run it?
1) When the system loads the code that can run, you can know the name of the library on which it depends. But you still need to know the absolute path.

In this case, the system dynamic Loader (Linker/loader) is required.
2) for the ELF format of the program can be run. It was finished by ld-linux.so*. It searched the elf file dt_rpath Segment-environment variable Ld_library_path-/etc/ld.so.cache file List-/lib/,/usr/lib folder to locate the library file and load it into memory.
How to get the system to find it:

If installed under/lib or/usr/lib, the LD can be found by default. No additional action is required, assuming installation in a different folder. Need to add it to the/etc/ld.so.cache file. Procedures such as the following:

Edit the/etc/ld.so.conf file to add the path to the folder where the library file resides. Execute ldconfig. The command rebuilds the/etc/ld.so.cache file, and we copy the created dynamic library to the/usr/lib below, then execute the test program and succeed.


Four: Linux lower library related commands

# #ldd Libdynmath.so
Linux-vdso.so.1 = (0x00007fffed837000)
libstdc++.so.6 =/usr/lib64/libstdc++.so.6 (0x00007f48b0413000)
libm.so.6 =/lib64/libm.so.6 (0x00007f48b018e000)
Libgcc_s.so.1 =/lib64/libgcc_s.so.1 (0x00007f48aff78000)
libc.so.6 =/lib64/libc.so.6 (0x00007f48afbe8000)

/lib64/ld-linux-x86-64.so.2 (0x0000003da7800000)

g++ (GCC) compilation options
-shared: Specifies that the dynamic link library is generated.
-static: Specifies that a static link library is generated.


-fpic: represents compiled as location-independent code for compiling shared libraries. The target files need to be created as location-independent codes, so that they can be placed in a running program's memory wherever they are loaded.
-L.: Indicates the folder where the library you want to connect is located.
-L: Specifies the dynamic library that is required for the link.

The compiler has an implicit naming convention when looking for a dynamic connection library, which is to precede the given name with Lib. Add. a/.so to determine the name of the library.
-wall: Generates all warning messages.
-GGDB: This option will generate as much debugging information as GDB can use.
-G: The compiler generates debug information at compile time.


-C: Only activates preprocessing, compilation, and compilation, that is, the program is made into a target file (. o file).

-wl,options: Pass the number of parameters (options) to the linker ld.

Assuming that the options have commas in the middle, the options are divided into multiple choices and then passed to the linked program.

NM command
Sometimes you might need to see what functions are in a library, and the NM command prints out all the symbols involved in the library.

A library can be both static and dynamic.

The number of symbols listed in NM is very many, and there are three common types:
One is called in the library. However, it is not defined in the library (indicating that additional library support is required). expressed in U;
One is the function defined in the library, denoted by T, which is the most common;
One is the so-called "weak" notation, which, although defined in the library, may be overwritten by a symbol of the same name in other libraries. expressed in W.


$NM libhello.h

LDD command

The LDD command is able to view a shared library that can be run by a program, such as the arithmetic dynamic library we write relies on the following libraries:


Open file, save, close file (used in VI command mode)

VI filename//open filename File
: w//Save File
: w vpser.net//Save to Vpser.net file
: Q//exit editor, assuming the file has changed please use the following command
: q! Exits the editor without saving
: Wq//Exit editor, and save file


V: Related code

H file

#pragma onceclass staticmath{public:    staticmath (void);    ~staticmath (void);     Static double Add (double A, double b);//Add    static double sub (double A, double b);//subtract    static double Mul (double A, Double b);//multiply    static double div (double A, double b);//Division     void Print ();};

CPP file

#include "StaticMath.h" #include <stdio.h> staticmath::staticmath (void) {    //    print ();} Staticmath::~staticmath (void) {    //    print ();} Double Staticmath::add (double A, double b)//addition {    return a + b;} Double Staticmath::sub (double A, double b)//subtraction {return a-a    ;}       Double Staticmath::mul (double A, double b)//multiplication {    return a * b;} Double Staticmath::d IV (Double A, double b)//Division {    if (0==b)         return-1;    return a/b;} void Staticmath::p rint () {    printf ("**********************\n");}

Test file

#include "StaticMath.h"//test static library libstaticmath.a and dynamic library Libdynmath.so#include <iostream> #include <stdlib.h> using namespace Std; int main (int argc, char* argv[]) {    double A = ten;    Double b = 2;     cout << "A + b =" << Staticmath::add (A, b) << Endl;    cout << "A-B =" << staticmath::sub (A, c) << Endl;    cout << "A * b =" << Staticmath::mul (A, b) << Endl;    cout << "A/b =" << Staticmath::d IV (A, b) << Endl;     Staticmath SM;    Sm.print ();     System ("pause");    return 0;}

VI: summary

Static-link libraries and dynamic-link libraries are all ways to share code, assuming a static-link library. If you wish,the instructions inLib are all directly included in the EXE file that is finally generated .

But if you useDLL, theDLLdon't have to be included in finallyEXEfile,EXEThe file runtime is able to "dynamically" reference and unload thisEXEindependent ofDLLfile. Another difference between static-link libraries and dynamic-link libraries is that static-link libraries can no longer include other dynamic-link libraries or static libraries. In the dynamic link library, you can also include other dynamic or static link libraries. A dynamic library is a function that needs to be called, and then calls into the stack to run according to the Function mapping table. Assume that there are multiple pairs in the current projectDLLa call to the same function in the file. At run time, this function will only leave a copy.

However, assuming that there are multiple calls to the same function in the lib file, at run time, the function will leave multiple copies in the running space of the current program, and will produce a copy of a call.



Linux static link library and dynamic Connection library

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.