Home > Software engineering >  Python's CFFI - Pass in floating point value #define directives to cdef()
Python's CFFI - Pass in floating point value #define directives to cdef()

Time:12-18

CFFI's documentation says that it supports the use of constants and macros (like #define) in the cdef "provided the macro is defined to be an integer value", but it doesn't explicitly say that floating point constants are not supported. I've tried using the #define FOO ... syntax with floats and #define FOO 0.5 but both fail, and have not found any more information on how I can possibly use floating point values in this manner. I'm trying to call functions from my C libraries from Python.

Is there a way to work around this limitation (if indeed it's a limitation) without touching the C code?

CodePudding user response:

Only integers are supported. For things like floats, there are two solutions:

  1. You can of course write the constant as pure Python code, if it has a known value, and use that from the rest of your Python code. In other words, put it outside CFFI.

  2. This works in API mode: static const float FOO; declares FOO as something that can be read from in C, but not written to.

  • Related