My whole stack of the road -C basis of the C language program run flow
4.1 C program running processes
Programs written in C language must pass the written source files -> Pretreatment -> compile -> compile -> run these processes after the link.
- Pretreatment
Pretreatment mainly macro substitution, include headers and conditional compilation extension memo to delete the pre-compilation stage is generally generated file .i. - Compiler
Compiler mainly to good pre-generated file compilation of documents, compiled file extension is usually .s. - Compilation
compilation compilation of documents generated mainly to a binary file, the file extension after the compilation is usually .o. - Links
Links are mainly individual binary files, library functions, startup code executable file.
Then write the source program_running_process.c, and then combine the various options GCC compiler on Windows implementations provided pre-compiler, assembler, linker, on GCC installation and configuration can refer to the
Visual Studio Code for content Hello World section.
#include <stdio.h>
//定义浮点型的常量
#define PI 3.14
/*
C语言程序的运行流程
@author tony [email protected]
@version 2019/08/20
*/
int main(int argc, char* argv[]) {
printf("PI =%lf\n", PI);
return 0;
}
Pretreatment using gcc -E option can be realized, which represents the name of the output file -o
D:\workspace\c\visualstuido2019\c-core-programming\c-core-programming-foundational\c-core-programming-foundational>gcc -E program_runing_process.c -o program_runing_process.i
After the pretreatment, the resulting file size is 54kb program_running_process.i
When preprocessing is complete, the defined constants are replaced by actual values, and the contents of the file header contains #include also copied to the pre-generated files
Compiling by gcc -S option
D:\workspace\c\visualstuido2019\c-core-programming\c-core-programming-foundational\c-core-programming-foundational>gcc -S program_runing_process.i -o program_runing_process.s
Compiler will generate an assembly file
.file "program_runing_process.c"
.text
.def __main; .scl 2; .type 32; .endef
.section .rdata,"dr"
.LC1:
.ascii "PI =%lf\12\0"
.text
.globl main
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
subq $32, %rsp
.seh_stackalloc 32
.seh_endprologue
movl %ecx, 16(%rbp)
movq %rdx, 24(%rbp)
call __main
movq .LC0(%rip), %rax
movq %rax, %rdx
movq %rdx, %xmm1
movq %rax, %rdx
leaq .LC1(%rip), %rcx
call printf
movl $0, %eax
addq $32, %rsp
popq %rbp
ret
.seh_endproc
.section .rdata,"dr"
.align 8
.LC0:
.long 1374389535
.long 1074339512
.ident "GCC: (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 7.3.0"
.def printf; .scl 2; .type 32; .endef
Compiled by gcc -c option to achieve
D:\workspace\c\visualstuido2019\c-core-programming\c-core-programming-foundational\c-core-programming-foundational>gcc -c program_runing_process.s -o program_runing_process.o
Link gcc does not provide options here as long as the implementation of gcc program_running_process.o -o program_running_process.exe will link an executable file
D:\workspace\c\visualstuido2019\c-core-programming\c-core-programming-foundational\c-core-programming-foundational>gcc program_runing_process.o -o program_runing_process.exe
Executable file extension .exe in windows by default, of course gcc compiler does not care about the suffix, if you install ubuntu or macOS the gcc, the C compiler can also generate executable .exe suffix.