When I compile my library I have switched ont -fPIC
because I want to be able to compile it as a shared library but also as static.
Using gcc 3.4.4 on cygwin I get this warning on all source files:
-fPIC ignored for target (all code is position independent)
And I really wonder what's the point of it. It tells me that I use a switch which has no effect because what the switch should avieche is already accomplished. Well, it means it's redundant, fine. But what's the point of it and how can I suppress it?
I'm not talking about why using PIC or not, just why it generates that IMO useless warning.
the switch has some effect on linux (on windows/cygwin it would do nothing, maybe the compiler did not add platform specific check heregg) code generated with -fPIC is position independent, that means all instructions that refer to a specific address must be replaced by redirection to memory location; memory location is then set by dynamic loader; the result is is slightly slower - and takes more time to load; You don't need that for a static library, where all addresses are set by the linker, when the executable is created/linked.
The warning probably means that code of the static library is not quite as fast as you might expect it to be.You can create two object files of the same name in different directories, one with -fPIC for the shared library and the other for the static library.
That's a good question, and I have not seen a definitive answer. At least one of the GCC devs considers it a pointless warning. Paolo Bonzini called it that in his recent patch Remove pointless -fPIC warning on Windows platforms.
According to Jonathan Wakely on the GCC mailing list at How to suppress "warning: -fPIC ignored for target..." under Cygwin (August 2015):
And from Alexander Monakov on the same thread (referencing Bonzini's patch):
Related, Windows has
/ASLR
, which is address space layout randomization. Its optional, but often required as a security gate, meaning all program code on must be compiled with it. If you have an SDLC, then you are probably using/ASLR
because Microsoft calls it out as a best practice in Writing Secure Code.The Linux/Unix equivalent of
/ASLR
is-fPIE
for executables.Under Windows, all DLL code is relocatable. Under Linux/Unix, shared object code can be made relocatable with
-fPIC
.-fPIC
is a "superset" of-fPIE
(some hand waiving). That means-fPIC
can be used anywhere you would use-fPIE
(but not vice-versa).Personally, I'd just add os detection to the makefile. Something along the lines of
Not only a useless warning, but a distraction that makes it difficult to follow other warnings and errors.
Given my make output consistently showed 3 related lines, I opted to filter out the 3 "useless" lines using the following:
I realize this is not the ideal way to suppress the noise, but maybe it will help to some degree. Be advised that I'm cutting 3 lines where other situations may only require removal of just one line. Note that I'm also routing stderr to stdout.
Here's a snipit of make output without the sed filter:
And the same with the sed filter: