Linux C function dlopen implementation load dynamic Library so file code example

Source: Internet
Author: User
Tags posix

dlopen () is a powerful library function. The function opens a new library and loads it into memory. This function is primarily used to load symbols in the library, which are not known at compile time. This mechanism makes it unnecessary to recompile a module when it is added to or removed from the system. You can use Dlopen () in your own programs. Dlopen () is defined in the Dlfcn.h and implemented in the DL library. It requires two parameters: a file name and a flag. The file name is a dynamic library so file that indicates whether the library's dependencies are calculated immediately. If set to Rtld_now, it is calculated immediately, if Rtld_lazy is set, it is calculated when needed. In addition, you can specify Rtld_global, which makes the symbols available for libraries that are loaded later. < Span style= "COLOR: #999999" and compiled by www.169it.com

The Dlopen () function opens the specified dynamic connection library file in the specified mode, and returns a handle to the calling process. Use Dlclose () to unload the open library.

Mode is open mode, its value has multiple, different operating system on the implementation of features, under Linux, according to the function can be divided into three categories:

1 parsing methods

Rtld_lazy: Before Dlopen returns, no parsing is performed for undefined symbols in the dynamic library (only valid for function references, and for variable references is always resolved immediately).

Rtld_now: All undefined symbols need to be parsed before Dlopen returns, and if not resolved, the Dlopen will return null, with the error: Undefined symbol:xxxx ....

2 scope of action, can be resolved by means of the "|" Combined use

Rtld_global: Symbols defined in a dynamic library can be relocated by other libraries that are opened later.

Rtld_local: In contrast to Rtld_global, symbols defined in a dynamic library cannot be relocated by other libraries that are subsequently opened. If it is not specified as Rtld_global or rtld_local, the default is rtld_local.

3 function mode

Rtld_nodelete: The library is not unloaded during dlclose (), and static variables in the library are not initialized at a later time when the library is reloaded with Dlopen (). This flag is not a POSIX-2001 standard.

Rtld_noload: The library is not loaded. Can be used to test whether the library has been loaded (Dlopen () returns null if the description is not loaded, otherwise the description has been loaded), or it can be used to change the flag of the loaded library, such as: Flag for the previously loaded library is rtld_local, with Dlopen (rtld_noload| RTLD_GLOBAL) flag will become Rtld_global. This flag is not a POSIX-2001 standard.

Rtld_deepbind: Search for symbols in the library before searching for global symbols, avoiding conflicts with symbols of the same name. This flag is not a POSIX-2001 standard.


function return value:

Open error returns null

Successful, return to library Reference

Compile time to join-LDL (Specify DL library)


Specific code examples are as follows:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include "stdio.h" #include <dlfcn.h> intmain( intargc, char*argv[]) {      char module_name[1024]={ "./test_module.so" };      char*error=NULL;      void*module_handle = dlopen(module_name, RTLD_NOW);      if (module_handle == NULL)      {        error = dlerror();        chartp[1024] = {0};          snprintf(tp, 256,       "Load module \"%s\" error: %s\n" ,       module_name, error);            printf ( "%s" ,tp);        return1;      } else      {          printf ( "Load module[%s] success!\r\n" ,module_name);      }      return1; }
    • Article Source: Linux C function dlopen implementation load dynamic Library so file code example


Linux C function dlopen implementation load dynamic Library so file code example

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.