【问题标题】:What is the difference between `-fpic` and `-fPIC` gcc parameters?`-fpic` 和 `-fPIC` gcc 参数有什么区别?
【发布时间】:2011-04-02 10:09:46
【问题描述】:

我已经阅读了gcc 联机帮助页,但我仍然无法理解-fpic-fPIC 之间的区别。有人可以用非常简单明了的方式解释一下吗?


相关问题:

【问题讨论】:

  • 是的,答案不在man gcc,而是在info gcc,有更多文档。

标签: gcc fpic


【解决方案1】:

http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

使用 `-fPIC` 或 `-fpic` 生成与位置无关的代码。是否使用 `-fPIC` 或 `-fpic` 生成位置无关代码取决于目标。 `-fPIC` 选项总是有效的,但可能会产生比 `-fpic` 更大的代码(要记住的是 PIC 的情况更大,因此它可能会产生更多的代码)。使用 `-fpic` 选项通常会生成更小更快的代码,但会有平台相关的限制,例如全局可见符号的数量或代码的大小。当您创建共享库时,链接器会告诉您它是否适合。如果有疑问,我会选择`-fPIC`,因为它总是有效的。

【讨论】:

  • 更重要的是:我在这里做了一个小实验(在 x86_64 平台上),-fPIC-fpic 似乎生成了相同的代码。他们似乎只在 m68k、PowerPC 和 SPARC 上生成不同的代码。
  • 针对某个目标以一种方式编译的一个 gcc 版本的单个实验。对结果持保留态度,期望结果会随着时间的推移而改变,特别是使用 GCC 之类的工具。
  • 我可以问一个问题吗? 全局可见符号是什么意思?
  • @DenilsonSáMaia 小更新:根据man gcc,它对 ARM64 也有影响
【解决方案2】:

来自Gcc manual page

为共享库生成代码时,-fpic 意味着 -msmall-data 和 -fPIC 意味着 -mlarge-data。

地点:

 -msmall-data
 -mlarge-data
       When -mexplicit-relocs is in effect, static data is accessed via
       gp-relative relocations.  When -msmall-data is used, objects 8
       bytes long or smaller are placed in a small data area (the
       ".sdata" and ".sbss" sections) and are accessed via 16-bit
       relocations off of the $gp register.  This limits the size of the
       small data area to 64KB, but allows the variables to be directly
       accessed via a single instruction.

       The default is -mlarge-data.  With this option the data area is
       limited to just below 2GB.  Programs that require more than 2GB
       of data must use "malloc" or "mmap" to allocate the data in the
       heap instead of in the program's data segment.

       When generating code for shared libraries, -fpic implies
       -msmall-data and -fPIC implies -mlarge-data.

【讨论】:

  • 链接的手册页已更新,记得查看。
猜你喜欢
  • 1970-01-01
  • 2011-07-15
  • 2021-07-27
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 2018-02-08
  • 2017-01-05
  • 2013-12-26
相关资源
最近更新 更多