声明环境:
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 4
typedef long unsigned int __uint64_t;
# 134 "/usr/include/machine/_default_types.h" 3 4
typedef signed char __int_least8_t;
typedef unsigned char __uint_least8_t;
# 160 "/usr/include/machine/_default_types.h" 3 4
typedef short int __int_least16_t;
typedef short unsigned int __uint_least16_t;
# 182 "/usr/include/machine/_default_types.h" 3 4
typedef int __int_least32_t;
typedef unsigned int __uint_least32_t;
# 200 "/usr/include/machine/_default_types.h" 3 4
typedef long int __int_least64_t;
typedef long unsigned int __uint_least64_t;
# 214 "/usr/include/machine/_default_types.h" 3 4
typedef long int __intmax_t;
... 省略 ...
2.执行 gcc -S source.c 对上一步预处理的文件进行编译得到文件 <file.o>,此步仅编译不汇编,可以查看汇编代码。若想查看执行过程中援引了哪些文件,可加参数 -v
.file "first.c"
.def __main; .scl 2; .type 32; .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
.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 $48, %rsp
.seh_stackalloc 48
.seh_endprologue
call __main
movl $1, -4(%rbp)
leaq .LC0(%rip), %rcx
call puts
leaq .LC1(%rip), %rcx
call puts
movl -4(%rbp), %eax
movl %eax, %edx
leaq .LC2(%rip), %rcx
call printf
movl $0, %eax
addq $48, %rsp
popq %rbp
ret
.seh_endproc
.ident "GCC: (GNU) 6.4.0"
.def puts; .scl 2; .type 32; .endef
.def printf; .scl 2; .type 32; .endef
3. 执行 gcc -c source.s 对上一步编译的文件进行汇编得到目标代码文件 source.o ,此版本的gcc把 -c 这个参数的编译和汇编合并在了一起,若想查看执行过程中援引了哪些文件,可加参数 -v
488d 0d00 0000 00e8 0000 0000 488d 0d0e
0000 00e8 0000 0000 8b45 fc89 c248 8d0d
1800 0000 e800 0000 00b8 0000 0000 4883
c430 5dc3 9090 9090 9090 9090 4920 616d
2061 2073 696d 706c 6500 636f 6d70 7574
6572 2e00 4d79 2066 6176 6f72 6974 6520
6e75 6d62 6572 2069 7320 2564 2062 6563
6175 7365 2069 7420 6973 2066 6972 7374
2e0a 0000 0000 0000 0000 0000 0108 0305
0852 0403 0150 0000 0000 0000 4800 0000
0000 0000 4743 433a 2028 474e 5529 2036
2e34 2e30 0000 0000 0000 0000 0000 0000
0000 0000 0900 0000 1200 0000 0400 1700
0000 0a00 0000 0400 1c00 0000 1300 0000
0400 2300 0000 0a00 0000 0400 2800 0000
1300 0000 0400 3400 0000 0a00 0000 0400
3900 0000 1400 0000 0400 0000 0000 0400
0000 0300 0400 0000 0400 0000 0300 0800
0000 0c00 0000 0300 2e66 696c 6500 0000
0000 0000 feff 0000 6701 6669 7273 742e
6300 0000 0000 0000 0000 0000 6d61 696e
0000 0000 0000 0000 0100 2000 0201 0000
0000 0000 0000 0000 0000 0000 0000 0000
2e74 6578 7400 0000 0000 0000 0100 0000
0301 4800 0000 0700 0000 0000 0000 0000
0000 0000 2e64 6174 6100 0000 0000 0000
0200 0000 0301 0000 0000 0000 0000 0000
0000 0000 0000 0000 2e62 7373 0000 0000
0000 0000 0300 0000 0301 0000 0000 0000
0000 0000 0000 0000 0000 0000 2e72 6461
7461 0000 0000 0000 0400 0000 0301 4700
0000 0000 0000 0000 0000 0000 0000 0000
2e78 6461 7461 0000 0000 0000 0500 0000
0301 0c00 0000 0000 0000 0000 0000 0000
0000 0000 2e70 6461 7461 0000 0000 0000
0600 0000 0301 0c00 0000 0300 0000 0000
0000 0000 0000 0000 0000 0000 0f00 0000
0000 0000 0700 0000 0301 1100 0000 0000
0000 0000 0000 0000 0000 0000 5f5f 6d61
696e 0000 0000 0000 0000 2000 0200 7075
7473 0000 0000 0000 0000 0000 2000 0200
7072 696e 7466 0000 0000 0000 0000 2000
0200 1a00 0000 2e72 6461 7461 247a 7a7a
002e 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>
- 下图是考拉总结的编译原理图