Compilation and header files

Source: Internet
Author: User
C language. C and. to put it simply, you need to understand the c file and header file (that is. h) What are the differences? First, you need to understand the working process of the compiler. Generally, the compiler will perform the following processes: 1. preprocessing phase
2. Lexical and syntax analysis phase
3. In the compilation phase, compile the statement into a pure Assembly Statement, compile the statement into a CPU-related binary code, and generate each target file (. OBJ file)
4. in the connection phase, each segment of the Code in each target file is located with an absolute address to generate executable files related to a specific platform. Of course, you can use objcopy to generate a pure binary code, that is, the file format information is removed. (Generate. EXE file) the compiler is executed in units of C files during compilation. That is to say, if none of your project's c files exist, your project will not be compiled, the connector is based on the target file. It migrates functions and variables to one or more target files, generates the final executable file, and develops programs on the PC, generally, there is a main function, which is the Convention of each compiler. Of course, if you write a connector script yourself, you don't need the main function as the program entry !!!! (Main. with these basic knowledge, the target file of the C file can be implemented. To generate a final executable file, some target files, that is, the C file, are required, in these C files, another main function is required as the entry to the executable program. We will start with a C file and assume that the content of this c file is as follows:
# Include <stdio. h>
# Include "mytest. H" int main (INT argc, char ** argv)
{
Test = 25;
Printf ("test... % d/N", test );
} The header file content is as follows:
Int test; now we use this example to explain the compiler's work:
1. preprocessing phase: the compiler uses the c file as a unit. First, read the c file and find that the first and second sentences contain a header file, the two files will be searched in all search paths. After the files are found, the corresponding header files will be processed to detect the dependency between macros, variables, function declarations, nested header file inclusion, and so on, macro replacement to check whether repeated definitions and declarations have occurred. Finally, all the things in those files are scanned into the current c file, form an intermediate "c file" 2. in the compilation phase, in the previous step, the test variable in the header file is scanned into a medium C file, and the test variable is changed to a global variable in the file, in this case, all the variables in the intermediate C file, the function allocates space, compiles each function into a binary code, and generates the target file according to the specific target file format, each global variable and function symbol is described in the target file in this format. These binary codes are organized into a target file according to certain standards. 3. in the connection phase, the target files generated in the previous step are connected according to some parameters to generate the final executable files. The main task is Relocate functions and variables of each target file is equivalent to combining the binary code in the target file into a file according to certain specifications, and then returning to the topic of content written by the C file and the header file: in theory, the content in the C file and header file can be written no matter what it is supported by the C language. For example, if you write the function body in the header file, the function can be compiled into a part of the target file as long as any c file contains this header file (the compilation is in the unit of C file, if this header file is not included in any c file, this code will be virtually empty.) You can perform function declaration, variable declaration, and struct declaration in the C file, this is not a problem !!! So why do we need to split the file into a header file and a C file? Why are functions, variable declarations, macro declarations, and struct declarations in the header? What about variable definition and function implementation in file C ?? The reason is as follows:
1. if you implement a function body in the header file, if you reference it in multiple C files and compile and translate multiple C files at the same time, connect the generated target file to an executable file. In each target file generated by the C file that references this header file, there is a code for this function, if this function is not defined as a local function, multiple identical functions will be found during connection and an error will be reported.
2. if a global variable is defined in the header file and an initial value is assigned to the global variable, a copy of the same variable name will also exist in multiple C files that reference this header file, the key is that this variable is assigned an initial value. Therefore, the compiler puts this variable into the data segment. Eventually, multiple Identical variables exist in the Data Segment during the connection phase, it cannot unify these variables into one variable, that is, allocate only one space for the variable, instead of multiple spaces. Assume that the variable has no initial values assigned to the header file, the compiler will put it into the BSS segment, and the connector will allocate only one bucket for multiple variables with the same name in the BSS segment
3. if the macro, struct, and function are declared in file C, I have to repeat the work once to reference the corresponding macro and struct in another file C, if I changed a statement in the C file and forgot to change the statement in other C files, this would not cause a major problem, the logic of the program becomes unimaginable. If you put these public stuff in a header file, if you want to use its c file, you only need to reference one !!! This is not convenient. to modify a declaration, you only need to move the header file.
4. declare struct and functions in the header file. When You Need To encapsulate your code into a library to allow others to use your code, you do not want to publish the source code, how can people use your database? That is, how to use the functions in your database ?? One method is to publish the source code, and others can use it whenever they want to use it. The other is to provide header files. Others can view your function prototype from the original file, just like you call the printf function, what are the parameters in it ?? How do you know ?? I am not looking at the relevant statements in the header file !!! Of course, these things have become the C standard. Even if you don't look at other people's header files, you can also know how to use the header files and source files respectively. First, we can put everything in one. in the CPP file. then the compiler will put this. CPP compiled. what is OBJ and OBJ? It is the compilation unit. A program can be composed of one compilation unit or multiple compilation units. if you don't want to make your source code hard to read, use multiple compilation units. (A function cannot be placed in two compilation units, but more than two functions can be placed in one unit, that is, CPP.) program, that is, the program. if. CPP uses another one. what should I do if a CPP-defined function is used? Only in this. write his function declaration in CPP. the other work is done by the linker. You can call this function at will. the linker connects all OBJ objects, but what if the same function or external variable happens? How does he identify it? Generally, two identical function names or external variable names cannot appear in the same program. fortunately, C ++ can be limited by a keyword called the link property. Your function is shared by the whole program, it is only used in a compilation unit obj. these keywords are extern and static. extern are external links. In addition to this unit, external units can also access this function. static is an internal link and belongs to its own unit. I haven't said it for so long. what is the role of H? Actually no. h can also work well, but when you find a function or external variable with an external link, you need many declarations, because C ++ is a language that uses functions and variables, he must be declared. Why? Only after the declaration can we know his specification and better find out what is not the same as the specification. don't think about a compilation unit. It will automatically get information from another compilation unit and know how you define this function. therefore, as long as the unit of the function is used, a declaration must be written in that. in CPP, this is not very troublesome, and if you want to modify it, you must modify it one by one. this is really unbearable .. h was born to solve this problem. He included these public things. then all. CPP, which can be included in # include. you need to modify the content later. please be careful not to abuse it. H ,. h. Do not write code in it ,. H is not. the CPP repository is full of stuff. if you write code in it. when CPP contains it, it will be repeatedly defined, for example, the function func () {printf}; put it in the header file. h, there are also some. the declaration required by CPP. Then you find B. CPP requires. A function in CPP is very happy to include. h. note: # include is not What is the Application Command? It just copies the content of the specified file. in fact,. CPP and B. CPP has the definition of a func () function. if this function is static with internal links, it will be a waste of space. If it is extern, external links (this is the default situation ), the connector will give you a connection error without mercy, as the function of the same name cannot appear in the same program!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.