Why gcc does not implicitly supply the -fPIC flag when compiling static libraries on x86_64

I had a lot of problems compiling shared objects that statically set static libraries. This issue only occurs on x84_64 platforms. When doing the same compilation work on x86_32 I have no problems.

This may be a specific GCC configuration for the OS, but my research shows that its GCC runs on x86_64 platforms. Anyway, I am using gcc 4.4.3 on Ubuntu 10.04 x86_64.

How to fix the problem? ... Make sure that all the dependencies of the static library are compiled with -fPIC.

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

Question 2: My assumption is that when linking to static code, you rigidly connect functions to your binary code during the connection, why do you need the indirectness level "position-independent code" does the equipment provide?

Question 3: Now, if x86 does not need -fpic / -fPIC to link shared objects with static archives, why is this necessary in x86_64?

Question 4: even if necessary, why is it not provided implicitly? I thought breaking change was supposed to be big, no-no

+48
gcc compilation dynamic-linking static-linking gnu-toolchain
Oct 18 '10 at 17:00
source share
1 answer
  • See question 3544035 . Also discussed here and there .
  • It depends on what use you have for your static library. If you want to associate it only with programs, it does not need PIC code (calls to libtool, which are a convenient library, because you could do without it, it just helps to get, for example, your compilation process to a reasonable size). Otherwise, if you intend to link shared libraries with it, you will need the PIC code in your static library.
  • See question 3146744 and also here
  • It inflates your code, so it is not the default. It should be noted that when you compile a single object file, GCC does not know whether you are going to create a shared library from it or not. In most of my small projects, I simply link several object files and, for example, do not need PIC code.

In addition, I would advise: if you need to worry about it, you are doing it wrong (or you like to learn on the hard way, which is nice because you will get more experience). Compilation systems (libtool, cmake, everything you use) should do this for you.

+37
Oct 27 '10 at 18:35
source share



All Articles