gcc编译选项

本文介绍了GCC编译动态库时的-fpic和-fPIC选项的区别,前者总是可用但可能导致代码增大,后者可能产生更小、更快的代码但有平台限制。同时,文章讨论了-static的作用,并详细阐述了-march选项,特别是-march=native如何让编译器自适应探测并优化目标架构。当遇到‘no such instruction’错误时,建议删除-march=native或使用正确的目标架构名称。

(1)fpic 和 fPIC 区别

在64位下编译动态库的时候,经常会遇到下面的错误

/usr/bin/ld: /tmp/ccQ1dkqh.o: relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC

Use -fPIC or -fpic to generate code. Whether to use -fPIC or -fpic to generate code is target-dependent. The -fPIC choice always works, but may produce larger code than -fpic (mnenomic to remember this is that PIC is in a larger case, so it may produce larger amounts of code). Using -fpic option usually generates smaller and faster code, but will have platform-dependent limitations, such as the number of globally visible symbols or the size of the code. The linker will tell you whether it fits when you create the shared library. When in doubt, I choose -fPIC, because it always works.

-fPIC: 总是能够工作,但可能产生的代码较大

-fpic: 通常能产生更快更小的代码,但有平台限制。

(2)编译选项 -static 的作用

(3)-march 指定目标架构选项

-march选项就是就是指定目标架构的名字,gcc就会生成针对目标架构优化的目标代码,如-march=prescott会生成针对i5或i7的目标码,从而充分发挥cpu的性能。

自gcc4.2,引入了 -march=native ,从而允许编译器自动探测目标架构并生成 针对目标架构优化的目标代码 ,这比手工设置要安全的多。

通过下面的命令查询当前机器的架构名称,比如:

#gcc -march=native -Q --help=target | grep march

 -march=                     haswell

有的时候编译会报 "no such instruction", (没有这个机器指令)

解决的方案:

1.makefile 中删掉 -march=native

2.找到正确的目标框架名称设置


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值