Hi,
I'm trying to create Nim bindings for an arcane C API that uses struct packing specified using #pragma pack(push, n) all over the headers. What is the most effective way to achieve such struct packing in Nim Objects. Here's an example in C
#pragma pack(push, 8)
struct A {
int m_0;
double m_1;
void* m_2;
/* so on */
...
char m_28
};
#pragma pack(pop)
Ideally it would be great if they can be set at a scope or object level.
I think packed pragma can be used for your case: https://nim-lang.org/docs/manual.html#foreign-function-interface-packed-pragma
There is push and pop pragmas: https://nim-lang.org/docs/manual.html#pragmas-push-and-pop-pragmas
Thanks for your reply. I don't necessarily need them always tightly packed, but with a byte boundary. The 'n' byte part in the C pragma compiler directive.
Is it possible to do like this? I tried but it doesn't seem to compile.
type
TestObj {.packed(8).} = object
m_1 : int64
m2 : float64
m3 : uint8
Ok I think the following compiles and works as expected.
type
TestObj {.packed.} = object
m_1 {.align(8).} : int64
m2 {.align(8).}: float64
m3 {.align(8).}: uint8
It would be great if we could set the alignment for all object fields with one pragma, but I guess one can write macros. If there is a better way to do this please let me know.