为什么gcc在x86_64上编译静态库时不会隐式地提供-fPIC标志 [英] Why does gcc not implicitly supply the -fPIC flag when compiling static libraries on x86_64

查看:863
本文介绍了为什么gcc在x86_64上编译静态库时不会隐式地提供-fPIC标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译与静态链接的静态链接的共享对象时遇到了许多问题。此问题只显示在x84_64平台上。当在x86_32上做同样的编译工作时我没有任何问题。



也许这是一个特定于操作系统的GCC配置,但我的研究表明GCC在x86_64平台上的工作原理。无论如何,我在Ubuntu 10.04 x86_64上使用gcc 4.4.3。



如何解决问题?...确保所有的静态库依赖关系都编译为-fPIC

问题1: -fpic和-fPIC有什么区别(显然-fPIC在x86上生成更多的指令)?为什么后来的类型在x86_64上下文更相关?



问题2:我的假设是,当你链接到静态代码,你在连接时将函数连接到二进制文件中,为什么是否需要位置独立代码机械提供的间接级别?



问题3:现在,如果x86不需要-fpic / -fPIC链接共享对象与静态归档为什么需要x86_64 ?



问题4:即使它是需要为什么不是隐式提供?我认为突破性变化应该是一个很大的不。

解决方案


  1. =http://stackoverflow.com/questions/3544035/what-is-the-difference-between-fpic-and-fpic-gcc-parameters> 3544035 。还讨论了此处此处

  2. 这取决于您对静态库的使用。如果你只想将它链接到程序,它不需要PIC代码(libtool调用一个便利库,因为你几乎可以没有它,它只是帮助你的编译过程到一个合理的大小,例如)。否则,如果您打算链接共享库,您需要在静态库中提供PIC代码。

  3. 查看问题 3146744 以及此处

  4. 它使代码膨胀,因此它不是默认值。有一件事要看到,当你编译一个单一的目标文件,GCC不知道你是否要创建一个共享库。在我的大多数较小的项目中,我只是链接在一起几个对象文件,并且不需要PIC代码,例如。

此外,我的建议是:如果你需要担心,你做错了(或者你喜欢学习的困难的方式,这是很好,因为你会得到更多的经验)。编译系统(libtool,cmake,无论你使用什么)应该为你做。


I've had numerous problems compiling shared objects that link statically against static libraries. This problem only shows up on x84_64 platforms. When doing the same compilation work on x86_32 I do not have any problems.

Perhaps this is a OS specific GCC configuration thing, but my research indicates that its how GCC works on x86_64 platforms. Anyhow, I am using gcc 4.4.3 on Ubuntu 10.04 x86_64.

How is the problem fixed ?... Making sure all the static library dependencies are compiled with -fPIC.

Question 1: What is the difference between -fpic and -fPIC (apparently -fPIC generates more instructions on x86) ? Why is the later type more relevant in the x86_64 context ?

Question 2: My assumption is that when you link against static code you are hard-wiring the functions into your binary at link time, why does it need the level of indirection the "position independant code" machinery provides ?

Question 3: Now if x86 doesn't need -fpic / -fPIC to link shared objects against static archives why is it needed in x86_64 ?

Question 4: even if it is needed why isn't it supplied implicitly ? I thought breaking changes was supposed to be a big no-no

解决方案

  1. See question 3544035. Also discussed here and there.
  2. It depends on what use you will have for your static library. If you only want to link it into programs, it doesn't need PIC code (libtool calls that a convenience library, because you could pretty much do without it, it simply helps get your compilation process to a reasonable size, for example). Otherwise, if you intend to link shared libraries against it, you need PIC code in your static library.
  3. See question 3146744 and also here
  4. It bloats your code, so it's not the default. One thing to see is that, when you compile a single object file, GCC doesn't know if you're going to create a shared library out of it or not. In most of my smaller projects, I simply link together a couple of object files, and do not need PIC code, for example.

Also, my advice would be: if you need to worry about that, you're doing it wrong (or you like to learn the hard way, which is nice because you'll get more out of the experience). Compilation systems (libtool, cmake, whatever you use) should do that for you.

这篇关于为什么gcc在x86_64上编译静态库时不会隐式地提供-fPIC标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆