问题
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