Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 545
Hassan Syed Avatar asked Oct 18 '10 17:10

Hassan Syed


People also ask

What does static flag Do gcc?

The -static flag forces the linker to accept only static libraries and not any shared libraries. If you want to use -static , you have to ensure that you have a static version of the C library installed, which might be tricky to find (most systems do not have a static C library any more).

Which gcc flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

Does gcc depend on glibc?

Is it possible to compile a program using gcc without depending on glibc? Yes, there are alternative libc versions, such as Musl, uClibc, dietLibc, etc. See their documentation on how to use them. Your problem does not appear to be a GLIBC dependency, but rather a mismatch between your build and your target hosts.

Can I use gcc to compile C++ code?

GCC stands for GNU Compiler Collections which is used to compile mainly C and C++ language. It can also be used to compile Objective C and Objective C++.


1 Answers

  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.

like image 69
F'x Avatar answered Sep 23 '22 12:09

F'x