Re: Generating struct members from c structs
On Wednesday, 1 July 2020 at 07:26:44 UTC, Anthony wrote: When doing interop with a c library, is there a way to automatically generate the fields that are needed for a struct? [snip] Is there an easier way though? Dstep is probably what you're looking for: https://github.com/jacob-carlborg/dstep It eats C header files and creates appropriate D files from them. -- Simen
Generating struct members from c structs
When doing interop with a c library, is there a way to automatically generate the fields that are needed for a struct? For example, when interfacing with the lwan c library: // lwan.h struct lwan { struct lwan_trie url_map_trie; struct lwan_connection *conns; struct lwan_strbuf headers; struct { pthread_barrier_t barrier; struct lwan_thread *threads; unsigned int max_fd; unsigned int count; } thread; struct lwan_config config; struct coro_switcher switcher; int main_socket; unsigned int n_cpus; }; module lwan; extern (C) { struct lwan { ... // <<< How do I populate this without having to write all the sub structs? }; void lwan_init(lwan* l); void lwan_shutdown(lwan* l); } import lwan; void main() { lwan l; //This is currently not allocating enough memory lwan_init(&l); lwan_shutdown(&l); } How do I populate the lwan struct without having to write all the sub structs? I'm thinking that maybe it'll be best to create a wrapper c function that initializes the lwan struct and returns a pointer. That way I don't have to declare any fields. extern (C) { struct lwan; lwan* lwan_make(); //wrapper function (perhaps in wrapper.c) void lwan_cleanup(lwan* l); //wrapper function void lwan_init(lwan* l); void lwan_shutdown(lwan* l); } Is there an easier way though?