本文主要对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
当前分类随机文章推荐
- C++ 回调函数 阅读3292次,点赞0次
- GCC - -fpic、-fPIC、-fpie、-fPIE编译选项的作用和区别 阅读235次,点赞0次
- C++ - 在Windows/Linux上创建单级目录以及多级目录的跨平台方法 阅读1915次,点赞0次
- C++ - Windows/Linux跨平台gbk与utf8字符集编码转换 阅读496次,点赞0次
- C++ - 使用C++标准库过滤Windows文件名中的非法字符 阅读4991次,点赞1次
- CMake - Windows系统设置CMake代理 阅读491次,点赞0次
- C++ - 线程安全的std::cout 阅读2288次,点赞0次
- C++ - 左值和右值,右值引用与移动语义的概念与理解 阅读742次,点赞1次
- C++ - 只有在Debug模式下才使用std::cout输出调试日志,Release发布版本不输出调试日志 阅读5073次,点赞0次
- C++ - 对字符串和图片进行base64编解码 阅读1068次,点赞0次
全站随机文章推荐
- CCTV电视高清直播源 阅读5962次,点赞4次
- PaddlePaddle - 使用Paddle2Onnx将Paddle模型导出为onnx模型 阅读202次,点赞0次
- C++ - 最简单的将文本文件的内容一次性读取到std::string的方法 阅读5663次,点赞4次
- FFmpeg - 录制HLS直播流为mp4文件 阅读2593次,点赞0次
- 资源分享 - GPU Gems中文译文版GPU 精粹(1-3) 高清PDF下载 阅读9517次,点赞1次
- 资源分享 - Introduction to Computer Graphics - A Practical Learning Approach 英文高清PDF下载 阅读1547次,点赞0次
- Pytorch - .to()和.cuda()的区别 阅读1227次,点赞0次
- 资源分享 - OpenGL编程指南(原书第7版)- OpenGL红宝书高清扫描版PDF下载 阅读5440次,点赞1次
- 资源分享 - Artificial Intelligence - A Modern Approach , Third Edition 英文高清PDF下载 阅读2434次,点赞0次
- 资源分享 - Polygon Mesh Processing英文高清PDF下载 阅读8039次,点赞1次
评论
169