编译C程序的流程

来源:互联网 发布:光明数据 编辑:程序博客网 时间:2024/06/11 00:41

声明环境:

win10 专业版

gcc.exe


以 first.c 程序为模型

#include <stdio.h>int main(void){int num, i;num = 1;printf("I am a simple\n");printf("computer.\n");printf("My favorite number is %d because it is first.\n", num);       return 0;};
进入 CMD 中的 .C 所在目录

1. 执行 gcc -E source.c 对源码进行预处理,若想查看执行过程中援引了哪些文件,可加参数 -v

# 1 "first.c"# 1 "<built-in>"# 1 "<command-line>"# 1 "first.c"# 1 "/usr/include/stdio.h" 1 3 4# 29 "/usr/include/stdio.h" 3 4# 1 "/usr/include/_ansi.h" 1 3 4# 15 "/usr/include/_ansi.h" 3 4# 1 "/usr/include/newlib.h" 1 3 4# 14 "/usr/include/newlib.h" 3 4# 1 "/usr/include/_newlib_version.h" 1 3 4# 15 "/usr/include/newlib.h" 2 3 4# 16 "/usr/include/_ansi.h" 2 3 4# 1 "/usr/include/sys/config.h" 1 3 4typedef long unsigned int __uint64_t;# 134 "/usr/include/machine/_default_types.h" 3 4typedef signed char __int_least8_t;typedef unsigned char __uint_least8_t;# 160 "/usr/include/machine/_default_types.h" 3 4typedef short int __int_least16_t;typedef short unsigned int __uint_least16_t;# 182 "/usr/include/machine/_default_types.h" 3 4typedef int __int_least32_t;typedef unsigned int __uint_least32_t;# 200 "/usr/include/machine/_default_types.h" 3 4typedef long int __int_least64_t;typedef long unsigned int __uint_least64_t;# 214 "/usr/include/machine/_default_types.h" 3 4typedef long int __intmax_t;... 省略 ...
2.执行 gcc -S source.c 对上一步预处理的文件进行编译得到文件 <file.o>,此步仅编译不汇编,可以查看汇编代码。若想查看执行过程中援引了哪些文件,可加参数 -v

.file"first.c".def__main;.scl2;.type32;.endef.section .rdata,"dr".LC0:.ascii "I am a simple\0".LC1:.ascii "computer.\0".align 8.LC2:.ascii "My favorite number is %d because it is first.\12\0".text.globlmain.defmain;.scl2;.type32;.endef.seh_procmainmain:pushq%rbp.seh_pushreg%rbpmovq%rsp, %rbp.seh_setframe%rbp, 0subq$48, %rsp.seh_stackalloc48.seh_endprologuecall__mainmovl$1, -4(%rbp)leaq.LC0(%rip), %rcxcallputsleaq.LC1(%rip), %rcxcallputsmovl-4(%rbp), %eaxmovl%eax, %edxleaq.LC2(%rip), %rcxcallprintfmovl$0, %eaxaddq$48, %rsppopq%rbpret.seh_endproc.ident"GCC: (GNU) 6.4.0".defputs;.scl2;.type32;.endef.defprintf;.scl2;.type32;.endef
3. 执行 gcc -c source.s 对上一步编译的文件进行汇编得到目标代码文件 source.o ,此版本的gcc把 -c 这个参数的编译和汇编合并在了一起,若想查看执行过程中援引了哪些文件,可加参数 -v

488d 0d00 0000 00e8 0000 0000 488d 0d0e0000 00e8 0000 0000 8b45 fc89 c248 8d0d1800 0000 e800 0000 00b8 0000 0000 4883c430 5dc3 9090 9090 9090 9090 4920 616d2061 2073 696d 706c 6500 636f 6d70 75746572 2e00 4d79 2066 6176 6f72 6974 65206e75 6d62 6572 2069 7320 2564 2062 65636175 7365 2069 7420 6973 2066 6972 73742e0a 0000 0000 0000 0000 0000 0108 03050852 0403 0150 0000 0000 0000 4800 00000000 0000 4743 433a 2028 474e 5529 20362e34 2e30 0000 0000 0000 0000 0000 00000000 0000 0900 0000 1200 0000 0400 17000000 0a00 0000 0400 1c00 0000 1300 00000400 2300 0000 0a00 0000 0400 2800 00001300 0000 0400 3400 0000 0a00 0000 04003900 0000 1400 0000 0400 0000 0000 04000000 0300 0400 0000 0400 0000 0300 08000000 0c00 0000 0300 2e66 696c 6500 00000000 0000 feff 0000 6701 6669 7273 742e6300 0000 0000 0000 0000 0000 6d61 696e0000 0000 0000 0000 0100 2000 0201 00000000 0000 0000 0000 0000 0000 0000 00002e74 6578 7400 0000 0000 0000 0100 00000301 4800 0000 0700 0000 0000 0000 00000000 0000 2e64 6174 6100 0000 0000 00000200 0000 0301 0000 0000 0000 0000 00000000 0000 0000 0000 2e62 7373 0000 00000000 0000 0300 0000 0301 0000 0000 00000000 0000 0000 0000 0000 0000 2e72 64617461 0000 0000 0000 0400 0000 0301 47000000 0000 0000 0000 0000 0000 0000 00002e78 6461 7461 0000 0000 0000 0500 00000301 0c00 0000 0000 0000 0000 0000 00000000 0000 2e70 6461 7461 0000 0000 00000600 0000 0301 0c00 0000 0300 0000 00000000 0000 0000 0000 0000 0000 0f00 00000000 0000 0700 0000 0301 1100 0000 00000000 0000 0000 0000 0000 0000 5f5f 6d61696e 0000 0000 0000 0000 2000 0200 70757473 0000 0000 0000 0000 0000 2000 02007072 696e 7466 0000 0000 0000 0000 20000200 1a00 0000 2e72 6461 7461 247a 7a7a002e 7264 6174 6124 7a7a 7a00 
4. 执行 gcc source.o -o <file> 把上一步的目标代码链接库文件,生成可执行文件



除非追究编译过程,通常情况都是采用一步编译,建议加入参数 -Wall 打开常用的警告。

  • 编译C语言
gcc -Wall source.c -o <file>
  • 编译C++语言
g++ -Wall source.c -o <file>
  • 调试C++

g++ -g source.c -o <file>gdb <file>

  • 下图是考拉总结的编译原理图




原创粉丝点击