杂记连载之gcc

该篇整理出了与gcc编译器相关的一些内容,希望大家对将源文件编译成可执行文件的过程及编译过程的一些细节有所了解。

①gcc的编译过程分为预处理(Pre-Processing)、编译(Compiling)、汇编(Assembling)、链接(Linking)四个阶段。gcc需要调用预处理程序cpp,由它负责展开在源文件中定义的宏,并向其中插入“#include”语句所包含的内容;接着,gcc会调用ccl和as将处理后的源代码编译成目标代码;最后,gcc会调用链接程序ld,把生成的目标代码链接成一个可执行程序。

以下将说明与各阶段有关的编译选项及一般生成文件的后缀名。

-E

Preprocess only; donot compile, assemble or link

-S

Compile only, do notassemble or link. The output is in the form of an assembler code file for eachnonassembler input file specified.

-c

Compile and assemble,but do not link

-o <file>

Place the output into<file>

.a为后缀的文件,是由目标文件构成的档案库文件(静态库)。静态库指编译链接时,把库文件的代码全部加入到可执行文件中。可使用ar打包生成,如ar -cr libtest.a test.o

.i为后缀的文件,是已经预处理过的C源代码文件;

.ii为后缀的文件,是已经预处理过的C++源代码文件;

.o为后缀的文件,是编译后的目标文件;

以hello.c为例,可生成各阶段的文件进行查看。

预处理gcc –E hello.c –o hello.i

编译gcc –S hello.i –o hello.s

汇编gcc –c hello.s –o hello.o

生成可执行文件gcc hello.o –o hello

②gcc的一些选项。

gcc的优化选项:

-Ox(大写O)使用优化选项,X的值为空、0、1、2、3,0为不优化,优化的目的是减少代码空间和提高执行效率等。

-O or –O1

Optimize.Optimizingcompilation takes somewhat more time, and a lot more memory for a largefunction.

With -O, the compilertries to reduce code size and execution time, without performing anyoptimizations that take a great deal of compilation time.

-O2

Optimize even more.GCC performs nearly all supported optimizations that do not involve aspace-speed tradeoff. As compared to -O, this option increases both compilationtime and the performance of the generated code.

-O0

Reduce compilationtime and make debugging produce the expected results. This is the default.

-Os

Optimize for size. -Osenables all -O2 optimizations that do not typically increase code size. It alsoperforms further optimizations designed to reduce code size.

-L参数指定库文件所在的目录,-l参数用来指定程序要链接的库。

-DMARCO 以字符串"1"定义MARCRO宏

-DMARCO=DEFN 以字符串DEFN定义MARCRO宏

-UMARCO 取消MACRO宏定义

-s

Remove all symbol tableand relocation information from the executable.

③这些选项与makefile的目标依赖关系有关。

-M 生成文件关联的信息。包含目标文件所依赖的所有源代码你可以用gcc -M hello.c

-MM 与上面的那个一样,但是它将忽略由#include造成的标准库头文件依赖关系。

-MD 与-M相同,将输出导入到.d的文件里面

-MMD 与-MM相同,将输出导入到.d的文件里面

使用-MM选项一般会生成如

snippet.o:snippet.c defs.h(自定义头文件)

的依赖关系,而在makefile中可以使用这一依赖关系进行目标的生成。通常的作法是,使用-MMD生成不包含标准库头文件的依赖关系.d文件,然后在makefile中使用include包含这一.d文件。

SRCS = foo.c bar.c

include $(SRCS:.c=.d)

上述语句中的"$(SRCS:.c=.d)"中的".c=.d"的意思是做一个替换,把变量$(SRCS)所有[.c]的字串都替换成[.d]。

④与静态库与动态库相关:

gcc -shared -fPIC -olibtest.so test1.o test2.o

-shared选项通知链接器生成共享库,而不是生成普通的可执行文件。-fPIC表明生成与位置无关的目标。

如果库的版本有静态和动态两个版本,除非明确指定链接静态版本,链接器会选择共享库版本进行链接。使用-L选项指明路径(静态和动态版本不在同目录)或-static选项。

gcc -static -o appapp.o -L. -ltest

指明链接时使用静态库版本。

gcc -ltest -o snippetsnippet.c //出现undefined refrence错误

gcc -o snippetsnippet.c -ltest //错误,要指明路径或cp到/lib/目录

gcc -o sniipetsnippet.c -L. -ltest //正确


注意库链接的顺序:

gcc -o snippetlibtest.a snippet.c //错误

gcc -o snippetsnippet.c libtest.a //正确 

对于gcc中的某些选项、生成的可执行文件及动态链接机制,则需要结合linux下的ELF(Executableand Linking Format 可执行和链接格式)文件格式进行理解。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值