How does the header file point to the library?

I started getting acquainted with make and asked myself this question.. For example, there is a connected header: #include<stdio.h> In it, as I understand it, there are prototypes of functions, and the functions themselves are compiled into object files and collected in a library, for example,". a". The linker sees the title bar, goes to /usr/lib . But how does he see the right library ? Iterates through everything ?

Author: Alice Smith, 2018-08-15

3 answers

In the traditional implementation, the linker does not see the necessary library in any way. It is your task to manually "feed" the appropriate library to the linker during linking. However, the standard C library is usually fed to the linker automatically, without your participation, i.e. it is for #include <stdio.h> that you usually do not need to do anything else.

In some implementations, you can still specify inside the header file which library you want to connect. For example, in MS Visual Studio does this by specifying

#pragma comment(lib, "something.lib")

Inside the header file. In this case, after including the header file, the corresponding library will connect "itself"at the linking stage.

The linker sees the title bar and goes to /usr/lib...

The linker usually can't see any "title bar" anymore. These "header lines" disappear without a trace even at the preprocessing stage, even before the actual compilation begins. Up to the linker already you get compiled object files that don't have any" header lines " anymore.

The implementation of things like #pragma comment(lib, is based on special fields in the object file format.

 5
Author: AnT, 2018-08-15 18:28:08

When you compile in C, one library is automatically linked. It connects live when the program starts. I have this library like this: /lib64/libc-2.22.so. You must connect all other libraries manually. For example, gcc -lrt ... connects /lib64/librt-2.22.so for parallel threads. It will also be connected live. The same is true only through the links: /usr/lib64/libc.so and /usr/lib64/librt.so.

 0
Author: AlexGlebe, 2018-08-15 17:46:33

The linker sees the title bar and goes to /usr/lib

Of course not. As already answered, at the linking stage, there are no #include operators, and the header files themselves are no longer there

But how does he see the right library ?

Since we are talking about linux and make, you can answer this from the gcc command line:

  1. The key -l (this is the letter "el malaya") specifies the file name of the library you are using. The prefix lib and the suffix so can be omitted. So if you use the math library libm.so, then the key should be written like this: -lm
  2. The key -L specifies the DIRECTORY where your libraries are located. If they are located in standard directories (/lib, /usr/lib, ...) then you do not need to use this key. If your libraries are located in the "left" directories, then they must be specified. A typical situation where the author of the program wants to place the libraries in the same place where the executable module is located. Then you should write like this: -L.
 0
Author: Sergey, 2018-08-16 04:43:22