C语言编译过程(预处理、编译、汇编、链接)

命令

$ gcc -E hello.c -o hello.i
$ gcc -S hello.i -o hello.s
$ gcc -c hello.s -o hello.o
$ gcc hello.o -o hello
$
$ ./hello
hello world!
pre-compiled here!
static __always_inline void func

C程序

// hello.c
#include <stdio.h>

#define PREPROCESSING	"Pre-processing"
static __always_inline void func(void)
{
	printf("static __always_inline void func\n");
	return;
}

int main(int argc, const char *argv[])
{
	printf("Hello world!\n");
	printf("%s here!\n", PREPROCESSING);

	func();

	return 0;
}

预处理(Preprocessing)

将宏、头文件等代码替换到被调用处,不检查语法
预处理结果就是将stdio.h 文件中的内容插入到test.c中了。
预处理后的文件格式为.i
gcc -E hello.c -o hello.i

// hello.i
/* 此处省略stdio.h */
# 2 "hello.c" 2
static __inline __attribute__ ((__always_inline__)) void func(void)
{
 printf("static __always_inline void func\n");
 return;
}
int main(int argc, const char *argv[])
{
 printf("hello world!\n");
 printf("%s here!\n", "pre-processing");

 func();

 return 0;
}

编译(Compilation)

将c语言代码编译为最高效的汇编语言代码,并检查语法
汇编文件格式为.s
gcc -S hello.i -o hello.s

// hello.s
	.file	"hello.c"
	.section	.rodata
.LC0:
	.string	"hello world!"
.LC1:
	.string	"%s here!\n"
.LC2:
	.string	"pre-processing"
	.align 8
.LC3:
	.string	"static __always_inline void func"
	.text
.globl main
	.type	main, @function
main:
.LFB1:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	movq	%rsp, %rbp
	.cfi_offset 6, -16
	.cfi_def_cfa_register 6
	subq	$16, %rsp
	movl	%edi, -4(%rbp)
	movq	%rsi, -16(%rbp)
	movl	$.LC0, %edi
	call	puts
	movl	$.LC1, %eax
	movl	$.LC2, %esi
	movq	%rax, %rdi
	movl	$0, %eax
	call	printf
	movl	$.LC3, %edi
	call	puts
	movl	$0, %eax
	leave
	ret
	.cfi_endproc
.LFE1:
	.size	main, .-main
	.ident	"GCC: (Ubuntu/Linaro 4.4.7-8ubuntu1) 4.4.7"
	.section	.note.GNU-stack,"",@progbits

汇编(Assembly)

汇编代码编译为目标文件
目标文件格式为.o
gcc -c hello.s -o hello.o

链接(Linking)

将程序的目标文件与所需的所有附加的目标文件连接起来,最终生成可执行文件。附加的目标文件包括静态连接库动态连接库
此步骤最终生成可执行程序
gcc hello.o -o hello

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值