ffi.error: struct tr_tcp_config: wrong total size (cdef says 24, but C compiler says 48). fix it or use "...;"

11 views
Skip to first unread message

jayesh bhoi

unread,
Jun 3, 2024, 5:39:51 AMJun 3
to python-cffi

Hello I have started using cffi==1.16.0

Now at function call rtrlib/rtr_manager.py it through bellow error

File "/mydir/rtr_myscript.py", line 52, in connectRtr mgr = RTRManager(server, 332) ^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.12/site-packages/rtrlib-0.1-py3.12-linux-x86_64.egg/rtrlib/rtr_manager.py", line 138, in __init__ self.tcp_config = ffi.new('struct tr_tcp_config *') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ffi.error: struct tr_tcp_config: wrong total size (cdef says 24, but C compiler says 48). fix it or use "...;" as the last field in the cdef for struct tr_tcp_config to make it flexible

so what could be the reason for this issue?

Armin Rigo

unread,
Jun 4, 2024, 5:31:37 AMJun 4
to pytho...@googlegroups.com
Hi,

On Mon, 3 Jun 2024 at 11:39, jayesh bhoi <jayesh...@gmail.com> wrote:
> ffi.error: struct tr_tcp_config: wrong total size (cdef says 24, but C compiler says 48). fix it or use "...;" as the last field in the cdef for struct tr_tcp_config to make it flexible
>
> so what could be the reason for this issue?

The ffi.cdef() contains a declaration of `struct tr_tcp_config`, but
that is not a correct one. You must find `struct tr_tcp_config`, both
in the cdef() and in the actual C header you are using, and compare
the two. You can either copy and paste the exact declaration from the
C header to the cdef(), or in some cases you can write in the cdef()
something more general and potentially portable (if you want help for
that case I'll need to know the original declaration from the header;
or read https://cffi.readthedocs.io/en/stable/cdef.html#letting-the-c-compiler-fill-the-gaps).


A bientôt,

Armin Rigo

Joel Vibert

unread,
Jun 4, 2024, 6:37:12 PMJun 4
to python-cffi
Hi,
Even if two declarations of structs are absolutely identical, they may correspond to distinct declarations depending on the context. The compilation context includes alignment options that can have an impact on the size of structs. It can even be the case with a single declaration within a single header file, if it is included by 2 different C files. For instance, you may have included header files that changed the settings for alignment, or have some defines that altered them. It is important to restore aligment options in your header files, if you have modified them (using pairs of pragmas such as:  #pragma pack(push,n) / #pragma pack(pop)). So check your alignment options and your defines.
Joël
Reply all
Reply to author
Forward
0 new messages