[Distutils] linking with libm

Ralf Schmitt ralf at systemexit.de
Wed Nov 23 13:22:00 CET 2011

Oscar Benjamin <oscar.benjamin at bristol.ac.uk> writes:

> Hello,
> I have an extension module that uses math.h. In order to compile it on
> Linux with gcc, it needs to link with libm. However, using
> libraries=['m'] leads to a linker error when using mingw32 on Windows.
> At the moment my setup.py contains something like:
> libraries = []
> if not 'win' in sys.platform:
>     libraries.append('m')

that fails on OS X - if I remember correctly

> This works for the two cases I user personally.  I'm guessing, though,
> that I should really be checking for something more specific, perhaps to
> do with the compiler that is being used. I guess that using math.h and
> wanting cross-platform compilation is probably quite a common case. So I
> wondered if anyone who knows more than me about c-compilers and
> distutils has a better suggestion for this.
> Apologies if this is mentioned somewhere already. The only thing I could
> find in the docs was instructions for adding linker options when
> installing python modules. Ideally, I'd like to be able to handle this
> in my setup.py without needing the setup.py user to manually alter the
> linker settings.

I'm not quite sure if it's the right way to do it, but I solved that
with the following code:

| from distutils import sysconfig
| if sysconfig.get_config_var("LIBM") == "-lm":
|     libraries = ["m"]
| else:
|     libraries = []

(e.g. in https://github.com/pediapress/timelib/commit/70a8a9c42ff005fbb3547cc6b300da05a42d0cf2)


More information about the Distutils-SIG mailing list