linux makefile

windows 可执行文件exe是由PE结构产生的

预编译(hello.i) -> 汇编(hello.s) -> 编译(hello.o) -> 链接(elf文件)

预编译: hello.c hello.h >> hello.i

​gcc -E -o hello.i hello.c

​程序名 预编译 输出 目标文件 源文件

为什么参数不需要hello.h
因为hello.c已经include

hello.i里面的数据为什么那么多
里面包含了头文件,还有预编译的展开

汇编: hello.i >> hello.s

​ gcc -S -o hello.s hello.i

​ 程序名 汇编 输出 目标文件 源文件

编译: hello.s >> hello.o

​ gcc -c -o hello.o hello.s

​ 程序名 编译 输出 二进制目标文件 源文件

链接: hello.o >> hello

​ gcc -o hello hello.o 除了-o,再不加其他参数,就是链接

运行程序的两种方式

绝对路径运行: /home/ubuntu/hello
相对路径运行: ./hello . 代表当前路径 ./hello 代表当前路径下的hello程序
如果是单文件的话,可以不用链接

为什么hello.o不能直接运行,这涉及到了linux文件的执行权限

二进制表示方式
rw-rw-r--
110 110 100
6 6 4

chmod    +x     hello.o

+x :给hello.o文件加一个执行权限

chmod运行效果:给文件拥有者和文件拥有者的组给了x(执行)权限

-rwx rwx r-x

111 111 101

7 7 5

还原hello.o的执行权限:

chmod  664   hello.o

hello.o可能会运行错误,因为可能没有包含其他头文件

gcc参数:

-g代表让编译程序的输出中产生调试信息(符号表),以供gdb调试的时候使用

-D代表DEBUG_printf,编译的时候加上 -DDEBUG参数,程序执行会打印编译信息,相当于在程序里#define DEBUG

-I代表指定include的第三方库文件

-L代表指定连接库的搜索目录,-l(小写)指定链接库的名字

比如: gcc main.o -L/usr/lib -lqt -o hello

-L/usr/lib:指定搜索目录为/usr/lib

-lqt:指定链接库的名字为qt

-Wall:一种更为高级的警告检查

-static:静态编译,缺点是静态编译后的文件比较大

-o:优化选项

-o1:和-o一样,都是指定1级优化

-o2: 指定2级优化

-o3: 指定3级优化

-o0: 指定不优化

例:gcc -c -o3 -o0 hello.c 若出现多个优化参数,以最后一个为准

*linux里面的动态链接库的后缀是.so
linux里面的静态链接库的后缀是.a*

linux命令:

tail hello.c 看最后10行

more hello.c 按住ENTER看更多的

cat hello.c 全看

ls -l hello.o 查看文件的权限

ls -l hello.o运行效果

​ - rw- rw- r-- 1 ubuntu ubuntu 1504 3月 2 12:26 hello.o

​ - : 普通文件

rw-: 文件拥有者可读可写

rw-: 文件拥有者的组可读可写

r- : 其他人拥有可读权限

ubuntu: 文件拥有者

ubuntu:文件拥有者的组

二进制表示方式
rw- rw- r--

110 110 100

6 6 4

命令id运行效果:

uid=500(ubuntu) ubuntu用户id是500 -> ubuntu代表500

root用户的id是0

为什么linux有时候不能成为root用户:

 sudo  vi  /etc/ssh/sshd_config

sshd_config里面有个PermitRootLogin , 把后面的no改为yes就可以了

makefile原则:依赖是具有传递性的

Last modification:June 16, 2021
If you think my article is useful to you, please feel free to appreciate