Search code examples
linuxgccmakefilefpic

Compiling executable with "-fPIC" flag (no shared library)


I am currently working on some legacy packages and I see that some Makefiles use the -fPIC flag to compile the final executable.

My understanding is that this is used when creating a shared library and that it would make no sense to use it when creating an executable.

I wonder if I am right or I am missing something, because when I search on the internet, I only get results talking about shared libraries and not executables.

Thanks,


Solution

  • Position independent code can be useful outside of shared library context. One common case is Address space layout randomization, which makes the final executable more secure against code injection attacks.

    In several distributions, such as Fedora, most of the packages are built as PIE.