Article directory
1. Introduction
The dynamic link library file under Linux (Dynamic Link Library) is also called a shared object file (Shared Object). In Linux systems, the suffix of dynamic link library files is usually ".so" (Shared Object).
Dynamic link library files are files that contain code and data that can be dynamically loaded and linked by a program at runtime. They provide a mechanism for sharing code and resources. Multiple programs can share the same dynamic link library, thus saving system resources and memory space.
Dynamic link library files are not statically embedded into the executable file during the compilation and linking process, but are dynamically loaded into memory when the program is running. In this way, different programs can share the same dynamic link library, thereby reducing the use of storage space, and making software updates more convenient. Only the dynamic link library needs to be updated without recompiling the entire program.
In Linux systems, you can use ldd
commands to view the dynamic link library files that executable files or other dynamic link library files depend on. You can use ldconfig
commands to manage dynamic link libraries in the system.
2. Creation and compilation steps
When creating and compiling dynamic link library files in a Linux system, you can follow these steps:
-
Writing source code: First, you need to write a source code file containing the required functionality, using an appropriate programming language (such as C, C++, etc.). Make sure to use appropriate function export tags (such as
__attribute__((visibility("default")))
) in your code so that they are visible in the dynamic link library. -
Write a compiler option file: Create a text file containing compiler options to specify the properties of the dynamic link library during compilation.
-fPIC
You can include things like compiler flags (like ) and linker flags (like-shared
) in the options file . -
Compile source code using a compiler: Use a compiler to compile source code into object files. For example,
gcc
compile C source code using:gcc -c -fPIC source.c -o source.o ``` 这将生成一个目标文件 `source.o`。
-
Create a dynamic link library: Use the compiler to link the object file into a dynamic link library file. Specify
-shared
options to create the shared object file and use-o
the options to specify the output file name. For example:gcc -shared source.o -o libexample.so ``` 这将生成一个名为 `libexample.so` 的动态链接库文件。
-
Set library path: Place the generated dynamic link library file in the appropriate library path so that the system can find it. Common library paths include
/usr/lib
,/usr/local/lib
etc. You can copy the dynamic link library file to one of the paths, or useLD_LIBRARY_PATH
environment variables to specify a custom library path. -
Use dynamic link library: In your program, you can use the functions provided by dynamic link library. When compiling and linking your program, be sure to specify the correct dynamic link library file name and path so that the program can load and link the dynamic link library correctly.
These are the general steps for creating and compiling dynamic link library files. The exact steps may vary depending on the programming language, compiler, and build system.
3. Specific examples
example.c
First, create a source code file named in the corresponding directory with the following content:
#include <stdio.h>
__attribute__((visibility("default")))
void say_hello() {
printf("Hello from the dynamic library!\n");
}
Next, use the following command to compile the source code into a dynamic link library file:
gcc -shared -fPIC example.c -o libexample.so
This will generate a libexample.so
dynamic link library file named .
After generating the .so file, write a simple program that uses the dynamic link library. Create a main.c
source code file named with the following content:
#include <stdio.h>
extern void say_hello();
int main() {
printf("Calling a function from the dynamic library...\n");
say_hello();
return 0;
}
Use the following commands to main.c
compile into an executable file and link the dynamic link library:
gcc main.c -L. -lexample -o main
This will generate an main
executable file named . By using -L
the option to specify the path of the dynamic link library (here the current directory), and using -l
the option to specify the name of the dynamic link library (removing lib
the prefix and .so
suffix), the program can correctly link the dynamic link library.
Before running, you need to specify the path of the dynamic link library. You should set the environment variable and add the current directory to it.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
It should be noted that export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
the modification is only effective for the current session
Then run main
the executable
./main
If you encounter permission problems, you can make the following settings
chmod +x main.c
The result looks like this:
Calling a function from the dynamic library...
Hello from the dynamic library!