Linux程序编译的过程

编译过程

c,c++代表的高层语言编写的程序在转化成为处理器可以识别并且执行的二级制代码的过程
在这里插入图片描述

预处理Preprocessing

编译Compilation

汇编Assembly

链接Linking

GCC 工具链介绍

GCC 编译工具

Binutils 二进制程序处理器

C运行库 语法 + 标准库

编写 hello 程序

#include<stdio.h>

int main(void){
	printf("Hello World\n");
	return 0;
}

gcc 将源文件进行预处理,将生成预处理文件

-E 源程序预处理之后停止

[root@localhost test]# gcc -E hello.c -o hello.i
[root@localhost test]# ls
demo01.sh  hello.c  hello.i
[root@localhost test]# cat hello.i

查看hello.i文件

在这里插入图片描述

编译,将预处理生成的我呢间编译成汇编文件hello.s

-S gcc执行编译后停止,生成汇编程序

[root@localhost test]# gcc -S hello.i -o hello.s
[root@localhost test]# ls
demo01.sh  hello.c  hello.i  hello.s
[root@localhost test]# cat hello.s
	.file	"hello.c"
	.section	.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
	movl	$.LC0, %edi
	call	puts
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-44)"
	.section	.note.GNU-stack,"",@progbits
[root@localhost test]# 

汇编,将生成的汇编程序hello.s生成hello.o目标文件

-c,汇编之后停止,生成目标文件

或者直接使用 as 代替 gcc 命令进行汇编,asBinutils中的命令

[root@localhost test]# gcc -c hello.s -o hello.o
[root@localhost test]# ls
demo01.sh  hello.c  hello.i  hello.o  hello.s
[root@localhost test]# cat hello.o                                                                                                                 ELF>@@
UH怨¸]ello WorldGCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-44)zRx 
P                                                             C
򽁁       hello.cmainputs

 
񁀿ÿÿ .symtab.strtab.shstrtab.rela.text.data.bss.rodata.comment.note.GNU-stack.rela.eh_frame @忰                                                     &UU1U
    90a.BWR@                                                                                                                                       
                                                                                                                                                   ΁0a[root@localhost test]# XshellXshellXshell

此时生成的hello.o文件是ELF文件,就是执行连接格式的可重定向文件

链接

静态连接,在编译阶段直接将静态库直接添加到可执行文件中去

[root@localhost test]# gcc -static hello.c -o staticHello
[root@localhost test]# size staticHello 
   text	   data	    bss	    dec	    hex	filename
 771320	   6196	   8640	 786156	  bfeec	staticHello
[root@localhost test]# ldd staticHello 
	不是动态可执行文件
[root@localhost test]# 

动态链接,在链接阶段,把需要的动态库加入到内存中去

使用了动态链接,使用 size 命令可以看到可执行文件的大小,ldd可以看到链接的其他glibc的动态库

[root@localhost test]# gcc hello.c -o hello
[root@localhost test]# size hello
   text	   data	    bss	    dec	    hex	filename
   1226	    548	      4	   1778	    6f2	hello
[root@localhost test]# ldd hello
	linux-vdso.so.1 =>  (0x00007ffdae1aa000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f92b215a000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f92b2528000)
[root@localhost test]# 

分析ELF文件

ELF 文件的段

text,已编译程序的指令代码段

rodata,只读数据

data,初始化后的C程序全局变量和静态局部变量

debug,调试符号表

在这里插入图片描述

使用readelf -S查看每个段的信息
在这里插入图片描述

反编译ELF文件,可以查看ELF文件的指令和数据

objdump -D hello

在这里插入图片描述
反编译并且和C源码混合显示

[root@localhost test]# gcc -o hello -g hello.c
[root@localhost test]# objdump -S hello

在这里插入图片描述

Linux 基本命令

安装和登录的命令

login,shutdown,halt,reboot,install,mount,unmount,chsh,exit,last,init 0,init 6

cat 查看文件的内容

mv 移动文件和重命名

chmod 修改权限

rm 删除文件

rmdir 删除目录

man 帮助文档

–help 帮助手册

tar 解压缩

su ,sudo 切换用户,sudo 命令需要输入当前用户的密码,su 命令需要输入 root 用户的密码

  • su root 输入root密码后切换之root用户但是pwd目录不变

  • su - root 输入root密码后切换之root用户但是pwd目录/root

  • sudo 命令只允许使用提升的权限运行单个命令,而 su 命令会启动一个新的 shell,同时允许使用 root 权限运行尽可能多的命令,直到明确退出登录。

df 磁盘剩余

grep,find,awk 文本查找三剑客

umask 掩码

ln 创建链接

who 查看当前系统使用者

ping 查看所给的URL是否正常通信

mkdir 创建文件夹

touch 创建文件

vim,vi 文本编辑器

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值