If you are running 32-bit 3.6 on Windows, please test this

Pavol Lisy pavol.lisy at gmail.com
Sat Sep 2 05:12:08 EDT 2017


On 9/2/17, eryk sun <eryksun at gmail.com> wrote:
> On Fri, Sep 1, 2017 at 3:23 AM, Peter Otten <__peter__ at web.de> wrote:
>>
>> I think you have to specify the types yourself:
>>
>>>>> import ctypes
>>>>> libm = ctypes.cdll.LoadLibrary("libm.so")
>>>>> libm.sqrt(42)
>> 0
>>>>> libm.sqrt.argtypes = [ctypes.c_double]
>>>>> libm.sqrt.restype = ctypes.c_double
>>>>> libm.sqrt(42)
>> 6.48074069840786
>
> On POSIX systems, use ctypes.util.find_library('m'), which, for
> example, resolves to "libm.so.6" in Ubuntu Linux 16.04. On the same
> system, "libm.so" is an ld script that's used by the compile-time
> linker.
>
>     $ cat /usr/lib/x86_64-linux-gnu/libm.so
>     /* GNU ld script
>     */
>     OUTPUT_FORMAT(elf64-x86-64)
>     GROUP ( /lib/x86_64-linux-gnu/libm.so.6
>         AS_NEEDED (
>             /usr/lib/x86_64-linux-gnu/libmvec_nonshared.a
>             /lib/x86_64-linux-gnu/libmvec.so.1 ) )
>
> The runtime linker doesn't know how to handle this script.
>
>     >>> ctypes.CDLL('libm.so')
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in <module>
>       File "/usr/lib/python3.5/ctypes/__init__.py",
>         line 347, in __init__
>         self._handle = _dlopen(self._name, mode)
>     OSError: /usr/lib/x86_64-linux-gnu/libm.so: invalid ELF header

Peter and Eryk thanks very much! :)

BTW julia on ubuntu 16.04 could do it ->

julia> result = ccall((:sqrt, "libm"), Cdouble, (Cdouble,), 1.3)
1.140175425099138

but I am afraid that it probably uses some llvm's magic so it could
not help to solve this problem in ctypes...



More information about the Python-list mailing list