“This is the ninth day of my participation in the First Challenge 2022. For details: First Challenge 2022”


I. Header file.h

Function declaration and function definition

Function declaration: it is only a description of the compiler system. It is a description of the information such as the return value type of the defined function. Function declaration only emphasizes the function prototype, and does not open up special memory space for it when executing the declaration.

Function definition: The definition of a function is a complete function unit, including function type, function name, parameter and parameter type, function body, etc. When defining a function, it is equivalent to giving a function entity, and special memory space will be opened during the execution of the definition.

Because the compiler is compiled from top to bottom on the command line, when calling a function, you should include the definition of the function before calling the command. Otherwise, the compiler will miss the function entity and cause an error. However, in the case of a function defined later, but to be used before, we need to give a declaration that tells the compiler the prototype of the “undefined” function, and that the execution (definition) is somewhere else. In the project, some function definitions are not in the project, but may be in another file, in which case the declaration should also be made, and the compiler should look for the function entity elsewhere.

2. Header files

Imagine making multiple declarations in a project, where “extraneous information” takes up space and affects the simplicity and readability of the entire code, hence the introduction of header files.

A header file is an interface specification file that holds function declarations. For example, the piece of code shown in Listing 1 is a typical header function declaration. It declares a function named X_x that returns an int and takes only one int argument. The implementation is in the corresponding source file (see listing 2).

#prama once
#ifndef STATICLIB_H
#define STATICLIB_H
extern "C"
int x_x(int);
#endif
Copy the code
#include "staticLib.h"
int x_x(int x){
	return x*x;
}
Copy the code

The advantage of introducing a header file is that it uses only one line of command such as #include “xx.h” (if the link library adds another line of link commands). Xx.h contains all the function declarations needed for this project, so the subsequent source file can call the function directly without worrying about errors caused by not declaring the function.

Extern “C” is an extern “C” extension of the DEFINITION of functions and global variables in CPP source files. Extern “C” is an extern “C” extension of functions and global variables in CPP source files.

3. Modular programming

If now consider a project, it requires the use of many mathematical formulas and physical function, and then in the main function in the project of the mathematics, physics function definition and calls has many disadvantages: (1) a lot of the function definition and the main function of mixed together, make the main function is to use the mathematics, physics function to achieve the goal of readability. ② As for the two main function branches, mathematics and physics, the definitions of which cross each other are confusing, and it is extremely difficult to improve or maintain the engineering. … Now if you modularize your programming, define one module as a mathematical function and the other as a physical function. These two types of modules are declared in header files and implemented in source files, respectively. Then in the main project, only the introduction of the header files of these two modules can directly call these functions, eliminating the complex function definition and declaration in the main project, so that the main function can be highlighted and read, and maintenance and upgrading of engineering functions only need to modify the header file and part of the source file of the corresponding module. Without having to read the entire project from scratch or make changes to the master project document. It can be said that the introduction of header files is the foundation of modular programming

Link library

In engineering development, applications (.exe) often need to use multiple modules. According to the modular programming method of header files, the corresponding header files and corresponding source files must be imported in the main project to compile. A big problem with this is that the application contains the source code of all the underlying functions. Developers often do not want users to see the implementation methods of these underlying functions during application, so they introduce the concept of library functions and encapsulate the underlying implementation of all functions.

Therefore, library file modular programming presents the header file (function declaration, telling the user how to use the function) and library file (the encapsulation of the underlying function implementation, the user sees the binary file); Header modular programming presents the user with header and source files (concrete implementations of the underlying functions)

Through the comparison, we can see that the library function has the following advantages: ① improve the security and stability of the underlying functions of the application program ② reduce the repeated compilation time and enhance the modularity of the program

Library files are linked into the target program when the program is linked, so they are also called link libraries. In terms of the specific implementation process and characteristics, it can be divided into two types: static link library and dynamic link library

1. Advantages and disadvantages of dynamic and static libraries

DLL Hell: DLL hell occurs when the version of the DLL on the computer is different from the version used when the program was created. DLLS have no built-in mechanism for backward compatibility, and even minor changes to DLLS make their internal structure different from previous versions, and trying to use them often causes applications to crash. Static libraries avoid this problem because the version used to build the application is contained within it, so even if newer versions exist elsewhere on the system, this does not affect the application.

2. Characteristics of dynamic and static libraries

The dynamic library is composed of lib and DLL. Lib is equivalent to the export library of DLL. For the sake of distinction, lib is called the export library and DLL is shared library (dynamic library). DLL dynamic library is a record of the specific implementation process of functions, but is a package of binary files; The exported library records the address symbol table and other information to ensure that the specific execution link in DLL can be found according to lib when the project is executed.

The static library has only the lib file, which at this point also contains the specific execution of the function.

Therefore, references to both static and dynamic libraries need to bridge the lib file. In addition to containing the corresponding header file of the library, the command #pragma comment(lib,”xx.lib”) is used to link the library file

C libraries such as iostream are static, but do not need to use link statements when calling library functions. This is because the compiler links these libraries by default. References to such libraries take the form of #include <xxx.h> to tell the compiler that they are libraries. For custom static libraries, the compiler does not link by default, so the header file takes the form of #include “xxx.h” and the link library is manually configured.

In addition, when dynamic libraries are written, the functions declared in header files are decorated with __declspec(DLlexport) statements so that the entire DLL is exported to the library lib, and the DLL can be used by linking to lib

Welcome to my AI channel “AI Technology Club”.