• 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

  • 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

  • 工资「喂饱肚子」,副业「养活灵魂」!

  • 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 感谢大家访问本站,希望本站的内容可以帮助到大家!

  • 问题反馈可发送邮件到stubbornhuang@qq.com

  • 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

  • 本站会放置Google广告用于维持域名以及网站服务器费用。

GCC – -fpic、-fPIC、-fpie、-fPIE编译选项的作用和区别

C++ 发布于2023-09-06 阅读 3,578次 0次评论 0次点赞 本文共1878个字,阅读需要5分钟。

本文主要对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代码生成选项。

参考

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:GCC – -fpic、-fPIC、-fpie、-fPIE编译选项的作用和区别

原文链接:https://www.stubbornhuang.com/2787/

发布于:2023年09月06日 15:18:22

修改于:2023年09月06日 15:18:22

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
Google Search Console移动设备易用性问题未设置视口值、可点击元素之间的距离太近、文字太小无法阅读、内容宽度超过了屏幕显示范围问题修复
建站运维
下一篇
GCC - 常用手动链接选项-lz、-lrt、-lm、-lc、-lpthread、-lcrypt、dl链接都是什么库?
C++
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号