(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.找到正确的目标框架名称设置