C header files
The header file is a file with the extension .h, which contains C function declarations and macro definitions and is referenced and shared by multiple source files. There are two types of header files: header files written by the programmer and header files provided by the compiler.
To use a header file in a program, you need to use the C preprocessing directive #include to reference it. We have already seen the stdio.h header file before, which is the header file that comes with the compiler.
Referring to the header file is equivalent to copying the contents of the header file, but we will not directly copy the contents of the header file in the source file, because it is easy to make mistakes, especially when the program is composed of multiple source files. when.
A simple practice in C or C++ programs, it is recommended to write all constants, macros, system global variables and function prototypes in header files, and reference these header files at any time when needed.
Syntax for referencing header files
Use preprocessing directives #include User and system header files can be referenced. It has the following two forms:
#include <file>
This form is used to reference system header files. It searches the standard list of system directories for a file named file. When compiling source code, you can prepend directories to the list with the -I option.
#include "file"
This form is used to reference user header files. It searches the directory containing the current file for a file named file. When compiling source code, you can prepend directories to the list with the -I option.
Operations to reference header files
#include directive instructs the C preprocessor to browse the specified file as input. The output of the preprocessor includes the generated output, the output generated by the referenced file, and the text output after the #include directive. For example, if you have a header file header.h, as follows:
char *test (void);
and a main program that uses the header file program.c, as follows:
int x;#include "header.h"int main (void){ puts (test ());}
compiler You will see the following token flow:
int x;char *test (void);int main (void){ puts (test ());}
Only reference the header file once
If a header file is referenced twice, the compiler will process the contents of the header file twice, which will generate mistake. To prevent this, standard practice is to put the entire contents of the file in a conditional compilation statement, as follows:
#ifndef HEADER_FILE#define HEADER_FILE the entire header file file#endif
This structure is commonly known as a wrapper #ifndef. When the header file is referenced again, the condition is false because HEADER_FILE is defined. At this point, the preprocessor skips the entire contents of the file and the compiler ignores it.
Conditional Reference
Sometimes it is necessary to select a reference from multiple different header files to the program. For example, you need to specify configuration parameters for use on different operating systems. You can achieve this through a series of conditions, as follows:
#if SYSTEM_1 # include "system_1.h"#elif SYSTEM_2 # include "system_2.h"#elif SYSTEM_3 ...#endif
But if there are many header files, this is very inappropriate. The preprocessor uses macros to define the names of header files. This is the so-called Conditional Reference. Instead of using the name of the header file as a direct argument to #include, you just need to use the macro name instead:
#define SYSTEM_H "system_1.h" ... #include SYSTEM_H
SYSTEM_H will be expanded and the preprocessor will look for system_1.h, Just like #include was originally written. SYSTEM_H can be defined by your Makefile via the -D option.