#Use Zig types with @cImported functions

1 messages · Page 1 of 1 (latest)

digital steeple
#

I used to import c functions with extern "c" to redefine their signature to use types defined in Zig. Works alright, but extern "c" doesn't work with inline functions: gives out error: UndefinedSymbolReference. So inline functions must be @cImported.

How do I use Zig types with @cImported functions?

#

Use Zig types with @cImported functions

indigo fog
#

@cImport will generate the actual code corresponding to inline functions (translating the C code in the header into Zig code). You can't use extern "c" because inline functions won't actually necessarily appear in the binary (that's basically what inline means!).

You can do the same thing manually: rewrite the inline function in Zig, using your own types. If you'd prefer to just copy the auto-translated implementation, you can get it using zig translate-c file.h (you'll want to just search through its output for the function name, it'll return a lot of junk).

digital steeple
# indigo fog `@cImport` will generate the actual code corresponding to inline functions (tran...

I reckoned that inline in c basically copies the contents of the function in-place. So when you think of it, of course it doesn't appear amidst the symbols.

I was asking in part because I did not know the implementation of the inline function – it's MacOS API. But now I see you can see implementations of some simple functions if you see the .h file directly.

Ok. Zig is too fun to not enjoy a bit of rewriting!

indigo fog
#

Yeah, so it's a bit weirder than that because C isn't technically obligated to respect the hint at all and often compilers will just ignore it, but the hint does guarantee that the function won't be exposed from the binary, like if you marked it static

cloud plinth