如果有的话,如果在可执行文件中使用带有 gcc -fPIC 标志的编译对象会有什么影响?

发布于 2024-07-27 12:08:53 字数 167 浏览 5 评论 0原文

我正在为我正在从事的项目编写一个 makefile。 我的项目中有一个可执行文件和一个共享库。 两者都使用一些相同的源文件,这些源文件被单独编译成目标文件。 为了能够在共享库中使用这些对象,我需要在 gcc 中使用 -fPIC(位置无关代码)标志。 使用 -fPIC 编译在可执行文件中使用的对象是否有任何影响?

I am putting together a makefile for a project i am working on. I have an executable and a shared library in the project. Both use some of the same source files which get compiled separately into object files. To be able to use these objects in the shared library I need to use the -fPIC (position independent code) flag in gcc. Are there any implications of compiling objects with -fPIC that get used in a executable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来分手还会想你 2024-08-03 12:08:53

在某些平台上,不必要时编译位置无关代码会拖累性能(最明显的是,寄存器匮乏的 x86 架构,因为 PIC 拿走了非 PIC 时可以更自由使用的寄存器),但不应该有任何不利影响否则会产生影响。

更进一步,甚至可以创建仅由位置无关代码构建的位置无关可执行文件 (-fPIE)。

话虽这么说,libtool 可以自动为您生成的每个源文件生成 PIC 和非 PIC 对象编译,即使在没有 libtool 集成的构建系统中也应该很容易执行相同的操作。

Compiling position-independent code when not necessary is a performance drag on some platforms (most notably, the register-starved x86 architecture, because PIC takes away a register that can be used more freely when non-PIC), but there should be no detrimental effects otherwise.

Going further, it is even possible to create a position-independent executable (-fPIE) built out of only position-independent code.

That being said, libtool can automatically produce both PIC and non-PIC objects for each source file you compile, and it should be easy to do the same even in a build system without libtool integration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文