Linux dynamic connection library

zhaozj2021-02-16  98

I. Principle and use of dynamic link library

Everyone is very familiar with the DLL file in the Windows operating system. In fact, this software component is also implemented in Linux. In fact, plugins and DLLs are usually used to add features to the entire new application, in general, the plugin provides new features for existing applications without changing the original application. There is even better in the Linux environment.

Linux provides 4 library functions, one header file DLFCN.H and two shared libraries (static library libdl.a and dynamic libraries libdl.so) support dynamic links.

? DLOPEN: Open the dynamically shared target file and map it into memory, return to its first address

? DLSYM: Returns the pointer to the entry point of the lock request

? DLERROR: Returns NULL or points to the string of the most recent wrong

? DLClose: Close Dynamic Sharing File

Function DLOPEN needs to find the target file in the file system and create a handle for it. There are four ways to specify the location of the target file:

? Absolute path

? In the directory specified in the environment variable ld_library_path

? In the list specified in the library specified in /etc/ld.so.cache

? In / usr / lib or / lib

Let's take an example.

Main program file hello.c:

#include #include void * slib = 0; void (* func) (char *); const char * Herror; int main (int Argc, char * argv []) {slib = DLOPEN ("./ Slib.so", RTLD_LAZY); Herror = DLERROR (); if (Herror) {Printf ("DLOPEN Error! / N"); Return 1;} Func = DLSYM (Slib, "FUNC"); Herror = DLERROR (); if (Herror) {DLClose (SLIB); Printf ("DLSYM Error! / N"); RETURN 1;} Func ("How do you do? / n"); Dlclose (Slib); Herror = DLERROR (); if (Herror) {Printf ("DLClose Error! / N"); Return 1;} return 0;

There are two options for the second parameter of the function DLOPEN:

• RTLD_LAZY: Postpone an external reference in parsing DLL until the DLL is executed

• RTLD_NOW: Resolves all external references before returning

The following is a DLL file source Slib.c:

INT func (char * msg) {Printf ("Func Be EXECUTED! / N"); Printf (MSG); Return 0;}

Is not it simple?

After the source code is written, it is a bit complicated when compiling and linking. To this end, we wrote a makefile:

All: Hello Slib.so Hello: gcc -o hello hello.c-ldl slib.so:slib.o gcc -shared -lc-io slib.so slib.o Slib.O: gcc -c -fpic slib.c generated This program takes three steps:

• Compile DLL into location without code

? Create a DLL shared target file

• Compile the main program and link with the DLL

When compiling Slib.c, use the -fpic or -fpic option, so that the generated code is not related to the location, because the reconstruction shared target library requires a location independent, and this class code supports large offset.

Use the -shared option when creating a DLL shared target file, which generates a shared target file Slib.so for dynamic link.

When generating a primary program, use the -ldl option, which is a link option, the partial symbol in the main program is a symbol in the dynamic link library, that is, it is necessary to resolve the reference in the DLL file at runtime.

2. Establishment of a dynamic function library of general types

The Linux operating system and a variety of packages provide a lot of dynamic library files for software developers. But in general, these libraries cannot meet all users' needs. Developers will write a lot of functions based on their needs. For these functions, if the source file is always linked to the program that calls them, although it is, the disadvantage is obvious. Then add them in a dynamic library.

In Linux, establish a dynamic function library does not require additional tools, only GCC is available.

It can be very convenient to see which libraries can be viewed through the LDD command.

Next, a simple example will be described with a simple example.

File MYLIB.C is the source program file of the library, the content is as follows:

Int myadd (int A1, int A2) {RETURN A1 A2;

File Testlib.c is the source program file of the test program:

#incoude extern int myadd (int, int); int main () {Printf ("% d / n", myadd (1, 2)); return 0;}

The following is given the contents of Makefile:

All: libmylib.so.1.0 testlib libmylib.so.1.0: mylib.o ld -m elf_i386 -shared -soname libmylib.so.1 -o libmylib.so.1.0 mylib.o ln -sf libmylib.so.1.0 libmylib. SO.1 ln -sf libmylib.so.1 libmylib.so testlib: testlib.c gcc -wall -o2 -l. -lmylib -o testlib testlib.c mylib.o: mylib.c gcc -c -wall -O2 - fpic -o mylib.o mylib.c clean: -rm -f libmylib.so * testlib * .o

Enter your Make command in Linux's shell, dynamic functions library libmylib.so.1.0 and its test programs are generated. Run ./testlib Try it.

If you don't walk, the system will not find the libmylib.so.1 dynamic function library because the system is considered such a file or directory. Don't panic. You may need to use the ld_library_path environment variable.

[root @ localhost home] export ld_library_path =.: $ ld_library_path

Run a test program again.

转载请注明原文地址:https://www.9cbs.com/read-10905.html

New Post(0)