Home

fPIC

fPIC is an abbreviation for position-independent code. It refers to a compiler option that instructs the compiler to generate code that can be loaded at any address in a process’s virtual memory, which is essential for creating shared libraries and for dynamically loaded modules. Code produced with fPIC uses relative addressing and indirection through mechanisms such as the global offset table, so references to symbols are resolved at load time rather than at link time.

In practice, fPIC is widely required for shared libraries. When building a dynamic library, enabling fPIC ensures

Performance and size considerations are part of the trade-offs. Position-independent code can incur a small overhead

Usage guidelines: for shared libraries on most systems, compile all units with -fPIC. For executables that should

the
library
can
be
mapped
into
a
process
at
arbitrary
addresses
without
modification.
For
executables,
some
toolchains
offer
analogous
options
(for
example,
-fPIE
in
GCC/Clang,
and
the
linker
flag
-pie)
to
produce
position-independent
executables.
Although
-fPIC
and
-fPIE
are
related,
they
are
not
always
interchangeable;
they
are
designed
for
different
final
outputs
and
may
differ
in
performance
and
relocation
details
depending
on
the
platform
and
toolchain.
and
larger
code
size
due
to
the
use
of
indirection
and
relocation
tables,
but
modern
architectures
minimize
these
costs.
The
benefits—relocation
flexibility,
shared
libraries,
and
improved
security
through
address
space
layout
randomization—often
outweigh
the
downsides.
be
relocatable,
use
the
appropriate
PIE
options
(-fPIE
and
-pie
or
equivalent).
For
statically
linked
programs
that
will
not
be
relocated,
fPIC
is
typically
unnecessary.