Search code examples
cshared-librariesmingwlibtool

autoconf set -fPIC only when necessary


I'm writing a shared library using autoconf/libtool which I want to compile for Linux and for Windows (Using the mingw cross-compiler). For Linux (and maybe other platforms which support it) I need to set -fPIC. So I put it into the CFLAGS in Makefile.am. But when I cross-compile it with mingw then gcc complains with a warning:

warning: -fPIC ignored for target (all code is position independent)

So obviously this option is not needed for Windows code. It is only a warning but I want to get rid of it anyway. How can I do this? Maybe there is already a libtool/autoconf feature which checks if the option is supported and only sets it when needed so I don't have to do this manually in Makefile.am?


Solution

  • You shouldn't need to set -fPIC manually, libtool will add it if you tell it what type of binary/library you're building.

    lib_LTLIBRARIES = mylibrary.la
    mylibrary_la_SOURCES = mylibrary.c
    

    This can produce both a mylibrary.so with PIC (if needed) and a mylibrary.a without, depending on other Autoconf/Automake options. (Probably something like .dll and .lib on Windows, but I don't use that platform.)