unix / linux shared library (DLL) Introduction

First, create a shared library:
1. Write source xxx1.c xxx2.c ... / * c (wildcard mode)
2. Compile source code, plus -fpic .o files are generated
gcc -c -fpic xxx1.c xxx2 .c ... / *. c (wildcard embodiment)
3. Build shared libraries
GCC -shared -o xxx.o libxxx.so
4. header files, easy call

Second, the use of shared libraries
1. Write to call the source xxx.c
2. compile the source code to generate .o file
3. Link the shared library files
gcc test.o -l xxx (shared libraries to remove the lib prefix and suffix .a rest name) -L. ---------- {PATH}
If the operating system supports LIBRARY_PATH environment variables, using the environmental variables may be omitted -L
4 performed
during execution, the dynamic loading of shared libraries, the system will automatically find LD_LIBRARY_PATH path environment variable to determine the location of a shared library, if you can not find
the shared library files, the program will fail. (Static library is not required)


Third, and explicitly load the shared library
#include <dlfcn.h>
void * dlopen (const char * filename, int In Flag);
// filename: filename SO
// flag: Open
// Return value: void *, shared library files Enables pointer
char * dlerror (void);
// check whether the shared libraries are opened with dlopen success
// returns a null pointer indicates the success of open
// return a non-null string representation failed to open, the contents of the string that contains the cause of failure
void dlsym * (hanlde void *, const char * Symbol);
// use the shared library dlsym function to obtain after opening the
// hanlde, shared libraries address you want to use, dlopen return value
// symbol, you need to call the function name
// return value: void *, require their own corresponding functions using conversion type
int dlclose (void * handle);
// close the shared library, the memory release
the meaning of the parameter flag dlopen follows:
RTLD_LAZY: symbol lookup when it is loaded. (Loaded into memory when used)
RTLD_NOW: immediately loaded. (Ie open loaded into memory)

Guess you like

Origin www.cnblogs.com/alphain/p/11008150.html