Linux dynamic connection library
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 dynamic shared target file and map it to the memory, return to its first address
Ø DLSYM: pointer to the entry point for returning the lock request
Ø DLERROR: Return null or point 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 environment variable ld_library_path
Ø In the list of libraries specified in /etc/ld.so.cache
Ø in / usr / lib or / lib
Let's take an example.
Main program file hello.c:
#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 the DLL until the DLL is executed
Ø RTLD_NOW: Resolve 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
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.