本文主要对gcc和cmake经常使用的编译选项-fpic、-fPIC、-fpie、-fPIE的作用和区别进行总结。

1 -fpic

官方文档说明

Generate position-independent code (PIC) suitable for use in a shared library, if supported for the target machine. Such code accesses all constant addresses through a global offset table (GOT). The dynamic loader resolves the GOT entries when the program starts (the dynamic loader is not part of GCC; it is part of the operating system). If the GOT size for the linked executable exceeds a machine-specific maximum size, you get an error message from the linker indicating that -fpic does not work; in that case, recompile with -fPIC instead. (These maximums are 8k on the SPARC, 28k on AArch64 and 32k on the m68k and RS/6000. The x86 has no such limit.)

Position-independent code requires special support, and therefore works only on certain machines. For the x86, GCC supports PIC for System V but not for the Sun 386i. Code generated for the IBM RS/6000 is always position-independent.

When this flag is set, the macros __pic__ and __PIC__ are defined to 1.

在编译代码时使用该选项,如果目标机器支持,生成适合在共享库(动态链接库)中使用的位置无关代码。

2 -fPIC

官方文档说明

If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table. This option makes a difference on AArch64, m68k, PowerPC and SPARC.

Position-independent code requires special support, and therefore works only on certain machines.

When this flag is set, the macros __pic__ and __PIC__ are defined to 2.

在编译代码时使用该选项,如果目标机器支持,生成适合在共享库(动态链接库)中使用的位置无关代码,并且避免对全局偏移表大小的限制。

3 -fpie和-fPIE

官方文档说明

These options are similar to -fpic and -fPIC, but the generated position-independent code can be only linked into executables. Usually these options are used to compile code that will be linked using the -pie GCC option.

-fpie and -fPIE both define the macros __pie__ and __PIE__. The macros have the value 1 for -fpie and 2 for -fPIE.

-fpie和-fPIE类似于-fpic和-fPIC,不过与-fpic和-fPIC不同的是,-fpie和-fPIE适合在可执行文件中生成与位置无关的代码。

4 总结

通过上面官方文档的说明,总的来说,如果我们编译的代码是为了生成一个与位置无关的动态链接库,则使用-fpic和-fPIC代码生成选项;如果编译的代码是为了生成一个一个与位置无关的可执行文件,则使用-fpie和-fPIE代码生成选项。

参考