C++ learning-how C/C++ programs are executed

As we all know-when we are studying, the textbook is written like this. The execution of a program requires four steps::

The source file goes through the preprocessing stage -> compilation stage -> assembly stage -> link stage -> executable

Here is an ordinary example to describe my doubts and what I have learned.

#include <stdio.h>
int main()
{
    printf("hello");
    return 0;
}

1. The pretreatment stage.

The preprocessor modifies the original C program according to the command beginning with the character #. For example, the #include <stdio.h> command in the first line of hello.c tells the preprocessor to read the content of the system header file and insert it directly into the program text. The result is another file, usually with a dot i as the file extension.

#include <> Retrieve the file directory directly according to the system standard method.

#include "" Search in the current source file directory, if not found, then search other file directories according to the system standard method.

2. Compilation stage.

The compiler translates the text file hello.i into a text file hello.s, which contains an assembly language program.

3. The compilation stage.

The assembler translates hello.s into machine language instructions, packs these instructions into a format called a relocatable object program, and saves the result in the object file hello.o, which is a binary file.

4. Link phase

The hello program calls the printf function. The printf function exists in a separate pre-compiled file named printf.o, and this file needs to be merged into hello.o in some form, and the linker is responsible for the merge here.

The figure below is a graphical representation of the above four stages.

There is a problem,

In the preprocessing stage, after loading #include<stdio.h>, there is a declaration of the printf function. In the subsequent linking stage, how to know the implementation location of printf and then link. ? ? (How to know the implementation location of the function prototype from the header file)

Guess you like

Origin blog.csdn.net/JACKSONMHLK/article/details/112105915