On Linux's library files

  • 2020-06-15 10:58:15
  • OfStack

Recently, when using the third library Protobuf under Linux, I encountered a problem: the executable program was running incorrectly: "error while shared libraries: libprotobuf.so.7: cannot open object file: No file or directory". So take the time to figure out why, find a solution, and share it with everyone.

1. What is a library

There are a lot of libraries in windows platform and linux platform.

Essentially, a library is a binary form of executable code that can be loaded into memory by the operating system for execution.

Because of the nature of windows and linux, the base 2 of a 2-library is incompatible.

2. Types of libraries

There are two types of libraries under linux: static libraries and Shared libraries (dynamic libraries).

The difference between the two is the time the code is loaded.

The code for the static library is already loaded into the executable during compilation, so it is bulky. The code for the Shared library is loaded into memory when the executable runs, and is simply referenced during compilation, so the code is small. For the choice of static and dynamic libraries, it is necessary to consider the advantages and disadvantages of both. Generally speaking, more common libraries should be Shared libraries.

3. The meaning of inventory

Libraries are existing, mature, reusable code that someone else has written that you can use but remember to abide by the license.

In reality, every program depends on many basic low-level libraries, and it is impossible for everyone to start from scratch, so the existence of libraries makes a lot of sense. The advantage of Shared libraries is that if different applications call the same library, only one instance of the Shared library needs to be in memory.

4. How are library files generated under linux

The static library suffix is.a, which is produced in two steps

Step 1: Generates a heap of.o, each.o contains the symbol table for this compilation unit;
The Step 2: ar command converts a lot of.o to.a and becomes a static library.

The dynamic library suffix is.so, which is generated by the compilation of gcc with specific parameters.

For example: $gcc-ES86en-c *.c $gcc-ES90en-ES91en, -ES92en, libfoo. 1-o libfoo. so. 1.0 *.

5. How are library files named? Is there any specification

Under linux, library file 1 is generally placed under /usr/lib and /lib,

The name of the static library is generally ES111en. a, where xxxx is the name of the lib

Dynamic library name 1 kind for libxxxx so. major. minor, xxxx is the title of the lib major is the major version number, minor is the version number

6. How do I know which libraries an executable program depends on

The ldd command looks at a Shared library that an executable depends on,

For example # ldd/bin/lnlibc. so. 6
= > / lib libc. so. 6 (0 x 40021000)/lib/ld - linux. so. 2
= > / lib/ld - linux. so. 2 (0 x 40000000)

You can see that the ln command relies on the libc library and the ld-ES163en library

7. How do executables locate Shared library files while executing

Static library: When the executable is generated, the static library has been linked into the executable as part 1 of itself, so the execution does not need to be repositioned, that is, it does not depend on the library file any more.

Dynamic library: Need to know the path of dynamic library, refer to another blog;

8. How can the system find a new library after installing it

If installed under /lib or /usr/lib, ld will be found by default, with no additional operations.

If the installation in other directories, need to add it to the/etc/ld so. cache file, the steps are as follows:

1. Edit/etc/ld. so. conf files, to join the library files directory path;

2. Run ldconfig, the command will rebuild/etc/ld so. cache file;

3. ldconfig command requires root permission;

conclusion

That's all about the Linux library file in this article, and I hope it will be helpful for you to learn Linux. Docker USES Linux iptables and Interfaces to manage container networks. If you have any questions, please feel free to leave a message. This site will reply to you in time.


Related articles: