Reputation: 7118
I understand g++ -fPIC option as nicely explainjed in: GCC -fPIC option
I have many source files those are managed through makefile
for build. It's difficult to segregate source files that is meant for being part of an executable or shared library. Can I used -fPIC option of g++ for any file that goes under compilation as:
g++ -c -fPIC ....
and later, if it is a shared library, link with -shared
, otherwise without -shared
for executable.
Upvotes: 2
Views: 606
Reputation: 8220
Can I used -fPIC option of g++ for any file that goes under compilation?
Yes. If something such as an assembly file is given, it'll just have no effect.
[ ... ] and later, if it is a shared library, link with -shared?
Yes.
Upvotes: -1
Reputation: 21906
It is possible to build executables with -fPIC
but this may result in performance penalties as compiler will make more conservative assumptions e.g. about inlining (to allow runtime symbol interposition). To produce position-independent executable, you'd better off using -fPIE
flag (most modern distros e.g. Ubuntu build with it on by default now).
In any case, unless you really don't care about performance you'll need to compile your files twice, with different flags.
Upvotes: 2