gcc build process - yaokun123/php-wiki GitHub Wiki

gcc编译过程

一、分步编译

1、预处理:gcc -E hello.c -o hello.i
2、编译:gcc -S hello.i -o hello.s
3、汇编:gcc -c hello.s -o hello.o
4、链接:gcc hello.o -o hello

预处理:
    带#的语句就是预处理指令,预处理指令在预处理的时候处理
    头文件展开:#include<stdio.h>包含文件stdio.h(预处理时将stdio.h文件拷贝至预处理文件中)
    删除注释:注释有两种方法://  /* */
    宏替换:#define代表是申明一个宏,在预处理时会将宏给替换
    预处理时 不会检查语法错误
    条件编译:条件不成立:#if 0->#endif    条件成立:#if 1->#endif

编译:
    将预处理文件编译成汇编文件
    检查语法错误
汇编:
    将汇编文件编译生成二进制文件.o
链接:
    设置运行环境、堆栈、链接其他库


#include<stdio.h>
#define PI 3.14

int main(){
    printf("hello world %lf\n",PI);

    //这是一个注释

    //条件编译
    #if 0
    printf("条件编译false\n");
    #endif

    #if 1
    printf("条件编译true\n");
    #endif
    return 0;
}
选项 含义
-E 只进行预处理
-S 只进行预处理和编译
-c 只进行预处理、编译和汇编
-o 指定生成的文件名
后缀名 含义
.c c语言文件
.i 预处理后的c语言文件
-s 编译后的汇编文件
-o 编译后的目标文件

二、system库函数

作用:在程序中启动另一个程序
参数:要的是待启动程序的路径名

#include<stdio.h>
#include<stdlib.h>

int main(){
    //system("pause");//pause程序的作用是等待用户输入任意按键
    system("ls");
    printf("hello world");
    return 0;
}

三、汇编

mov a,3    //3的值放在a对应内存的位置
mov b,4    //4的值放在b对应内存的位置
mov eax,a    //把a内存的值放在eax寄存器
add eax,b    //eax和b相加,结果放在eax
mov c,eax    //eax的值放在c中
⚠️ **GitHub.com Fallback** ⚠️