Generation and reference of linux dynamic link library (.so)

Generation and reference of dynamic link library (.so)

1. Introduction

Dynamic link library: In the Linux system, the dynamic link library (Dynamic Linking Library) is a shared library linked at runtime. Compared with the static link library, the dynamic link library can be loaded when the program is running, and does not need to be linked into the program at compile time, which can reduce the size of the executable program and facilitate the management and update of the library.

The dynamic link library has two main extensions, namely .so and .dll, where the .so file is the dynamic link library under the Linux system, and the .dll file is the dynamic link library under the Windows system. Under the Linux system, the dynamic link library is usually generated with the GCC compiler and compiled with the -shared option. When the program is running, the dynamic link library will be dynamically loaded into the memory, and multiple programs can share the same dynamic link library, reducing the memory and disk space usage.

Dynamic link libraries are widely used in Linux systems, and many system libraries and third-party libraries are dynamic link libraries, such as libc, libpthread, libssl, and so on. The use of the dynamic link library can improve the portability, maintainability and security of the program, which is one of the important features of the Linux system.

2. Generation of dynamic link library

In Linux, you can use the gcc command to generate a dynamic link library (also called a shared library) from multiple object files. The steps to generate a dynamic link library are as follows:

--sodir
	|--so.c
	|--s0.h
	|--libtest.so
	
gcc  so.c -fPIC -shared  -L./sodir -o libtest.so 

-fPIC:生成与位置无关的代码,这是生成动态链接库所必需的。
-shared:告诉链接器生成动态链接库而不是可执行文件.
-L:指定源文件路径,源文件在当前编译路径下不需要。
-o:指定输出文件名.
so.c:参与编译的源文件.
libtest.so:lib为前缀,test为库名,.so为后缀。
注意:库名可以自由取名,但是前缀和后缀必须遵循规范,否则系统无法识别。

3. Reference dynamic link library

  1. First, we create a source file called libhello.c that contains a simple hello function:

    //libhello.c
    #include <stdio.h>
    void hello() {
          
          
        printf("Hello, world!\n");
    }
    
  2. Next, we use the gcc command to compile this source file and generate a dynamic link library named libhello.so:

    gcc -fPIC  -shared -o libhello.so libhello.c
    
  3. Now, we can reference this dynamic link library in another source file, for example, we create a source file named main.c:

    //main.c
    void hello();
    
    int main() {
          
          
        hello();
        return 0;
    }
    
  4. Then compile main.c to generate an executable file:

    gcc -o main main.c -L. -lhello
    
    -L:指定so文件所在的目录;
    -l:动态链接库名,去除前缀lib,去除后缀.so;
    
  5. Finally, execute main:

    Save information, dynamic link library file not found.

    insert image description here

    Solution: Add the file where the dynamic link library is located to the environment variable:

    export LD_LIBRARY_PATH=$(pwd)
    

    insert image description here

    Other solutions:
    1. Temporary export environment variable = $ environment variable: the path you want to add
    2. Write to /etc/profile
    3. Write to .bashrc in your home directory
    4. Write to /etc/environment

Guess you like

Origin blog.csdn.net/qq_38393271/article/details/129161064
Recommended