GCC compiles methods that use both dynamic and static link libraries

  • 2020-04-01 21:37:11
  • OfStack

1 library classification

According to the different link period, there are static libraries and dynamic libraries.

Static libraries are linked during the linking phase (nonsense, but it is), so the generated executable is unaffected by the library, and the program can run successfully even if the library is deleted.

Unlike static libraries, dynamic libraries are linked during program execution. So, even after the program has compiled, the library must remain on the system for the program to call at runtime. (TODO: what exactly does the link phase do when linking dynamic libraries)

Comparison of static and dynamic libraries

A linked static library is a kind of paste and copy in a sense, except that it operates on object code rather than source code. Because static libraries are linked and then embedded directly in the executable, this presents two problems.

The first is that system space is wasted. This is obvious. Imagine that if multiple programs linked to the same library, there would be a copy of the library for each generated executable, which would waste system space.

Moreover, people are not saints, and even a well-tuned library is bound to make mistakes. Once a bug is found in the library, it is more troublesome to save it. Programs that link to the library must be found and recompiled.

The appearance of dynamic library makes up for the above disadvantages of static library. Because the dynamic library is linked while the program is running, only one copy is kept on disk, thus saving disk space. If you find a bug or need to upgrade, you can simply replace it with a new library.

So, are static libraries useless?

Answer a yue: not also not also. There is a saying: existence is rationality. Static library since there is no annihilation in the flood of history, it is bound to have its use. Imagine this: what if you wrote a program with the libpcap library, and it had to be run by someone who didn't have the pcap library on his system? The easiest way to do this is to compile the program by linking all the libraries to their static libraries so that you can run the program directly on someone else's system.

Because dynamic libraries are linked while the program is running, the program must be slower than the version of the linked static library. However, the disadvantages of dynamic libraries are negligible relative to their benefits in today's hardware, so linkers generally prefer to link to dynamic libraries when linking, unless the static library is specified with the -static parameter.

Dynamic link library

1. Create dynamic link libraries


#include<stdio.h>
void hello()
{
  printf("hello world/n");
}

Compile to a dynamic library with the command gcc-shared hello.c-o libhello.so. As you can see, there is an extra file, libhello.so, in the current directory.

2. Edit another test file, test.c, as follows


#include<stdio.h>
int main()
{
  printf("call hello()");
  hello();
}

Compile GCC test.c-lhello
The -l option tells the compiler to use the hello library. The odd thing is that the name of the dynamic library is libhello.so, and here we use hello.
But that doesn't work. The compilation will go wrong.

In the function ` main ':
Test.c :(.text+0x1d): undefined reference to 'hello'
Collect2: ld returned 1 exit status
This is because the hello library is in our own path and the compiler cannot find it.
You need to use the -l option to tell the location of the hello library
GCC test. C-lhello-l.-o test
-l. Tells the compiler to look for library files in the current directory

3. Execute./test after successful compilation, still error

Said I couldn't find the library

There are two ways:

First, you can add the current path to /etc/ld.so. Conf and then run ldconfig, or run ldconfig with the current path as the parameter (root permission is required).

Add the current path to the environment variable LD_LIBRARY_PATH

Of course, if you don't think it will cause confusion, you can simply copy the library to /lib,/usr/lib/, etc. (inevitably, this also requires permissions) so that the linker and loader can find the library exactly.

We adopt the second method:
Export LD_LIBRARY_PATH =. : $LD_LIBRARY_PATH
In this way, the execution is successful.

Let's talk more about static link libraries

Use the same hello.c and test.c.
1. Gcc-c hello.c note that the -shared option is not used
2. Document your goals       Ar - r libhello. A hello. O
      The program ar creates a new library called libhello.a with the -r parameter and inserts the object files listed on the command line. In this way, parameter -r creates a new library if the library does not exist, and replaces the original module with a new one if the inventory is available.
3. Link static libraries in programs
                    GCC test. C - lhello-l.-static -o hello.static
Or     C libhello.a - l. -o hello.static

The generated hello.static is no longer dependent on libhello.a

Two useful commands

The file program is used to determine the file type, under the file command, all the files will be exposed.
One trick, by the way. Sometimes in the Windows browser to download tar.gz or tar.bz2 file, the suffix name will become a strange tar.tar, to Linux some novice do not know how to unpack. But the file type under Linux is not affected by the file suffix, so we can first look at the file type with the command file xxx.tar.tar, and then unzip it with tar and the appropriate parameters.

In addition, you can use the program LDD utility to determine.
LDD is used to print information about all the dynamic libraries linked by the target program (specified by the command line parameter). If the target program does not link to the dynamic libraries, it prints "not a dynamic executable." see manpage for the use of LDD.


Related articles: