The use of dynamic library under linux

Compile-time and runtime library paths

Path search order for runtime dynamic libraries

  1. LD_PRELOAD environment variable, generally used for hacking

  2. The dynamic library search path specified when compiling the object code (referring to the -wl, rpath or -R option instead of -L), the readelf -dcommand can view the rpath parameter in the compiled object file;

    gcc -Wl,-rpath,/home/arc/test,-rpath,/usr/local/lib test.c
  3. The dynamic library search path specified by the environment variable LD_LIBRARY_PATH ;

    export LD_LIBRARY_PATH=/root/test/env/lib
    ./main

    or

    LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./main
  4. The dynamic library search path specified in the configuration file /etc/ld.so.conf ;

    Remember to execute the command after changing the /etc/ld.so.conf file: ldconfig ! This command will load the libraries under all paths in the /etc/ld.so.conf file into memory

  5. The default dynamic library search path /lib ;

  6. The default dynamic library search path /usr/lib .

The search path to look for libraries at compile time

  1. Use -L to specify the path to the library when compiling ;
    gcc main.c -o main -L./ -lcac
  2. Specify the search path through the environment variable LIBRARY_PATH

    LIBRARY_PATH=.:$LIBRARY_PATH gcc main.c -o main -lcac

  3. System standard path /lib /usr/lib /usr/local/lib

Compare

  1. The static library or dynamic library is searched at compile time, and only the dynamic library is searched at runtime;
  2. The gcc parameter -L specifies the link path at compile time, and -Wl,-rpath specifies the link path at runtime;
  3. Use the environment variable LIBRARY_PATH to specify the library path at compile time, and use the environment variable LD_LIBRARY_PATH or /etc/ld.so.conf to specify the library path at runtime ;
  4. The linker used at compile time is ld , and the linker used at runtime is /lib/ld-linux.so.2 ;
  5. Both compile time and runtime look for the default path: /lib /usr/lib ;
  6. There is also a default path at compile time: /usr/local/lib , which is not looked up at runtime by default.

Dynamic library usage example

header file that defines the library

/*caculate.h*/

#ifndef CACULATE_HEAD_
#define CACULATE_HEAD_
//加法
int add(int a, int b);
//减法
int sub(int a, int b);
//除法
int div(int a, int b);
//乘法
int mul(int a, int b);

#endif

Implementation of functions in the library

/*caculate.c文件*/
#include "caculate.h"

//求两个数的和
int add(int a, int b)
{
    return (a + b);
}
//减法
int sub(int a, int b)
{
    return (a - b);
}
//除法
int div(int a, int b)
{
    return (int)(a / b);
}
//乘法
int mul(int a, int b)
{
    return (a * b);
}

Compile and produce the libcac.so file as follows:gcc -shared -fPIC caculate.c -o libcac.so

How to use dynamic library 1

#include <stdio.h>
#include "caculate.h"

int main()
{
    int a = 20;
    int b = 10;
    printf("%d + %d = %d\n", a, b, add(a, b));
    printf("%d - %d = %d\n", a, b, sub(a, b));
    printf("%d / %d = %d\n", a, b, div(a, b));
    printf("%d * %d = %d\n", a, b, mul(a, b));
    return 0;
}

Compile and run:

gcc main.c -o main -L ./ -lcac
LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./main

How to use dynamic library 2

#include <stdio.h>
#include <dlfcn.h>

#define DLL_FILE_NAME "libcac.so"

int main()
{
    void *handle;
    int (*func)(int, int);
    char *error;
    int a = 30;
    int b = 5;

    handle = dlopen(DLL_FILE_NAME, RTLD_NOW);
    if (handle == NULL)
    {
    fprintf(stderr, "Failed to open libaray %s error:%s\n", DLL_FILE_NAME, dlerror());
    return -1;
    }

    func = dlsym(handle, "add");
    printf("%d + %d = %d\n", a, b, func(a, b));

    func = dlsym(handle, "sub");
    printf("%d + %d = %d\n", a, b, func(a, b));

    func = dlsym(handle, "div");
    printf("%d + %d = %d\n", a, b, func(a, b));

    func = dlsym(handle, "mul");
    printf("%d + %d = %d\n", a, b, func(a, b));

    dlclose(handle);
    return 0;
}

compile and run

gcc call_main.c -o call_main -ldl -Wl,-rpath,./
./main

write picture description here
refer to:

Dynamic library version compatibility issues

Dynamic library and static library exist at the same time

refer to

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325689153&siteId=291194637