Compile-time and runtime library paths
Path search order for runtime dynamic libraries
LD_PRELOAD environment variable, generally used for hacking
The dynamic library search path specified when compiling the object code (referring to the -wl, rpath or -R option instead of -L), the
readelf -d
command can view the rpath parameter in the compiled object file;gcc -Wl,-rpath,/home/arc/test,-rpath,/usr/local/lib test.c
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
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
The default dynamic library search path /lib ;
The default dynamic library search path /usr/lib .
The search path to look for libraries at compile time
- Use -L to specify the path to the library when compiling ;
gcc main.c -o main -L./ -lcac
Specify the search path through the environment variable LIBRARY_PATH
LIBRARY_PATH=.:$LIBRARY_PATH gcc main.c -o main -lcac
- System standard path /lib /usr/lib /usr/local/lib
Compare
- The static library or dynamic library is searched at compile time, and only the dynamic library is searched at runtime;
- The gcc parameter -L specifies the link path at compile time, and -Wl,-rpath specifies the link path at runtime;
- 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 ;
- The linker used at compile time is ld , and the linker used at runtime is /lib/ld-linux.so.2 ;
- Both compile time and runtime look for the default path: /lib /usr/lib ;
- 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
refer to:
Dynamic library version compatibility issues
Dynamic library and static library exist at the same time
- Linux program links dynamic library and static library at the same time
- Use Automake to generate Makefile and create dynamic and static libraries
refer to
- The path sequence of the link library at runtime under linux
- The use of Linux dynamic link library
- Skills: Detailed explanation of Linux dynamic library and static library production and use
- libc.so compatibility issue
- Use LD_PRELOAD to add hooks to glibc library functions
- Be wary of the LD_PRELOAD environment variable under UNIX