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? Non-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 running code, you know the name of the library on which it depends, but you also 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:

Assuming that it is installed under/lib or/usr/lib, the LD can be found by default, no additional action is required, and is assumed to be installed 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 is located; Execute ldconfig. The command rebuilds the/etc/ld.so.cache file. We'll copy the created dynamic library to/usr/lib and then execute the test program. Success.


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 code compiled as location-independent. Used to compile 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, followed by a. 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. The NM command can print out all the symbols involved in the library. A library can be both static and dynamic. There are very many symbols listed in NM. There are three common types of:
One is called in the library. However, it is not defined in the library (indicating that additional library support is required). denoted by U.
One is a function defined in the library. This is the most common, expressed in T.
One is the so-called weak-state notation, which is defined in the library. However, it may be overwritten with the same symbol 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. If the file has changed, 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 that you are using a static link library, whether you want to or not. Liball of the instructions are directly included in the last generatedEXEin the file. 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, and other dynamic or static link libraries can be included in the dynamic-link library. 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. Assuming that there are multiple calls to the same function in the DLL file in the current project , the function will only leave a copy of it at run time. But suppose there are multiple calls to the same function in the lib file. At run time, the function will leave multiple copies in the current program's running space, and a copy is generated from a single invocation.



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.