Linux shared library programming-Introduction to Linux shared library Programming

Source: Internet
Author: User

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.

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.