C language - programming mechanism

The exact procedure for generating a program varies depending on the computer environment. C is a portable language, so it can be used in many environments. 

When writing a program in C language, the written content is stored in a text file, which is called a source code file . Most C systems require the file name to end with .c (such as budget.c). In the file name, the part before the dot (.) is called the base name, and the part after the dot is called the extension. Therefore, budget is the base name and c is the extension. The combination of base name and extension is the file name. The filename should meet the special requirements of the particular computer operating system. For example, some UNIX systems limit the entire filename (including the extension) to no more than 14 characters, while others allow longer filenames, up to 255 characters. Linux, Windows, and Macintosh OS all allow long filenames.

Looking at a specific application, suppose there is a source file named xiaogu.c, in which the C source code is as follows:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    printf("I am XiaoGu! I love C!\n");
    system("pause");    // 防止运行后自动退出,需头文件stdlib.h
    return 0;
}

Object Code Files, Executables, and Libraries 

The basic strategy of C programming is to use a program to convert a source code file into an executable file (which contains machine language code that can be run directly). A typical C implementation does this in two steps, compiling and linking. The compiler converts the source code into intermediate code, and the linker combines the intermediate code with other code to generate an executable file. C uses this divide-and-conquer approach to facilitate modularization of programs, where individual modules can be compiled independently, and the compiled modules can be combined later with a linker. This way, if you only change one module, you don't have to recompile other modules as a result. Additionally, the linker combines the program you write with precompiled library code.

Intermediate files come in many forms. What we describe here is the most common form, which converts source code into machine language code and puts the result in an object code file (or object file for short) (here it is assumed that the source code has only one file). Although the object file contains machine language code, the file cannot be run directly. Because the source code translated by the compiler is stored in the object file, this is not a complete program.

The object code file is missing startup code. The startup code acts as the interface between the program and the operating system. For example, an IBM PC compatible can run under MS Windows or Linux. The same hardware is used in both cases, so the object code is the same, but the startup code required for Windows and Linux is different because of the way those systems handle programs.

The object code also lacks functions. Almost all C programs use functions from the C standard library. For example, the printf() function is used in xiaogu.c. The object code file does not contain the code for this function, it only contains instructions for using the printf() function. The actual code for the printf() function is stored in another file called a library. There are object codes for many functions in the library file.

The role of the linker is to combine the three parts of the object code you wrote, the standard startup code of the system and the library code into one file, which is the executable file. For library code, the linker will only extract the library function code to be used in the program.

In short, both object files and executable files are made up of machine language instructions. However, the object file only contains the machine language code that the compiler translates for the code you write, and the executable file also contains the machine code of the library functions and startup code used in the program you wrote.

Guess you like

Origin blog.csdn.net/weixin_51995147/article/details/128462413
Recommended