Linux 7 shared libraries so the dynamic loading dlopen

In front of the shared library is loaded by a static method used, about to be compiled into a shared library code, each shared library must be recompiled to modify the source file that uses the library.
Here we use dynamically loaded dynamic link library use, as long as the dynamic link library inside the corresponding function has not changed, loaded dynamically without recompiling the source file, you can use. You can reduce the size of the program, but also facilitate the expansion of dynamic libraries. If it is not very common, the program starts when you can not load, use only loaded when needed.

Use:
dlopen open dynamic link library
function dlsym use library
dlclose close open dynamic link library

dlopen
basic definition
functions: opening a dynamic link library
include the header file: #include <dlfcn.h> 
function definition: void * dlopen (const char *  pathname, int mode);
Function Description: designation mode in order to open the dlopen () function specified dynamic link library file and returns a handle to the calling process. Use dlclose () to unload the open library. 
mode: divided into two 
RTLD_LAZY defer the decision, such as when there is need to solve symbol.
RTLD_NOW decided to immediately lift all pending sign before returning. 
RTLD_LOCAL RTLD_GLOBAL allowed to export symbols.
RTLD_GROUP RTLD_WORLD Returns: Open error return NULL, the successful return of library references.
Compile time to join -ldl (designated dl library) 
such as gcc test.c -o test -ldl

dlsym () function prototype is 
void * dlsym (void * handle,  const char * symbol)
This function <dlfcn.h> file. 
open handle is returned by a dynamic-link library dlopen pointer,
Symbol function name is acquired request
function return value is void *, pointing to the address of the function, it calls for the use of
taking dynamic objects Address:
#include <dlfcn.h>
void dlsym * (pHandle void *, char * symbol);
dlsym operating handle in accordance with a dynamic link library (pHandle) symbol (symbol), the symbol corresponding to the return address.
Using this function can not only get the function address, you can also get variable address.

dlclose () 
include the header file: #include <dlfcn.h> 
function prototype: int dlclose (void * handle)  ;
Function Description: dlclose handle for closing the specified dynamic link library, the usage count only when this is a dynamic link library 0:00, it will really be unloaded system.

the dlerror () 
include the header file: #include <dlfcn.h> 
function prototype: const char * dlerror (void)  ;
Function Description: indicates the operation when a dynamic link library function fails, the dlerror may return an error message, when the return value is NULL operation function succeeds.

Dynamic libraries:

/* calculate.c */

int add(int a,int b)
{
   return (a + b);
}

int sub(int a, int b)
{
   return (a - b);
}

int mul(int a, int b)
{
   return (a * b);
}

int div(int a, int b)
{
   return (a / b);
}

Dynamic loading:

/* test.c */

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include "types.h"

#define LIB_CACULATE_PATH "./libcaculate.so"

typedef int32 (*CAC_FUNC)(int, int);

int32 main()
{
    void *handle;
    uint8 *error;
    CAC_FUNC cac_func = NULL;

    /* open ddl */
    handle = dlopen(LIB_CACULATE_PATH, RTLD_LAZY);
    
    if (NULL == handle) 
    {
        fprintf(stderr, "%s\n", dlerror());
        exit(EXIT_FAILURE);
    }

    dlerror();

    /* get function add sub mul div */

    cac_func = (CAC_FUNC)dlsym(handle, "add");
    printf("add: %d\n", (*cac_func)(2,7));

    cac_func = (CAC_FUNC)dlsym(handle, "sub");
    printf("sub: %d\n", cac_func(9,2));

    cac_func = (CAC_FUNC)dlsym(handle, "mul");
    printf("mul: %d\n", cac_func(3,2));

    cac_func = (CAC_FUNC)dlsym(handle, "div");
    printf("div: %d\n", cac_func(8,2));

    /* close ddl */
    dlclose(handle);
    
    exit(EXIT_SUCCESS);
}

Makefile

.PHONE : all

all : libcaculate.so test

libcaculate.so : caculate.o
	gcc -Wall -fPIC -shared $^ -o $@
test: test.o
	gcc -Wall -L./ -rdynamic $^ -o $@ -ldl
test.o : test.c
	gcc -c $<
caculate.o : caculate.c
	gcc -c $<

clean:
	rm -f *.so *.o test

Guess you like

Origin blog.csdn.net/zzj244392657/article/details/92561796