RiscV彙編介紹(1)-編譯過程

從c/c++源文件,到能夠執行文件,須要如下幾個步驟:c++

  • 預處理/編譯
  • 彙編
  • 連接


image


下面咱們以hello world程序爲例,展現整個編譯連接過程。編程

1. 編寫hello.c代碼ubuntu

#include <stdio.h>
int main(void)
{
        printf("Hello World!\n");
        return 0;
}


2.使用gcc –E hello.c –o hello.i, 將源文件hello.c文件預處理生成hello.iide

3.編譯, gcc –S hello.i –o hello.s, 生成彙編程序hello.s,對於x86系統,生成x86彙編代碼。spa

ction	.rodata
.LC0:
	.string	"Hello World!"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	leaq	.LC0(%rip), %rdi
	call	puts@PLT
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0"
	.section	.note.GNU-stack,"",@progbits

4.彙編 gcc –c hello.s –o hello.o, 生成目標機器碼。3d

5.連接,和系統庫文件進行連接,ld hello.o –o hello, 執行會出錯,只靠一個hello.o不能生成一個完整的可執行文件。code

gcc hello.c –o hello 能夠直接生成可執行文件。blog

相關文章
相關標籤/搜索