What, if any, are the implications of compiling objects with gcc -fPIC flag if they get used in executables?

一曲冷凌霜 提交于 2019-12-06 20:14:32

问题


I am putting together a makefile for a project i am working on. I have an executable and a shared library in the project. Both use some of the same source files which get compiled separately into object files. To be able to use these objects in the shared library I need to use the -fPIC (position independent code) flag in gcc. Are there any implications of compiling objects with -fPIC that get used in a executable?


回答1:


Compiling position-independent code when not necessary is a performance drag on some platforms (most notably, the register-starved x86 architecture, because PIC takes away a register that can be used more freely when non-PIC), but there should be no detrimental effects otherwise.

Going further, it is even possible to create a position-independent executable (-fPIE) built out of only position-independent code.

That being said, libtool can automatically produce both PIC and non-PIC objects for each source file you compile, and it should be easy to do the same even in a build system without libtool integration.



来源:https://stackoverflow.com/questions/1165593/what-if-any-are-the-implications-of-compiling-objects-with-gcc-fpic-flag-if-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!