What is the difference between `-fpic` and `-fPIC` gcc parameters?
Asked Answered
U

2

119

I've already read the gcc manpage, but I still can't understand the difference between -fpic and -fPIC. Can someone explain it, in a very simple and clear way?


Related questions:

Ulterior answered 23/8, 2010 at 0:24 Comment(1)
Yes the answer isn't in man gccbut in info gcc, which have more documentation.Woodchuck
D
133

http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

Use `-fPIC` or `-fpic` to generate position independent code. Whether to use `-fPIC` or `-fpic` to generate position independent code is target-dependent. The `-fPIC` choice always works, but may produce larger code than `-fpic` (mnemonic to remember this is that PIC is in a larger case, so it may produce larger amounts of code). Using `-fpic` option usually generates smaller and faster code, but will have platform-dependent limitations, such as the number of globally visible symbols or the size of the code. The linker will tell you whether it fits when you create the shared library. When in doubt, I choose `-fPIC`, because it always works.
Dad answered 23/8, 2010 at 1:33 Comment(4)
What's more: I did a little experiment here (on x86_64 platform), -fPIC and -fpic appears to have generated the same code. It seems they generate a different code only on m68k, PowerPC and SPARC.Corycorybant
A single experiment with one version of gcc compiled in one way for some target. Take that result with a grain of salt, expect that result to change over time particular with a tool like GCC.Photovoltaic
May I ask a question; what does globally visible symbols mean?Assailant
@DenilsonSáMaia small update: It also makes a difference on ARM64, according to man gccNeck
K
35

From the Gcc manual page:

When generating code for shared libraries, -fpic implies -msmall-data and -fPIC implies -mlarge-data.

Where:

 -msmall-data
 -mlarge-data
       When -mexplicit-relocs is in effect, static data is accessed
       via gp-relative relocations.  When -msmall-data is used,
       objects 8 bytes long or smaller are placed in a small data
       area (the ".sdata" and ".sbss" sections) and are accessed via
       16-bit relocations off of the $gp register.  This limits the
       size of the small data area to 64KB, but allows the variables
       to be directly accessed via a single instruction.

       The default is -mlarge-data.  With this option the data area
       is limited to just below 2GB.  Programs that require more
       than 2GB of data must use "malloc" or "mmap" to allocate the
       data in the heap instead of in the program's data segment.

       When generating code for shared libraries, -fpic implies
       -msmall-data and -fPIC implies -mlarge-data.
Kagu answered 5/6, 2017 at 18:26 Comment(1)
The linked manual page has been updated, remember to check out.Pitcher

© 2022 - 2024 — McMap. All rights reserved.