Hello, I’m interested in how the libraries work, namely: what .DLL library files are used for and how they are associated with .lib files that are placed in the compiler's lib folder, with * .h files that are put into the project itself, why you can't just add .h file in the project? I ask you to explain, because I myself do not understand this, I'm sorry if the question has been raised is stupid.

    2 answers 2

    Let's start with the basics :) Initially, the compiler turns the source file into an intermediate .obj format (the so-called object file), there is nothing in it except the machine code of the program and links to external library functions (links are obtained from header files). As a rule, there is no code in the header files, there are only declarations ("headings") of the procedures and functions. Further, in order to turn an object file into an executable, it needs to be fed to the linker, but the linker also needs .lib files that contain already compiled functions that are referenced in the object file. The linker connects all this household, and it turns out a ready-made executable file in which there is both the program code and the code of the functions being called.

    Lib files are called static libraries, because they can be attached to the program only at compile time. Also, there are dynamic (dynamically connected) libraries (dinamiс-link libraries, dll), unlike static libraries, they are connected either at the moment of launching the program or after it has already been launched. In order to connect the dll at the time of launch, the static library lib must be linked to the program, which has links to the functions of the dll library. Only these function references are added to the executable file itself, but not the functions from the dll itself.

    • one
      Well, a small addition. The object files still contain the information necessary for reconfiguring addresses (relocatability table) for recalculating address constants, depending on the location of the code. In general, after linking, the program and / or dll should be loaded to a strictly defined address. There are means to avoid this. Such a code is called a position independent code (PIC). In linux, all dll have a PIC sign (in Windows - no) - alexlz
    • In dll for these purposes, the relocation table is used. - insolor
    • Aha, and some (for example, cygwin developers) have already come across the fact that during operation the system has to move dll to new addresses (when libraries that use the same addresses are used at the same time) - alexlz
    • That is, if I have a third-party DLL and LIB, are there three ways !? - 1) Use LIB on compilation stages, at which library functions will join the project code !? 2) Use DLL functions which will be called dynamically when the program is running !? 3) Use both LIB and DLL to load the library when loading my application !? - rejie
    • Usually, those libs that come with a dll are needed just for linking the dll, and there are no functions in them. The glut is done that way. Therefore, there are only two ways: link lib to access dll functions, or link dll functions explicitly while the program is running . - insolor

    The .dll is the dynamic-link library (dynamic link library), contains the compiled code provided by the library functions.

    .lib - these static libraries (static libraries) are an archive of a set of object files, each of which is the result of compiling one source file (.c, .cpp).

    .h files usually contain only declarations of functions and constants provided by the library. This is enough to compile code using library functions. At the output of the compiler, one or more object files are obtained again.

    In order to get an executable file from the object files, they (the object files) need to be linked. In the linking process, calls to library functions are resolved either by inserting the implementation from static libraries into the executable file, or by adding links to the corresponding. implementations in dynamic libraries.

    More detailed description