Linux dynamic Library Loading

Source: Internet
Author: User

Dlopen () is a powerful library function. This function will open a new library and load it into the memory. This function is mainly used to load symbols in the library. These symbols are unknown during compilation. For example, the Apache web server uses this function to load modules during running, which provides additional capabilities. A configuration file controls the process of loading modules. This mechanism does not need to be re-compiled when a module is added or deleted in the system.

If the function fails to be executed in the dynamic link library, dlerror returns an error message. If the returned value is null, the operation is successful.

Creating and using dynamic link libraries in Linux is not difficult.

Compile function sourceProgramSelect the-shared option to create a dynamic link library. so suffix name, it is best to put it under the Public Library directory (such as/lib,/usr/lib), and write the user interface file to facilitate sharing by other users.

Use the dynamic link library. The source program must contain dlfcn. h header file. When writing a program, pay attention to the correct call of functions such as dlopen. during compilation, use the-rdynamic option and-LDL option to generate the execution of the dynamically linked library that can be called.Code.

Here we will talk about the determination of the dlsym return value.

Cosine = dlsym (handle, "Cos ");// Cos is the symbol

If the symbol is not found,   In the specified library or any of the libraries that were automati-cally loaded by dlopen () when that library was loaded, dlsym () returns   Null. (The search timed med by dlsym () is breadth first through   Dependency tree of these libraries.) since the value of the symbol   Cocould actually be null (so that a NULL return from dlsym () need not   Indicate an error), the correct way to test for an error is to call   Dlerror () to clear any old error conditions, then call dlsym (), and   Then call dlerror () Again, saving its return value into a variable, and   Check whether this saved value is not null.

The statement is as follows:

①Dlerror ();

②A_func_name =... dlsym (...);

③If (dlerror () goto end;

④A_func_name (...);

Example:

However, this method is often used in practice:

Cosine = dlsym (handle, "Cos ");

If (error = dlerror ())! = NULL)

{

Fprintf (stderr, "% s \ n", error );

Exit (1 );

}

Example

# Include <stdio. h>

# Include <dlfcn. h>

Int main (INT argc, char ** argv)

{

Void * handle;

Double (* cosine) (double );

Char * error;

Handle = dlopen ("libm. So", rtld_lazy );

If (! Handle)

{

Fprintf (stderr, "% s \ n", dlerror ());

Exit (1 );

}

Cosine = dlsym (handle, "Cos ");

If (error = dlerror ())! = NULL)

{

Fprintf (stderr, "% s \ n", error );

Exit (1 );

}

Printf ("% F \ n", (* cosine) (2.0 ));

Dlclose (handle );

Return 0;

}

Gcc-rdynamic-O Foo. C-LDL

Original

[1]Http://blog.csdn.net/jernymy/article/details/6903683

[2] http://www.9php.com/FAQ/cxsjl/c/2006/12/710159053564.html

[3] http://linux.about.com/library/cmd/blcmdl3_dlsym.htm

[4] http://stackoverflow.com/questions/13941944/why-can-the-value-of-the-symbol-returned-by-dlsym-be-null

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.