gcc編譯選項


(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 的作用

spacer.gifwKiom1g75lzRXZEhAACzOMduVys242.gif



(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.找到正確的目標框架名稱設置


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章