zoukankan      html  css  js  c++  java
  • RiscV汇编介绍(1)-编译过程

    从c/c++源文件,到可以执行文件,需要以下几个步骤:

    • 预处理/编译
    • 汇编
    • 链接


    image


    下面我们以hello world程序为例,展示整个编译链接过程。

    1. 编写hello.c代码

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


    2.使用gcc –E hello.c –o hello.i, 将源文件hello.c文件预处理生成hello.i

    3.编译, gcc –S hello.i –o hello.s, 生成汇编程序hello.s,对于x86系统,生成x86汇编代码。

    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, 生成目标机器码。

    5.链接,和系统库文件进行链接,ld hello.o –o hello, 执行会出错,只靠一个hello.o不能生成一个完整的可执行文件。

    gcc hello.c –o hello 可以直接生成可执行文件。















  • 相关阅读:
    出差常熟,郁闷中 沧海
    ABAP中程序之间传递参数的办法 沧海
    LSMW中出现No logical path specified. 沧海
    请认真对待生活 沧海
    escape sequence 沧海
    休假一周 沧海
    Print Program and Form Modify 沧海
    下周回南京 沧海
    LO020真麻烦 沧海
    函数讲解函数列表(ZT) 沧海
  • 原文地址:https://www.cnblogs.com/mikewolf2002/p/11342374.html
Copyright © 2011-2022 走看看