1. Python CFFI
  2. Untitled project
  3. cffi
Issue #74 invalid

linking issue with math functions

Flávio Coelho
created an issue

I a have this simple example which requires (in straight C) linking with -lm to work.

ffi.cdef("""
    unsigned long bit_length(long n){
        if (n == 0)
            return 1;
    return floor(log2(abs(n)));
    };
""")

lib = ffi.verify("""
    #include <math.h>
    #include <stdio.h>
    #include <stdlib.h>
    """)

but with cffi I am getting the following error:

Traceback (most recent call last): File "/home/fccoelho/Documents/Projects_Software/bsmatcomp/bscompression/bsarray.py", line 279, in <module> """) File "/home/fccoelho/.virtualenvs/py3bsmat/lib/python3.2/site-packages/cffi/api.py", line 96, in cdef self._parser.parse(csource, override=override) File "/home/fccoelho/.virtualenvs/py3bsmat/lib/python3.2/site-packages/cffi/cparser.py", line 119, in parse self._internal_parse(csource) File "/home/fccoelho/.virtualenvs/py3bsmat/lib/python3.2/site-packages/cffi/cparser.py", line 159, in _internal_parse raise api.CDefError("unrecognized construct", decl) cffi.api.CDefError: line 18: unrecognized construct

Comments (2)

  1. Armin Rigo

    There are two issues. The "-lm" needs to be written as "libraries=['m']" in verify(). But the first issue is that you cannot write the body of a C function in cdef(). Corrected example:

    ffi.cdef("""
        unsigned long bit_length(long n);
    """)
    lib = ffi.verify("""
        #include <math.h>
        unsigned long bit_length(long n){
            if (n == 0)
                return 1;
        return floor(log2(abs(n)));
        };
    """, libraries=['m'])
    
  2. Log in to comment