Autoconf set -fPIC only when necessary

I am 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 possibly other platforms that support it) I need to install -fPIC. So I put it in CFLAGS in Makefile.am. But when I cross-compile it with mingw, gcc complains about the warning:

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

Therefore, obviously, this option is not needed for Windows code. This is just a warning, but I still want to get rid of it. How can i do this? Perhaps there is already a libtool / autoconf function that checks if this option is supported and only installs it when necessary, so I don’t need to do this manually in Makefile.am?

+3
source share
2 answers

You do not need to install it -fPICmanually, it libtoolwill add it if you say what type of binary / library you are building.

lib_LTLIBRARIES = mylibrary.la
mylibrary_la_SOURCES = mylibrary.c

This can provide both mylibrary.sowith PIC (if necessary) and mylibrary.awithout, depending on other Autoconf / Automake parameters. (Perhaps something like .dll, and .libin Windows, but I do not use this platform.)

+4
source

I used the following in configure.ac to add -fpIC conditionally:

AC_MSG_CHECKING(whether fPIC compiler option is accepted)
SAVED_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -fPIC -Werror"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [return 0;])],
    [AC_MSG_RESULT(yes)
     CFLAGS="$SAVED_CFLAGS -fPIC"],
    [AC_MSG_RESULT(no)
     CFLAGS="$SAVED_CFLAGS"])
+1
source

Source: https://habr.com/ru/post/1787545/


All Articles