[Distutils] linking with libm

Oscar Benjamin oscar.benjamin at bristol.ac.uk
Wed Nov 23 15:30:48 CET 2011


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

Thanks, that does look better. If I understand correctly that snippet
will check to see if python was linked with libm and ensure that my
extension module will also be only if python was. I tested it with Linux
and Windows and it works and seems a lot more robust than what I had
before.

Would it be slightly better to test just for the existence of the LIBM
configuration variable rather than its value? I don't know of an
example, but I guess a system could have libm but use a compiler with a
different linker argument format.

e.g.
if 'libm' in sysconfig.get_config_vars()

Is there a more general way to discover if the compiler that is going to
be used knows of a library with a specified name? Something like

if get_compiler_type().library_exists(libname)

Thanks,
Oscar.

On Wed, Nov 23, 2011 at 01:22:00PM +0100, Ralf Schmitt wrote:
> 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)
> 
> -- 
> Cheers 
> Ralf


More information about the Distutils-SIG mailing list