Search code examples
cross-compilingsconsmingw-w64fpic

Remove default -fpic flag from library cross-compilation


My setup of an environment in SConstruct is the following:

env = Environment(
    CC = 'i686-w64-mingw32-gcc',
    LINK = 'i686-w64-mingw32-gcc',
    CCFLAGS = '-Werror',
    RANLIB = 'i686-w64-mingw32-ranlib',
    AR = 'i686-w64-mingw32-ar'
)

Nonetheless scons adds the -fPic parameter to the compilation:

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
i686-w64-mingw32-gcc -o src/apply.os -c -Werror -fPIC src/apply.c
src/apply.c:1:0: error: -fPIC ignored for target (all code is position independent) [-Werror]

Since scons lacks any cross-compilation features I would like to remove any "forced on" compiler parameter.

I'm using version 2.3.4 of this tool.


Solution

  • SCons detects that you're trying to build a shared library, for which the "-fPIC" option is kind of required under normal circumstances. If your cross-compiler doesn't support it, you can overwrite the definition of the shared-lib flags variable after constructing the Environment:

    env = Environment( ... as above ...)
    env.Replace(SHCCFLAGS='$CCFLAGS')
    

    This should remove the offending option, when creating C based libraries. You might have to do the same with the "SHCXXFLAGS" variable, which is responsible for compiling C++ sources...