- C language
- Compile
- here
How to create and use a shared library
Explains how to create and use shared libraries on Unix / Linux. This article assumes that you understand the split compilation technique.
What is a shared library?
A shared library is an object file that is not statically linked when an executable file is generated, but can be linked at run time. Shared libraries can be linked at link time and can also be loaded at run time. The extension is ".so".
Please note that the shared libraries described here are a UNIX / Linux mechanism, not a Mac or Windows mechanism. For Mac, the extension is different. Windows has a similar mechanism called DLL (Dynamic Link Library), but it is different from UNIX / Linux shared libraries.
How to create a shared library
Specify the "-shared" option to create a shared library. Generally, specify "-fPIC" and Position-independent code.
Add the "-shared" and "-fPIC" options to the procedure for creating the object file so that:
#Normal compilation-Create object file gcc -Iinclude -c -o mylib1.o src / mylib1.c #Create a shared library gcc -Iinclude -c -shared -fPIC -o mylib1.so src / mylib1.c
Create and link a shared library
For this procedure, you can use the procedure used in Split compilation method. Create the library as a shared library.
Application source files
The source file "myapp.c" for the application.
myapp.c
#include <stdio.h> #include "mylib1.h" #include "mylib2.h" int main (void) { mylib1_print (); mylib2_print (); }
Library header file
The library header files "mylib1.h" and "mylib2.h".
mylib1.h
#ifndef MYLIB1_H #define MYLIB1_H void mylib1_print (void); #endif
mylib2.h
#ifndef MYLIB2_H #define MYLIB2_H void mylib2_print (void); #endif
Library source files
The library source files "mylib1.c" and "mylib2.c".
mylib1.c
#include <stdio.h> #include "mylib1.h" void mylib1_print (void) { printf("mylib1_print\n"); }
mylib2.c
#include <stdio.h> #include "mylib2.h" void mylib2_print (void) { printf("mylib2_print\n"); }
Compile
Compile the application source file and the shared library source file respectively. Specify the location of the header file with the option "-I". Create a shared library with the "-shared" and "-fPIC" options.
gcc -Iinclude -c -o myapp.o src / myapp.c gcc -Iinclude -c -shared -fPIC -o mylib1.so src / mylib1.c gcc -Iinclude -c -shared -fPIC -o mylib2.so src / mylib2.c
Link-Create executable file
Link to create an executable file. Symbols such as function names are tied to the entity.
gcc -o myapp myapp.o mylib1.so mylib2.so
Execution
Let's execute the executable file.
./myapp
This is the output result.
mylib1_print mylib2_print
I've mastered how to create a shared library and link it when creating an executable.
How to load a shared library at runtime and execute a function?
Load the shared library at run time and expose only the steps to execute the function.
1. Open the shared library with the dlopen function.
2. Specify the function name with dlsym to get the function pointer.
3. Execute the function from the function pointer.
How to know the symbols such as function names included in the shared library?
Use the "nm" command to find out the symbols such as function names contained in the shared library. It seems to be an abbreviation for "name".
nm mylib1.so
This is the output result.
U _GLOBAL_OFFSET_TABLE_ 0000000000000000 T mylib1_print U puts
The nm command can be used not only for shared libraries, but also for executables and object files.
Search for shared libraries
Shared libraries have a mechanism to avoid collisions. For shared libraries, you can specify the shared library search directory with the "-L" option without having to name the file directly. The library name must start with "lib".
If you place a shared library in the shared library search directory, the first shared library found specified by the "-l" option will be used. The "-l" option omits the leading "lib" and ".so".
Shared library
/foo/lib/libmylib1.so
Shared library links
gcc -o myapp myapp.o -L /foo/lib -l mylib1