使用gcc编译C程序详解(Windows系统)

目录

预处理(Preprocessing)

编译(Compiling)

汇编(Assembling)

链接(Linking)

简化编译链接流程


翻译:编译+链接;编译包括:预处理,编译,汇编三个步骤。

预处理(Preprocessing)

指令 gcc -E use_gcc.c -o use_gcc.i
实现 执行以#开头的指令。头文件被包含进来;宏常量和宏代码段被替换;注释被去掉。
注:预处理阶段不检查语法错误; gcc -E是使得源文件编译到预处理阶段停下来;-o use_gcc.i指定生成文件的名称。

 

 use_gcc.c

//一个程序
#include <stdio.h>
#include <stdlib.h>
#define MONTHS 12
#include "Test.h"
int main(void)
{
	int days[MONTHS] = { 31, 28,[4] = 31, 30, 31, [1] = 29 };
	int i;
	for (i = 0; i < MONTHS; i++)
		printf("%2d %d\n", i + 1, days[i]);
	MONTHS;                            //宏替换代码段
	CODE;
	DoNothing();
	getchar();
	return 0;
}

 Test.h

	#define N 1
	
	#define     CODE      if(1)                              \
	                                {                            \
	                                    printf("ccc\n");       \
	                                }
	
	void DoNothing(void);           

use_gcc.i(省略部分代码)

//头文件stdio.h的展开
# 1 "use_gcc.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "use_gcc.c"

# 1 "e:\\mingw\\include\\stdio.h" 1 3
# 38 "e:\\mingw\\include\\stdio.h" 3

...
...

//头文件stdlib.h的展开
# 3 "use_gcc.c" 2
# 1 "e:\\mingw\\include\\stdlib.h" 1 3
# 34 "e:\\mingw\\include\\stdlib.h" 3

...
...

//头文件"Test.h"的展开 
# 4 "use_gcc.c" 2
# 1 "Test.h" 1

# 8 "Test.h"
void DoNothing(void);

//宏替换(#define);
//条件编译的处理(#if #endif等);
//去注释;
# 6 "use_gcc.c" 2
int main(void)
{
 int days[12] = { 31, 28,[4] = 31, 30, 31, [1] = 29 };
 int i;
 for (i = 0; i < 12; i++)
  printf("%2d %d\n", i + 1, days[i]);
 12;
 if(1) { printf("ccc\n"); };
 DoNothing();
 getchar();
 return 0;

编译(Compiling)

指令 gcc -S use_gcc.i -o use_gcc.s
实现 将*.i文件中源码转化为汇编代码*.s文件;进行词法分析、语义分析、语法分析、符号汇总;
注:对语法的检查是在编译阶段进行的。符号汇总:汇总的是全局的符号(如说函数名,全局变量)

 use_gcc.s

	.file	"use_gcc.c"
	.def	___main;	.scl	2;	.type	32;	.endef
	.section .rdata,"dr"
LC0:
	.ascii "%2d %d\12\0"
LC1:
	.ascii "ccc\0"
	.text
	.globl	_main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
LFB14:
	.cfi_startproc
	pushl	%ebp
	.cfi_def_cfa_offset 8
	.cfi_offset 5, -8
	movl	%esp, %ebp
	.cfi_def_cfa_register 5
	pushl	%edi
	andl	$-16, %esp
	subl	$80, %esp
	.cfi_offset 7, -12
	call	___main
	leal	28(%esp), %edx
	movl	$0, %eax
	movl	$12, %ecx
	movl	%edx, %edi
	rep stosl
	movl	$31, 28(%esp)
	movl	$29, 32(%esp)
	movl	$31, 44(%esp)
	movl	$30, 48(%esp)
	movl	$31, 52(%esp)
	movl	$0, 76(%esp)
	jmp	L2
L3:
	movl	76(%esp), %eax
	movl	28(%esp,%eax,4), %eax
	movl	76(%esp), %edx
	addl	$1, %edx
	movl	%eax, 8(%esp)
	movl	%edx, 4(%esp)
	movl	$LC0, (%esp)
	call	_printf
	addl	$1, 76(%esp)
L2:
	cmpl	$11, 76(%esp)
	jle	L3
	movl	$LC1, (%esp)
	call	_puts
	call	_DoNothing
	call	_getchar
	movl	$0, %eax
	movl	-4(%ebp), %edi
	leave
	.cfi_restore 5
	.cfi_restore 7
	.cfi_def_cfa 4, 4
	ret
	.cfi_endproc
LFE14:
	.ident	"GCC: (MinGW.org GCC-6.3.0-1) 6.3.0"
	.def	_printf;	.scl	2;	.type	32;	.endef
	.def	_puts;	.scl	2;	.type	32;	.endef
	.def	_DoNothing;	.scl	2;	.type	32;	.endef
	.def	_getchar;	.scl	2;	.type	32;	.endef

汇编(Assembling)

指令 gcc -c use_gcc.s -o use_gcc.o
实现 将*.s文件中的汇编源码转化为机器能执行的二进制机器码,生成文件*.o

use_gcc.o

4c01 0600 0000 0000 6402 0000 1500 0000
0000 0401 2e74 6578 7400 0000 0000 0000
0000 0000 a400 0000 0401 0000 1402 0000
0000 0000 0700 0000 2000 3060 2e64 6174
6100 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
4000 30c0 2e62 7373 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 8000 30c0 2e72 6461
7461 0000 0000 0000 0000 0000 0c00 0000
a801 0000 0000 0000 0000 0000 0000 0000
4000 3040 2f34 0000 0000 0000 0000 0000
0000 0000 2400 0000 b401 0000 0000 0000
0000 0000 0000 0000 4000 3040 2f31 3500
0000 0000 0000 0000 0000 0000 3c00 0000
d801 0000 5a02 0000 0000 0000 0100 0000
4000 3040 5589 e557 83e4 f083 ec50 e800
0000 008d 5424 1cb8 0000 0000 b90c 0000
0089 d7f3 abc7 4424 1c1f 0000 00c7 4424
201d 0000 00c7 4424 2c1f 0000 00c7 4424
301e 0000 00c7 4424 341f 0000 00c7 4424
4c00 0000 00eb 288b 4424 4c8b 4484 1c8b
5424 4c83 c201 8944 2408 8954 2404 c704
2400 0000 00e8 0000 0000 8344 244c 0183
7c24 4c0b 7ed1 c704 2408 0000 00e8 0000
0000 e800 0000 00e8 0000 0000 b800 0000
008b 7dfc c9c3 9090 2532 6420 2564 0a00
6363 6300 4743 433a 2028 4d69 6e47 572e
6f72 6720 4743 432d 362e 332e 302d 3129
2036 2e33 2e30 0000 1400 0000 0000 0000
017a 5200 017c 0801 1b0c 0404 8801 0000
2000 0000 1c00 0000 0400 0000 a200 0000
0041 0e08 8502 420d 0547 8703 0297 c5c7
0c04 0400 0b00 0000 1000 0000 1400 6d00
0000 0a00 0000 0600 7200 0000 1100 0000
1400 8500 0000 0a00 0000 0600 8a00 0000
1200 0000 1400 8f00 0000 1300 0000 1400
9400 0000 1400 0000 1400 2000 0000 0400
0000 1400 2e66 696c 6500 0000 0000 0000
feff 0000 6701 7573 655f 6763 632e 6300
0000 0000 0000 0000 5f6d 6169 6e00 0000
0000 0000 0100 2000 0201 0000 0000 0000
0000 0000 0000 0000 0000 0000 2e74 6578
7400 0000 0000 0000 0100 0000 0301 a200
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 0c00 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
1900 0000 0000 0000 0500 0000 0301 2300
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 2400 0000 0000 0000 0600 0000
0301 3c00 0000 0100 0000 0000 0000 0000
0000 0000 5f5f 5f6d 6169 6e00 0000 0000
0000 2000 0200 5f70 7269 6e74 6600 0000
0000 0000 2000 0200 5f70 7574 7300 0000
0000 0000 0000 2000 0200 0000 0000 2e00
0000 0000 0000 0000 2000 0200 5f67 6574
6368 6172 0000 0000 0000 2000 0200 3900
0000 2e72 6461 7461 247a 7a7a 002e 6568
5f66 7261 6d65 002e 7264 6174 6124 7a7a
7a00 2e65 685f 6672 616d 6500 5f44 6f4e
6f74 6869 6e67 00

链接(Linking)

指令 gcc use_gcc.o -o use_gcc.exe
实现 把由编译器产生的目标代码和所需的其他附加代码整合在一起。把函数库中的函数定义给关联进来。

链接错误:找不到函数DoNothing的函数定义undefined reference to `DoNothing'
解决 将DoNothing.c封装成静态库供其调用
1、生成DoNothing.o二进制文件
指令 gcc -c DoNothing.c -o DoNothing.o
2、将DoNothing.o生成静态库文件libDoNothing.a
指令 ar -rcs libDoNothing.a DoNothing.o
3、重新链接
指令 gcc use_gcc.o libDoNothing.a -o use_gcc.exe

 DoNothing.c

#include"Test.h"
#include <stdio.h>
	
void DoNothing(void)
{
    printf("Do nothing");
    CODE;
}

 DoNothing.o

4c01 0600 0000 0000 c601 0000 1200 0000
0000 0401 2e74 6578 7400 0000 0000 0000
0000 0000 2400 0000 0401 0000 9401 0000
0000 0000 0400 0000 2000 3060 2e64 6174
6100 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
4000 30c0 2e62 7373 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 8000 30c0 2e72 6461
7461 0000 0000 0000 0000 0000 1000 0000
2801 0000 0000 0000 0000 0000 0000 0000
4000 3040 2f34 0000 0000 0000 0000 0000
0000 0000 2400 0000 3801 0000 0000 0000
0000 0000 0000 0000 4000 3040 2f31 3500
0000 0000 0000 0000 0000 0000 3800 0000
5c01 0000 bc01 0000 0000 0000 0100 0000
4000 3040 5589 e583 ec18 c704 2400 0000
00e8 0000 0000 c704 240b 0000 00e8 0000
0000 90c9 c390 9090 446f 206e 6f74 6869
6e67 0063 6363 0000 4743 433a 2028 4d69
6e47 572e 6f72 6720 4743 432d 362e 332e
302d 3129 2036 2e33 2e30 0000 1400 0000
0000 0000 017a 5200 017c 0801 1b0c 0404
8801 0000 1c00 0000 1c00 0000 0400 0000
2100 0000 0041 0e08 8502 420d 055d c50c
0404 0000 0900 0000 0a00 0000 0600 0e00
0000 1000 0000 1400 1500 0000 0a00 0000
0600 1a00 0000 1100 0000 1400 2000 0000
0400 0000 1400 2e66 696c 6500 0000 0000
0000 feff 0000 6701 446f 4e6f 7468 696e
672e 6300 0000 0000 0000 0000 0000 1900
0000 0000 0000 0100 2000 0201 0000 0000
0000 0000 0000 0000 0000 0000 0000 2e74
6578 7400 0000 0000 0000 0100 0000 0301
2100 0000 0400 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 0f00 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 2400 0000 0000 0000 0500 0000 0301
2300 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 2f00 0000 0000 0000 0600
0000 0301 3800 0000 0100 0000 0000 0000
0000 0000 0000 5f70 7269 6e74 6600 0000
0000 0000 2000 0200 5f70 7574 7300 0000
0000 0000 0000 2000 0200 3900 0000 2e72
6461 7461 247a 7a7a 002e 6568 5f66 7261
6d65 005f 446f 4e6f 7468 696e 6700 2e72
6461 7461 247a 7a7a 002e 6568 5f66 7261
6d65 00

 libDoNothing.a

213c 6172 6368 3e0a 2f20 2020 2020 2020
2020 2020 2020 2020 3136 3739 3730 3938
3737 2020 3020 2020 2020 3020 2020 2020
3020 2020 2020 2020 3230 2020 2020 2020
2020 600a 0000 0001 0000 0058 5f44 6f4e
6f74 6869 6e67 0000 446f 4e6f 7468 696e
672e 6f2f 2020 2020 3136 3739 3730 3938
3632 2020 3020 2020 2020 3020 2020 2020
3130 3036 3636 2020 3833 3520 2020 2020
2020 600a 4c01 0600 0000 0000 c601 0000
1200 0000 0000 0401 2e74 6578 7400 0000
0000 0000 0000 0000 2400 0000 0401 0000
9401 0000 0000 0000 0400 0000 2000 3060
2e64 6174 6100 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 4000 30c0 2e62 7373 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 8000 30c0
2e72 6461 7461 0000 0000 0000 0000 0000
1000 0000 2801 0000 0000 0000 0000 0000
0000 0000 4000 3040 2f34 0000 0000 0000
0000 0000 0000 0000 2400 0000 3801 0000
0000 0000 0000 0000 0000 0000 4000 3040
2f31 3500 0000 0000 0000 0000 0000 0000
3800 0000 5c01 0000 bc01 0000 0000 0000
0100 0000 4000 3040 5589 e583 ec18 c704
2400 0000 00e8 0000 0000 c704 240b 0000
00e8 0000 0000 90c9 c390 9090 446f 206e
6f74 6869 6e67 0063 6363 0000 4743 433a
2028 4d69 6e47 572e 6f72 6720 4743 432d
362e 332e 302d 3129 2036 2e33 2e30 0000
1400 0000 0000 0000 017a 5200 017c 0801
1b0c 0404 8801 0000 1c00 0000 1c00 0000
0400 0000 2100 0000 0041 0e08 8502 420d
055d c50c 0404 0000 0900 0000 0a00 0000
0600 0e00 0000 1000 0000 1400 1500 0000
0a00 0000 0600 1a00 0000 1100 0000 1400
2000 0000 0400 0000 1400 2e66 696c 6500
0000 0000 0000 feff 0000 6701 446f 4e6f
7468 696e 672e 6300 0000 0000 0000 0000
0000 1900 0000 0000 0000 0100 2000 0201
0000 0000 0000 0000 0000 0000 0000 0000
0000 2e74 6578 7400 0000 0000 0000 0100
0000 0301 2100 0000 0400 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
0f00 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 2400 0000 0000 0000 0500
0000 0301 2300 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 2f00 0000 0000
0000 0600 0000 0301 3800 0000 0100 0000
0000 0000 0000 0000 0000 5f70 7269 6e74
6600 0000 0000 0000 2000 0200 5f70 7574
7300 0000 0000 0000 0000 2000 0200 3900
0000 2e72 6461 7461 247a 7a7a 002e 6568
5f66 7261 6d65 005f 446f 4e6f 7468 696e
6700 2e72 6461 7461 247a 7a7a 002e 6568
5f66 7261 6d65 000a 

use_gcc_e.exe

4d5a 9000 0300 0000 0400 0000 ffff 0000
b800 0000 0000 0000 4000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 8000 0000
0e1f ba0e 00b4 09cd 21b8 014c cd21 5468
6973 2070 726f 6772 616d 2063 616e 6e6f
7420 6265 2072 756e 2069 6e20 444f 5320
6d6f 6465 2e0d 0d0a 2400 0000 0000 0000
5045 0000 4c01 0d00 3cbc 0164 0072 0000
d701 0000 e000 0701 0b01 021c 002e 0000
...
...
6e5f 746c 735f 696e 6974 5f63 616c 6c62
6163 6b00 5f5f 746c 735f 7573 6564 005f
5f5f 6372 745f 7874 5f65 6e64 5f5f 005f
7666 7072 696e 7466 005f 5f69 6d70 5f5f
456e 7465 7243 7269 7469 6361 6c53 6563
7469 6f6e 4034 005f 5f69 6d70 5f5f 6677
7269 7465 00
(共2586行)

程序运行结果:

 1 31
 2 29
 3 0
 4 0
 5 31
 6 30
 7 31
 8 0
 9 0
10 0
11 0
12 0

简化编译链接流程

指令 gcc use_gcc.c
    gcc use_gcc.c -o use_gcc.exe 指定生成文件名
    gcc C:\Users\***\Desktop\use_gcc\use_gcc.c 指定.c文件位置
    gcc use_gcc.c -o ..\use_gcc.exe 指定生成文件位置(上一级目录)
实现 直接由use_gcc.c生成可执行文件use_gcc.exe

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HaGoq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值