Linux generates dynamic libraries

Dynamic library

1. Naming rules

Linux: libxxx.so

  • lib: prefix (fixed);
  • xxx: The name of the dynamic library (choose it yourself);
  • .so: suffix (fixed);

Windows:libxxx.dll

2. Production of dynamic library

  1. Use gcc to get the .o file and get position-independent code:
gcc -c -fpic a.c b.c ...
  1. Use gcc to get the dynamic library:
gcc -shared a.o b.o ... -o libxxx.so

3.Example

Let’s first create a demo with the following structure:

The above code is on the blog: Linux generates static library

1. Use gcc to get the .o file and get position-independent code


At this time, the error message shows that head.h cannot be found. It’s because we didn’t specify the path to the header file. Just add -I ../include/ after the command.

2. Use gcc to get the dynamic library


Move the generated dynamic library file libcal.so to the lib/ directory.

3. Generate the executable program app and execute it


When we generated the executable file app and executed it, an error was reported.

It says the dynamic library cannot be found.

The reason for execution failure requires first understanding the process and principle of loading dynamic libraries in linux.

4. Working principle

  • Static library: gcc will package the code in the static library into an executable file when linking;
  • Dynamic library: gcc When linking, the code of the dynamic library will not be packaged into the executable file;

After the program is started, the dynamic library will be dynamically loaded into the memory, and the dependencies of the dynamic library can be checked through the ldd (list dynamic dependencies) command.

So how do we locate shared files?

When the system loads an executable file, it can know the name of the library it depends on, but it also needs to know the absolute path of the library. At this time, the system’s dynamic loader is needed to obtain the absolute path of the library.

For executable files in the elf format, this is done by ld-linux.so. It searches for the DT_RPATH of the elf file, then the environment variable LD_LIBRAR_PATH and then the < strong>/etc/ld.so.cacheFile list Go to the /lib/, /usr/lib directories and find The library file is then loaded into memory.

In response to the error reported in the sample code, we first use ldd to detect dependencies:


We found that the system showed that the libcal.so file could not be found.

Since DT_RPATH generally cannot be changed, we can modify the environment variable LD_LIBRARY_PATH first.

We first obtain the absolute path of the dynamic library libcal.so.

1. Modify the environment variable LD_LIBRARY_PATH, which means it is valid for the current terminal


After adding the environment variable at this time, app can execute normally. However, this setting is only effective for the current terminal and is temporary.

I closed this terminal and opened a new terminal, and the previous settings were invalid.

2. Modify the environment variable LD_LIBRARY_PATH, user level

Modify the .bashrc file in the user’s home directory, that is, ~/.bashrc, and add this command: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH: the absolute path of the dynamic library file .

Then refresh the .bashrc file.

source .bashrc

Execute again.


There is no problem in opening a new terminal at this time.

3. Modify the environment variable LD_LIBRARY_PATH, system level

Add the command export LD_LIBRARY_PATH=$LD_LIBRARY_PATH: the absolute path of the dynamic library file in the system configuration /etc/profile.


After saving and exiting, refresh and execute.

4. Modify the /etc/ld.so.cache file

Since /etc/ld.so.cache is a binary file, it cannot be modified.

We can achieve indirect modification by modifying /etc/ld.so.conf. We directly add the absolute path of libcal.so to /etc/ld.so.conf will do.

Then update and execute. Note that the updated command is ldconfig.

5. Static library and dynamic library

The main difference between static libraries and dynamic libraries is from how the linking phase is handled and linked into an executable program. They are called static linking method and dynamic linking method respectively.

1. Static library production process

2. Dynamic library production process

3. The difference between static libraries and dynamic libraries

Static library

Advantages:

  • Static libraries are packaged into applications and loaded faster;
  • There is no need to provide a static library when publishing a program, making it easy to transplant;

Disadvantages:

  • It also consumes system resources, wastes memory, and the executable file size is too bloated;
  • Updating, deploying, and publishing are very troublesome;
Dynamic library

Advantages:

  • Inter-process resource sharing (shared library) is possible;
  • Updates, deployments, and releases are simpler;
  • You can manually control when to load dynamic libraries;

Disadvantages:

  • The speed of loading into memory is not as fast as that of static libraries being packaged into the program;
  • When publishing a program, you need to provide dependent dynamic libraries;