Linux shared library Programming
Author: su huaiguang
I. Description
Similar to the dynamic link library in windows, Linux also has a shared library to support code reuse. *. Dll in windows and *. So in Linux. The following describes how to create and use a Linux shared library.
Ii. Create a shared library
In the mytestso. c file, the Code is as follows:
# Include <stdio. h>
# Include <stdlib. h>
Int getmax (int A, int B)
{
If (A> = B)
Return;
Return B;
}
Int getint (char * psztxt)
{
If (0 = psztxt)
Return-1;
Return atoi (psztxt );
}
Run the following command to compile:
gcc -fpic -shared mytestso.c -o mytestso.so
-FPIC: the output object module is generated in the relocated address mode.
After compilation is successful, mytestso. So is in the current directory, and the shared library mytestso. So has been created successfully.
Iii. Use shared libraries
Functions in the shared library can be loaded and executed by the main program, but do not need to be linked to the target file of the main program during compilation. When using functions in the shared library, the main program needs to know the name (string) of the function in advance, and then obtain the start address (function pointer) of the function according to its name ), then you can use the function pointer to use the function.
In the mytest. c file, the Code is as follows:
#include <dlfcn.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
void* pdlhandle;
char* pszerror;
int (*GetMax)(int a, int b);
int (*GetInt)(char* psztxt);
int a, b;
char* psztxt = "1024";
// open mytestso.so
pdlhandle = dlopen("./mytestso.so", RTLD_LAZY);
pszerror = dlerror();
if (0 != pszerror) {
printf("%s"n", pszerror);
exit(1);
}
// get GetMax func
GetMax = dlsym(pdlhandle, "GetMax");
pszerror = dlerror();
if (0 != pszerror) {
printf("%s"n", pszerror);
exit(1);
}
// get GetInt func
GetInt = dlsym(pdlhandle, "GetInt");
pszerror = dlerror();
if (0 != pszerror) {
printf("%s"n", pszerror);
exit(1);
}
// call fun
a = 200;
b = 600;
printf("max=%d"n", GetMax(a, b));
printf("txt=%d"n", GetInt(psztxt));
// close mytestso.so
dlclose(pdlhandle);
}
Run the following command to compile:
gcc mytest.c -ldl -o mytest
-LDL option, indicating that the generated object module must use the Shared Library
(1) dlopen ()
The first parameter: Specifies the name of the shared library, and searches for the specified shared library in the following position.
-The environment variable LD_LIBRARY_PATH lists all directories separated by semicolons.
-Use ldconfig to maintain the list of databases found in the/etc/lD. So. cache file.
-Directory usr/lib.
-Directory/lib.
-Current directory.
Second parameter: specify how to open the shared library.
-Rtld_now: load all functions in the shared library to the memory.
-Rtld_lazy: pushes back the function loading operation in the shared library until a function is loaded when dlsym () is called.
(2) dlsym ()
When you call dlsym, use the phandle and function name of the Shared Library returned by dlopen () as parameters to return the entry address of the function to be loaded.
(3) dlerror ()
This function is used to check the errors related to the function that calls the shared library.
Iv. Conclusion
This article describes how to use shared libraries in Linux, and describes how to use shared libraries in a simple example.