Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 657
user19745 Avatar asked Jul 22 '09 14:07

user19745


1 Answers

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.

like image 188
ephemient Avatar answered Sep 20 '22 05:09

ephemient