Notes for Dynamic Link Libraries in linux

Source: Internet
Author: User
Notes on the Dynamic Link Library under linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. The header file required to load the dynamic link library is ldfcn. h.

The main functions are: dlopen dlsym dlerror dlclose

When compiling a dynamic library source file, the-fPIC parameter must be included.

Gcc-fPIC-c test. c

Use the-shared parameter to link the so file.

Gcc-shared-o test. so test. o

The-dynamic parameter is generally used when the dynamic library to be used is linked.

Gcc-rdynamic-o main. cpp-ldl

Dlsym can only find functions and variables, but cannot find class definitions. It is a little tricky to use classes in a dynamic library.

First, c ++ will make the function name very strange during compilation. When using dlsym, this function will not be found, which requires extern "C" to play a role.

Use extern "c" to modify the functions to be exported in the C ++ file. The function name will not be destroyed during compilation, this means that this function cannot be reloaded. (All function names that need to be searched by dlsym must be modified by extern "C", and others are not required ).

Secondly, since dlsym cannot find the class definition, it wants a new method for generating classes and enabling classes to be used normally. This requires polymorphism. In the header file of the library, define a virtual base class and all operations. In the cpp file, start to define the implementation of all the sub-classes and sub-classes of this class (the sub-class definition can also be placed in other header files ). To get a specific class, And dlsym can only find the function, it is necessary to define two factory functions in the header file. One is used to generate a specific class by parameters, and the other is responsible for destroying the specific class. This definition method of the dynamic library is suitable for plug-ins. Functions in the class do not need to be modified by extern "C", because you will not use dlsym to find them. The two factory functions are exported and the only way to generate and destroy objects. They need to be modified by extern "C.

Some undefined functions and variables can be used in the source file of the dynamic library, which are undefined symbols. When dlopen, a program using a dynamic library, opens the dynamic library, the undetermined symbols used in these functions will be re-parsed. The parsing time is determined by the dlopen parameter.

RTLD_NOW indicates that Parsing is started now. If some symbols are still not found, the parsing will fail, and dlopen returns a null pointer.

RTLD_LAZY indicates that the pending symbols are not resolved at present, and that function will be parsed when you use dlsym to find a function.

The RTLD_LOCAL and RTLD_GLOABL parameters can be added to the preceding two parameters. They determine whether the symbols they use are visible to other external dynamic libraries.

If the dynamic library a. so defines a function hello.

The world function in the dynamic library B. so uses a function named hello, while B does not define hello. Hello in B is the pending symbol.

When a program main needs to use both the dynamic library a. so and B. so, and the main does not define a function named hello, it first loads the library

Dlopen ("a. so", RTLD_LAZY | RTLD_GLOBAL)

Because the RTLD_GLOBAL parameter is used, any other dynamic library that loads data later can see the hello function defined in database a. If you load database B, you must parse all the symbols immediately, no problem.

Dlopen ("a. so", RTLD_NOW)

However, if you do not use RTLD_GLOBAL when loading database a, when loading database B, it considers that there is no hello function outside. Because the symbolic parsing fails, dlopen will immediately return a NULL.

The use of impl idiom can effectively reduce the dependency on header files. The pointer in the idiom must be assigned a new object when it is generated. If you do not want to simply add a new object, use a fixedAllocator. It is not very troublesome to use the library or write it by yourself, and can effectively improve efficiency.

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.