C/C++ | Introduction to Internet of Things Development + Project Combat | C Language Basics | C Language Preprocessing and Compilation Process Analysis - Study Notes (2)


Excerpt from: Wheat Academy - C Language Programming and Quick Start

C Language Preprocessing and Compilation Process Analysis

gcc -v -o build 1.c

output content

C:\Users\Vera\Desktop>gcc -v -o build 1.c
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=f:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/4.7.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../build/gcc/src/configure --target=x86_64-w64-mingw32 --prefix=/c/bb/vista64-mingw32/mingw-x86-x86_64/build/build/root --with-sysroot=/c/bb/vista64-mingw32/mingw-x86-x86_64/build/build/root --enable-languages=all,obj-c++ --enable-fully-dynamic-string --disable-multilib
Thread model: win32
gcc version 4.7.0 20111220 (experimental) (GCC)
COLLECT_GCC_OPTIONS='-v' '-o' 'build.exe' '-mtune=generic' '-march=x86-64'
 f:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/4.7.0/cc1.exe -quiet -v -iprefix f:\anaconda3\pkgs\mingw-4.7-1\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/ -U_REENTRANT 1.c -quiet -dumpbase 1.c -mtune=generic -march=x86-64 -auxbase 1 -version -o C:\Users\Vera\AppData\Local\Temp\ccRFZVtk.s
GNU C (GCC) version 4.7.0 20111220 (experimental) (x86_64-w64-mingw32)
        compiled by GNU C version 4.5.2, GMP version 5.0.1, MPFR version 2.4.2, MPC version 0.8.2
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
ignoring duplicate directory "f:/anaconda3/pkgs/mingw-4.7-1/mingw/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/4.7.0/include"
ignoring nonexistent directory "c:/bb/vista64-mingw32/mingw-x86-x86_64/build/build/rootc:/bb/vista64-mingw32/mingw-x86-x86_64/build/build/root/lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../include"
ignoring duplicate directory "f:/anaconda3/pkgs/mingw-4.7-1/mingw/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/4.7.0/include-fixed"
ignoring duplicate directory "f:/anaconda3/pkgs/mingw-4.7-1/mingw/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include"
ignoring nonexistent directory "c:/bb/vista64-mingw32/mingw-x86-x86_64/build/build/rootc:/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
 f:\anaconda3\pkgs\mingw-4.7-1\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include
 f:\anaconda3\pkgs\mingw-4.7-1\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/include-fixed
 f:\anaconda3\pkgs\mingw-4.7-1\mingw\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include
End of search list.
GNU C (GCC) version 4.7.0 20111220 (experimental) (x86_64-w64-mingw32)
        compiled by GNU C version 4.5.2, GMP version 5.0.1, MPFR version 2.4.2, MPC version 0.8.2
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
Compiler executable checksum: 6dafe883c1f59de430b201ea1dfaaae8
COLLECT_GCC_OPTIONS='-v' '-o' 'build.exe' '-mtune=generic' '-march=x86-64'
 f:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/bin/as.exe -o C:\Users\Vera\AppData\Local\Temp\ccaZqlwM.o C:\Users\Vera\AppData\Local\Temp\ccRFZVtk.s
COMPILER_PATH=f:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/4.7.0/;f:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../libexec/gcc/;f:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/bin/
LIBRARY_PATH=f:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.7.0/;f:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../lib/gcc/;f:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/lib/../lib/;f:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'build.exe' '-mtune=generic' '-march=x86-64'
 f:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/4.7.0/collect2.exe --sysroot=c:/bb/vista64-mingw32/mingw-x86-x86_64/build/build/root -m i386pep -Bdynamic -o build.exe f:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/lib/../lib/crt2.o f:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/lib/../lib/crtbegin.o -Lf:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.7.0 -Lf:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../lib/gcc -Lf:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/lib/../lib -Lf:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/lib C:\Users\Vera\AppData\Local\Temp\ccaZqlwM.o -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_eh -lgcc -lmoldname -lmingwex -lmsvcrt f:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/lib/../lib/crtend.o

The main steps

preprocessing (equivalent to replacement)

cpp -o build.i 1.c is equivalent to gcc -E -o build.i 1.c

example

Modify 1.c and add define:

C:\Users\Vera\Desktop>type 1.c
#include <stdio.h>
#define POLICE  110
int main()
{
    int a =POLICE;
    printf("hello world! 你好\n");
    return 0;
}

C:\Users\Vera\Desktop>gcc -E -o build.i 1.c
displays the content of build.i :

C:\Users\Vera\Desktop>type build.i

output value:

# 1 "1.c"
# 1 "<command-line>"
# 1 "1.c"
# 1 "f:\\anaconda3\\pkgs\\mingw-4.7-1\\mingw\\bin\\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/stdio.h" 1 3
# 9 "f:\\anaconda3\\pkgs\\mingw-4.7-1\\mingw\\bin\\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/stdio.h" 3
# 1 "f:\\anaconda3\\pkgs\\mingw-4.7-1\\mingw\\bin\\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/_mingw.h" 1 3
# 10 "f:\\anaconda3\\pkgs\\mingw-4.7-1\\mingw\\bin\\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/_mingw.h" 3
# 1 "f:\\anaconda3\\pkgs\\mingw-4.7-1\\mingw\\bin\\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/_mingw_mac.h" 1 3
# 32 "f:\\anaconda3\\pkgs\\mingw-4.7-1\\mingw\\bin\\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/_mingw_mac.h" 3

# 41 "f:\\anaconda3\\pkgs\\mingw-4.7-1\\mingw\\bin\\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/_mingw_mac.h" 3

# 11 "f:\\anaconda3\\pkgs\\mingw-4.7-1\\mingw\\bin\\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/_mingw.h" 2 3
# 293 "f:\\anaconda3\\pkgs\\mingw-4.7-1\\mingw\\bin\\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/_mingw.h" 3
# 1 "f:\\anaconda3\\pkgs\\mingw-4.7-1\\mingw\\bin\\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/vadefs.h" 1 3
# 13 "f:\\anaconda3\\pkgs\\mingw-4.7-1\\mingw\\bin\\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/vadefs.h" 3
# 1 "f:\\anaconda3\\pkgs\\mingw-4.7-1\\mingw\\bin\\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/_mingw.h" 1 3
# 739 "f:\\anaconda3\\pkgs\\mingw-4.7-1\\mingw\\bin\\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/_mingw.h" 3
# 1 "f:\\anaconda3\\pkgs\\mingw-4.7-1\\mingw\\bin\\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/sdks/_mingw_directx.h" 1 3
......
# 1025 "f:\\anaconda3\\pkgs\\mingw-4.7-1\\mingw\\bin\\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../x86_64-w64-mingw32/include/stdio.h" 2 3
# 2 "1.c" 2

int main()
{
    int a =110;
    printf("hello world! 你好\n");
    return 0;
}

1. The output content above the main program body is the header file part added during precompilation, and the compiler automatically adds it;
2. You can see that the macro name POLICE has been replaced by 110. define and include are executed at this stage, so they are not keywords in essence (keywords should be processed by the compiler).

compile

The first invoked command : f:/anaconda3/pkgs/mingw-4.7-1/mingw/bin/…/libexec/gcc/x86_64-w64-mingw32/4.7.0/cc1.exe … 1.c … -o \ccRFZVtk.s**

cc1.exe is called a compiler, which is equivalent to gcc -S 1.c ccRFZVtk.s

Assembly (let the cpu know the code)

The command called by the second article : x86_64-w64-mingw32/bin/as.exe …-o \ccaZqlwM.o…\ccRFZVtk.s

The output file ccRFZVtk.s of the previous step is used as the input file this time, and the \ccaZqlwM.o file is output after executing as.exe.
The operation performed by as.exe is equivalent to gcc -c, that is, compile first and then assemble, because 1.c cannot be directly assembled, that is, the -c option directly generates .o.

linker (link system standard library)

The command called by the third article : /x86_64-w64-mingw32/4.7.0/collect2.exe -o build.exe multiple .o files

The operation of collect2.exe is equivalent to gcc -o, which already includes the operations of -S and -c.

example

C:\Users\Vera\Desktop>gcc -S -o build.s 1.c

C:\Users\Vera\Desktop>dir/w
The volume in drive C is the OS
volume and the serial number is 70C8-B66E

C:\Users\Vera\Desktop directory

[.] […] 1.c build.s

C:\Users\Vera\Desktop>notepad build.s
generates x86 assembly code here:

C:\Users\Vera\Desktop>gcc -c -o build.o build.s

C:\Users\Vera\Desktop>dir/w
The volume in drive C is the OS
volume and the serial number is 70C8-B66E

C:\Users\Vera\Desktop directory

[.] […] 1.c build.o build.s

C:\Users\Vera\Desktop>notepad build.o
generates binary code here.
C:\Users\Vera\Desktop>gcc -o build 1.c

C:\Users\Vera\Desktop>dir/w
The volume in drive C is the OS
volume and the serial number is 70C8-B66E

C:\Users\Vera\Desktop directory

[.] […] 1.c build.exe build.o build.s

C:\Users\Vera\Desktop>build
hello world!

Guess you like

Origin blog.csdn.net/Medlar_CN/article/details/130063131