gcc编译过程
ARM开发步骤 : 预处理(Preprocess)——编译( Compile)——汇编(assemble)——连接(link)
gcc使用方法
gcc [options] file
gcc常用选项
-v 查看版本号,显示gcc执行时的详细过程Display the programs invoked by the compiler
方法1:
-o <file> Place the output into <file>
gcc -o hello hello.c :编译hello.c文件,输出一个可执行hello.out文件 ,然后./hello.out执行应用程序
步骤1:
gcc -E -o hello.i hello.c 对hello.c进行预处理得到hello.i
步骤2:
gcc -S -o hello.s hello.i 对预编译的文件hello.i进行汇编得到hello.s
步骤3:
gcc -c -o hello.o hello.s 将汇编文件hello.s翻译成机器语言hello.o
步骤4:
gcc -o hello hello.o 连接,生产输出文件hello文件,就是.elf文件,可执行应用程序
*.o文件就是object file(OBJ文件)
小结:
(1)输入文件的后缀名和选项共同决定gcc到底执行哪些操作
(2)在编译过程中,除非使用了-E -S -c选项(或者编译出错阻止了完整的编译过程,否则最后的步骤都是连接)
gcc会对.c文件默认进行预处理、汇编处理,-c再来指明了编译、汇编,从而得到.o文件,再通过gcc -o hello hello.o 将.o文件进行链接,得到可执行应用程序
链接的解释:就是将汇编生成的OBJ文件、系统库的OBJ文件、库文件链接起来,最终生成可以在特定平台运行的执行程序。
crt1.o、crti.o、crtbegin.o、crtend.o、crtn.o是gcc加入的系统标准启动文件,对于一般应用程序,这些启动是必须的
-lc:链接libc库文件,其中libc库文件就是实现printf等函数
gcc -v -nostdlib -o hello hello.o 会提示因为没有连接系统标准启动文件和标准库文件,而连接失败。这个-nostdlib选项常用于裸机/bootloader、linux内核等程序,因为他们不需要启动文件、标准库文件。
一般应用程序才需要系统标准启动文件和标准库文件
动态链接使用动态链接库进行链接,生成的程序在执行的时候需要加载所需的动态库才能运行。
动态链接生成的程序体积较小,但是必须依赖所需的动态库,否则无法执行。
静态链接使用静态库进行连接,生成的程序包含程序运行所需的全部库,可以直接运行,不过静态库链接生成的程序体积大。
使用-static选项就是静态链接
-### Like -v but options quoted and commands not executed
-E 只预处理,不编译 Preprocess only; do not compile, assemble or link
-S Compile only; do not assemble or link
-c Compile and assemble, but do not link
-o <file> Place the output into <file>
-pie Create a position independent executable
-shared Create a shared library
-x <language> Specify the language of the following input files
Permissible languages include: c c++ assembler none
'none' means revert to the default behavior of
guessing the language based on the file's extension