Question

When I compile my library I have switched on -fPIC because I want to be able to compile it as a shared and as a static library.

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 achieve 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.

Was it helpful?

Solution 3

And I really wonder what's the point of it...
I'm not talking about why using PIC or not, just why it generates that IMO useless warning.

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):

That warning has been there since well before 2003 (I couldn't be bothered tracing the history back past a file rename in 2003).

And from Alexander Monakov on the same thread (referencing Bonzini's patch):

A patch was proposed just recently to just remove the warning: https://gcc.gnu.org/ml/gcc-patches/2015-08/msg00836.html


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).

OTHER TIPS

and how can I suppress it?

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:

make 2>&1 | sed '/PIC ignored/{N;N;d;}'

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:

libavcodec/x86/mlpdsp.c:51:36: warning: taking address of expression of type 'void'
                                    &ff_mlp_iirorder_4 };
                                    ^
CC      libavcodec/x86/motion_est_mmx.o
libavcodec/x86/motion_est_mmx.c:1:0: warning: -fPIC ignored for target (all code is position independent)
 /* */ 
 ^
CC      libavcodec/x86/mpegaudiodec_mmx.o
libavcodec/x86/mpegaudiodec_mmx.c:1:0: warning: -fPIC ignored for target (all code is position independent)
 /* */ 
 ^
CC      libavcodec/x86/mpegvideo_mmx.o
libavcodec/x86/mpegvideo_mmx.c:1:0: warning: -fPIC ignored for target (all code is position independent)
 /* */ 
 ^

And the same with the sed filter:

                                                    ^
libavcodec/x86/mlpdsp.c:51:36: warning: taking address of expression of type 'void'
                                    &ff_mlp_iirorder_4 };
                                    ^
CC      libavcodec/x86/motion_est_mmx.o
CC      libavcodec/x86/mpegaudiodec_mmx.o
CC      libavcodec/x86/mpegvideo_mmx.o
CC      libavcodec/x86/proresdsp-init.o

Personally, I'd just add os detection to the makefile. Something along the lines of

TARGET_TRIPLE := $(subst -, ,$(shell $(CC) -dumpmachine))
TARGET_ARCH   := $(word 1,$(TARGET_TRIPLE))
TARGET_OS     := $(word 3,$(TARGET_TRIPLE))

ifeq      ($(TARGET_OS),mingw32)
else ifeq ($(TARGET_OS),cygwin)
else
CFLAGS += -fPIC
endif

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top