Questions tagged [fpic]

`-fPIC` is a flag for the C compiler `gcc` which causes position-independent code to be produced. Position-independent code is important when compiling libraries which are to be dynamically linked, also known as .dll (dynamically-linked libraries) in Windows or .so (shared objects) in Linux. Use this tag if you have problems compiling code when the flag `-fPIC` is in use, or when asking about building binaries with this flag.

-fPIC is a flag for the C compiler gcc which causes position-independent code to be produced. Position-independent code is important when compiling libraries which are to be dynamically linked, also known as .dll (dynamically-linked libraries) in Windows or .so (shared objects) in Linux.

Taken from man gcc for gcc 4.6.3, on Ubuntu 12.04:

-fPIC
  If supported for the target machine, emit position-independent
  code, suitable for dynamic linking and avoiding any limit on the
  size of the global offset table.  This option makes a difference on
  the m68k, PowerPC and SPARC.

  Position-independent code requires special support, and therefore
  works only on certain machines.

  When this flag is set, the macros "__pic__" and "__PIC__" are
  defined to 2.
142 questions
577
votes
5 answers

GCC -fPIC option

I have read about GCC's Options for Code Generation Conventions, but could not understand what "Generate position-independent code (PIC)" does. Please give an example to explain me what does it mean.
Narek
  • 38,779
  • 79
  • 233
  • 389
135
votes
3 answers

What does -fPIC mean when building a shared library?

I know the '-fPIC' option has something to do with resolving addresses and independence between individual modules, but I'm not sure what it really means. Can you explain?
Sasha
116
votes
2 answers

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

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: What does -fPIC mean when building a shared library? What, if any,…
Denilson Sá Maia
  • 47,466
  • 33
  • 109
  • 111
74
votes
9 answers

How to recompile with -fPIC

I was trying to reinstall my ffmpeg, following this guide, on my ARM Ubuntu machine. Unfortunately, when I compile a program which uses this lib I get the following failure: /usr/bin/ld: /usr/local/lib/libavcodec.a(amrnbdec.o): relocation…
user1455085
  • 915
  • 1
  • 6
  • 10
73
votes
7 answers

How can I tell, with something like objdump, if an object file has been built with -fPIC?

How can I tell, with something like objdump, if an object file has been built with -fPIC?
Crazy Chenz
  • 12,650
  • 12
  • 50
  • 62
33
votes
3 answers

Linking a shared library against a static library: must the static library be compiled differently than if an application were linking it?

At least on Linux and Solaris, static libraries are really just a bunch of compiled .o's tossed into one big file. When compiling a static library, usually the -fpic flag is ommited, so the generated code is position dependent. Now say my static…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
27
votes
2 answers

Why not always use fpic (Position Independent Code)?

I read this post on PIC and it seems that it always be good to use PIC (whenever it is exe / static / share llibrary). So what are the disadvantages? Are there examples elaborating when not to use PIC?
Azil
  • 457
  • 2
  • 5
  • 10
25
votes
3 answers

Why is fPIC absolutely necessary on 64 and not on 32bit platforms?

I recently received a: ...relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC error while trying to compile a program as a shared library. Now the solution to this is not too difficult…
Bojan B
  • 393
  • 3
  • 9
22
votes
2 answers

Compiling ghc with -fPIC support

I'm trying to install GHC with -fPIC support in Fedora. I've grabbed a source tarball since it seems no binary one has this. In Build.mk i've changed the quick build type to ifeq "$(BuildFlavour)" "quick" SRC_HC_OPTS = -H64m -O0 -fasm…
Phyx
  • 2,697
  • 1
  • 20
  • 35
22
votes
2 answers

Is there a way to determine that a .a or .so library has been compiled as position indepenent code?

I am getting a linking error when compiling the numpy library against lapack indicating I need to compile lapack with -fPIC. I thought I had done just that. Is there a way to determine that the produced lapack library is position independent?
Setjmp
  • 27,279
  • 27
  • 74
  • 92
22
votes
7 answers

Recompile with -fPIC option, but the option is already in the makefile

I get this error when I do the make: relocation R_X86_64_32 against `vtable for Torch::MemoryDataSet' can not be used when making a shared object; recompile with -fPIC It says that I should recompile with the -fPIC option. I did that, adding the…
Eduardo
  • 19,928
  • 23
  • 65
  • 73
15
votes
5 answers

Shared libraries and .h files

I have some doubt about how do programs use shared library. When I build a shared library ( with -shared -fPIC switches) I make some functions available from an external program. Usually I do a dlopen() to load the library and then dlsym() to link…
nick2k3
  • 1,399
  • 8
  • 18
  • 25
14
votes
2 answers

Global variables, shared libraries and -fPIC effect

I made a piece of code which consists in a dynamic library (lib.c), and a main executable (main.c). In both files I define a global variable named: int global. Not very smart but it's not the question. When I compile the dynamic library the -fPIC…
yota
  • 2,020
  • 22
  • 37
12
votes
1 answer

What, if any, are the implications of compiling objects with gcc -fPIC flag if they get used in executables?

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…
user19745
  • 3,499
  • 8
  • 25
  • 21
11
votes
2 answers

Trying to load position independent code on cortex-m3

I have an embedded application which will have a bootloader which will decide to run 1 of two applications directly from internal flash. I am trying to make these apps position independent so that they both can be compiled for the same base…
Dan
  • 261
  • 3
  • 10
1
2 3
9 10