0

I'm writing a C extension for Python and need to pass the size of a C type to the compilation. So I'll want to do

extra_compile_args = ['-DSIZEOF_MYTYPE=32']

in my setup.py. My question is: how can I get the size? (32 here)

If my target were pure C and I use autoconf, then I could use AC_CHECK_SIZEOF, if writing a Ruby extension there is check_sizeof in the mkmf module, is there some similar facility (presumably, like autoconf and Ruby's mkmf, by making a test-compile which calls C's sizeof and prints the result to stdout, and capturing that) in Python? Or do I need to roll my own?

4
  • Does this answer your question? How do I determine the size of an object in Python? Commented Mar 6, 2023 at 14:04
  • Afraid not: tt's the size of a C type that I need. Commented Mar 6, 2023 at 14:04
  • 1
    struct.calcsize or ctypes.sizeof would probably be the most appropriate built-in tools for the job, but they can't parse your C headers to read your type definitions or anything like that, so you'd have to repeat the type definitions in a format struct or ctypes understands. Commented Mar 6, 2023 at 14:11
  • That sizeof works just fine -- many thanks! If you would like to convert that comment to an answer, I'll make as solved Commented Mar 6, 2023 at 14:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.