Conditionally disable shared library build

2019-07-21 08:01发布

问题:

I’ve written a C library that builds using Libtool, and I’d like to only build static libraries on Cygwin. To that end, I placed

if test "$target_os" = "cygwin"; then
    AC_DISABLE_SHARED
fi

in my configure.ac.

This does indeed disable building shared libraries on Cygwin; however, it also disables building them everywhere else. I assume this is because expanding AC_DISABLE_SHARED causes some unfortunate side effects.

How can I use Libtool to avoid building shared libraries on Cygwin while still building them on other platforms?

回答1:

I'm not sure $target_os is what you want. $host_os is the name for the system the code will run on. The 'target' triple is rarely used outside of building compilers / toolchains.

Even though the configure script might still say yes / enabled for shared libraries, you can override the result by setting the enable_shared|static variables.

AC_CANONICAL_HOST
...
LT_INIT

case $host_os in
  cygwin*)
    AC_MSG_RESULT([explicitly disabled shared libraries for $host])
    enable_shared=no; enable_static=yes ;;
esac

These variables aren't documented, so it's technically a hack - but it's basically behaving like any other AC_ARG_ENABLE option. Your original idea might still work if it appears before LT_INIT, but this approach overrides any configure options.