make--(1)编译流程

前言

现在的IDE很成熟了,但掌握底层的编译仍然很重要。c的编译流程分成四个部分
1预处理(Preprocessing), 2.编译(Compilation), 3.汇编(Assemble), 4.链接(Linking)。
本篇主要验证windows环境下的4个流程。

准备

首先下载一个编译器,你可以选择用VS自带的cl,我这里使用的是GCC。
下载地址:MINGW
下载之后,一直继续,选择要安装的包,等待安装完成。ok
接着,配置环境变量。
点击我的电脑右键- >属性 -> 高级系统设置->环境变量-> 设置
将你安装MINGW路径中的bin添加到PATH中,将lib添加到lib中,将include添加到include中,我这里的路径是 D:\study\gcc\include

ok,打开命令行,输入gcc,安装成功


创建一个测试文件–main.c

#include<stdio.h>
#define HELLO "hello word"
int main()
{
  printf(HELLO);
  getchar();
  return 0;
}

预处理

预处理总的来说 处理预处理命令也就是#开头如#define,#include,#ifdef等,其次是删除代码中的注释,添加行号标识等
命令 gcc -E -o main.i main.c
-E 可以是编译器在预处理结束时停止编译。
-o 可以给目标文件起个名字,否则默认a.exe

...
 __attribute__((__cdecl__)) __attribute__((__nothrow__)) int _getw (FILE *);
 __attribute__((__cdecl__)) __attribute__((__nothrow__)) int _putw (int, FILE *);
 __attribute__((__cdecl__)) __attribute__((__nothrow__)) wint_t fgetwchar (void);
 __attribute__((__cdecl__)) __attribute__((__nothrow__)) wint_t fputwchar (wint_t);
 __attribute__((__cdecl__)) __attribute__((__nothrow__)) int getw (FILE *);
 __attribute__((__cdecl__)) __attribute__((__nothrow__)) int putw (int, FILE *);
# 2 "main.c" 2
# 5 "main.c"
int main()
{
  printf("hello word");
  getchar();
  return 0;
}

编译

在预处理后,先进行词法分析,语法分析,语义分析,如果无误就会将代码翻译为汇编语言
命令 gcc -S -o main.s main.i

	.file	"main.c"
	.def	___main;	.scl	2;	.type	32;	.endef
	.section .rdata,"dr"
LC0:
	.ascii "hello word\0"
	.text
	.globl	_main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
LFB10:
	.cfi_startproc
	pushl	%ebp
	.cfi_def_cfa_offset 8
	.cfi_offset 5, -8
	movl	%esp, %ebp
	.cfi_def_cfa_register 5
	andl	$-16, %esp
	subl	$16, %esp
	call	___main
	movl	$LC0, (%esp)
	call	_printf
	call	_getchar
	movl	$0, %eax
	leave
	.cfi_restore 5
	.cfi_def_cfa 4, 4
	ret
	.cfi_endproc
LFE10:
	.ident	"GCC: (MinGW.org GCC-6.3.0-1) 6.3.0"
	.def	_printf;	.scl	2;	.type	32;	.endef
	.def	_getchar;	.scl	2;	.type	32;	.endef

汇编

将汇编语言转换为机器语言,这也就是目标文件,在linux中是.o在windows中是.obj文件
命令 : gcc -c -o main.obj main.s

链接

到这里还没有对依赖的库进行链接,如main.c中的printf();所以最后一步是进行依赖库的链接,exe文件就实现了。
命令:gcc -o main.exe main.obj

最后

所以总结一下各种文件的编译流程:
.h文件–只在预处理使用,所以预处理后,.h与.c合并,文件会增大很多
.c文件–经历预处理,编译,汇编,生成二进制的目标文件
.i文件–经历编译汇编,生成目标文件
.lib.dll文件–只在链接使用,与目标文件一起生成可执行文件
.obj文件-- 只在链接使用,与库文件一起生成课执行文件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

工农村贴膜小哥

我倒是要看看是那个憨憨在给我打

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

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

打赏作者

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

抵扣说明:

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

余额充值