A simple dynamic link library example

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 lddcommands to view the dynamic link library files that executable files or other dynamic link library files depend on. You can use ldconfigcommands 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:

  1. 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.

  2. Write a compiler option file: Create a text file containing compiler options to specify the properties of the dynamic link library during compilation. -fPICYou can include things like compiler flags (like ) and linker flags (like -shared) in the options file .

  3. Compile source code using a compiler: Use a compiler to compile source code into object files. For example, gcccompile C source code using:

    gcc -c -fPIC source.c -o source.o
    ```
    
    这将生成一个目标文件 `source.o`。
    
    
  4. Create a dynamic link library: Use the compiler to link the object file into a dynamic link library file. Specify -sharedoptions to create the shared object file and use -othe options to specify the output file name. For example:

    gcc -shared source.o -o libexample.so
    ```
    
    这将生成一个名为 `libexample.so` 的动态链接库文件。
    
    
  5. 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/libetc. You can copy the dynamic link library file to one of the paths, or use LD_LIBRARY_PATHenvironment variables to specify a custom library path.

  6. 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.cFirst, 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.sodynamic link library file named .

After generating the .so file, write a simple program that uses the dynamic link library. Create a main.csource 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.ccompile into an executable file and link the dynamic link library:

gcc main.c -L. -lexample -o main

This will generate an mainexecutable file named . By using -Lthe option to specify the path of the dynamic link library (here the current directory), and using -lthe option to specify the name of the dynamic link library (removing libthe prefix and .sosuffix), 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 mainthe 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!

Guess you like

Origin blog.csdn.net/weixin_46841376/article/details/132490142