注:以下学习内容学习于韦东山老师arm裸机第一期教程
一.编译过程简介
一般C程序编译过程:
预处理->编译->汇编->链接
1.1 预处理: C/C++源文件中,以#开头的命令被称为预处理命令,包括#include,#define,#if,#endif等。
预处理就是将要包含的文件插入到源文件中,将宏定义展开,根据条件编译选择要使用的代码,最后将这些代码输出到一个.i文件中
1.2 编译: 将C/C++代码(比如上述的".i"文件)“翻译”成汇编代码
1.3 汇编: 将汇编代码翻译成一定格式的机器码,在Linux系统上一般为elf文件(OBJ文件)
1.4 链接: 将生成的OBJ文件和系统库的OBJ文件、库文件连接起来,生产可执行文件
以hello.c为例
预处理 编译 汇编 链接
hello.c ------------->hello.i------------------>hello.s--------------->hello.o-------------->elf格式可执行文件
二.gcc使用方法
gcc [选项] 文件名
最简单的方法就是 gcc hello.c
会产生一个a.out的可执行文件
三.gcc的常用选项
3.1 gcc --help来查看有哪些编译选项
3.2 gcc -v
a.查看gcc编译器的版本
b.显示gcc编译时的详细过程
3.3 gcc -o
指定输出的文件名,输出文件名不可跟源文件名同名
gcc -o hello hello.c
3.4 gcc -E
对文件只预处理,不进行汇编,编译,链接
gcc -E -o hello.i hello.c
3.5 gcc -S
只编译,不进行汇编和链接
gcc -S -o hello.s hello.i
3.6 gcc -c
只编译和汇编,不会链接
gcc -c -o hello.o hello.s
3.7 gcc -o
链接
gcc -o hello hello.o
3.8 小结:
1. 输入文件的后缀名和选项共同决定gcc执行哪些操作
2. 在编译过程中,除非使用了-E,-S,-c选项,或者编译出错阻止了编译的过程,否则最后的步骤都是链接。
3.9 编译文件的4个步骤
1
2
3
4gcc -E -o hello.i hello.c gcc -S -o hello.s hello.i gcc -c -o hello.o hello.s gcc -o hello hello.o
1
2gcc -c -o hello.o hello.c gcc -o hello hello.o
四.深入链接过程
4.1 对于hello.c程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <stdio.h> #define MIN 5 #if 0 #define MAX 10 #endif int main() { printf("hello world!n"); printf("MIN = %d", MIN); return 0; }
在生成可执行文件之后,ls -l hello查看文件大小发现有8K多字节,但是我们的源代码只有10几行。
链接将生成的OBJ文件和系统库的OBJ文件、库文件连接起来,生产可执行文件,最终生生成可以在特定平台上运行的可执行程序。
a.生成的OBJ文件: hello
b. 系统库的OBJ文件: 在链接文件时加上-v选项 gcc -o hello hello.o
链接时具体细节如下:1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31book@www.100ask.org:/work/gcc_options/1th$ gcc -v -o hello hello.o Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.4' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/ LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../:/lib/:/usr/lib/ COLLECT_GCC_OPTIONS='-v' '-o' 'hello' '-mtune=generic' '-march=x86-64' /usr/lib/gcc/x86_64-linux-gnu/5/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/5/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper -plugin-opt=-fresolution=/tmp/ccbhavbV.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro -o hello /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/5/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/5 -L/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/5/../../.. hello.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/5/crtend.o /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crtn.o
链接过程中的crt1.o crti.o crtn.o等都是在链接过程中gcc加入的系统标准启动文件,对于一般应用程序,这些标准文件是必须的.
printf在C库中实现,在链接的时候-L来指明了链接的路径,-l指明要链接哪些库文件,-lc表示要链接libc这个库文件,里面实现了printf的函数
因此,链接的时候需要链接系统标准启动文件和库文件
4.2 在编译时可以使用-nostartfiles选项来表示不链接系统标准启动文件,而标准库文件仍然正常使用,-nostdlib选项表示不链接系统标准启动文件和标准库文件
使用命令 gcc -nostdlib -o hello hello.o进行链接时报错如下
1
2
3
4
5
6
7
8
9/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400144 hello.o: In function `main': hello.c:(.text+0xa): undefined reference to `puts' hello.c:(.text+0x1e): undefined reference to `printf' collect2: error: ld returned 1 exit status
提示没有找到入口标号,原因是没有链接标准启动文件
同时提示没有定义puts,printf函数,原因是没有链接标准库文件
注意:
-nostdlib选项常用于裸机,bootloader,linux内核等程序,因为它们不需要启动文件,也不需要标准库文件
但是对于应用程序需要系统标准系统文件和标准库文件
五.动态链接与静态链接
5.1 通过命令file hello查看hello文件的信息
其中dynamically linked表示动态链接
然后通过命令ldd hello查看hello所依赖哪些库文件
1
2
3linux-vdso.so.1 => (0x00007ffefb1bc000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8240a8a000) /lib64/ld-linux-x86-64.so.2 (0x00007f8240e54000 )
5.2 动态链接和静态链接的区别
a. 动态链接使用动态链接库进行链接,生产的程序在执行的时候需要加载所需的动态库才能执行。
动态链接生成的程序体积较小,但是必须依赖所需的动态库,否则无法执行
b. 静态链接使用静态库进行链接,生产的程序包含程序运行所需要的全部库,可以直接运行
不过静态库链接生产的程序体积较大
如下方式分别进行动态链接和静态链接
1
2
3gcc -c -o hello.o hello.c gcc -o hello_shared hello.o gcc -static -o hello_static hello.o
1
2
3ls -l hello* -rwxrwxr-x 1 book book 8656 6月 5 12:24 hello_shared -rwxrwxr-x 1 book book 912760 6月 5 12:26 hello_static
ldd hello_static查看是否需要库,发现并不需要任何库
最后
以上就是伶俐巨人最近收集整理的关于arm裸机学习预备知识二:gcc编译器相关知识的全部内容,更多相关arm裸机学习预备知识二内容请搜索靠谱客的其他文章。
发表评论 取消回复