I'm trying to build Swift bindings for zeromq using czmq.
I've configured a bridging header in xcode and it seems to understand that, but I still get this error:
/Users/jjl/code/swiftzmq/src/zsock.swift:47:37: error: use of undeclared type 'zsock_t'
typealias zsock_ptr = UnsafePointer<zsock_t>
Using this in C, I would have this type available if I did this (which is the exact contents of my bridging header):
#include <czmq.h>
I know it's being included because it was complaining about something in one of the header files until I was linking against the correct library path.
I don't know where to go from here. Any help you can offer would be appreciated
My project is at https://github.com/jjl/swiftzmq
I figured out what was going on. The swift interop layer isn't smart enough to understand the tricks the author of czmq plays on the compiler toolchain in the name of better code, so we have to do some C wrapping to hide it from swift.
The easy bits:
#include <czmq.h>
line from your bridging headerszsocket.h
because it's the swift zsocket wrapper)#import <czmq.h>
The harder bits:
You need to recreate any structures in your header that are used by the library. In my case the
zsock_t
structure which was defined as follows:We create a simple wrapper like so:
You will also need to recreate any typedefs used in those structures. In this case, handily no others. These all go in the new header (.h) file
You then need to wrap every function in the library that accepts one of these structures. We take
zsock_new
function as an example.First we need to predeclare our version in the header to avoid any zsock types. We just replace every occurrence of
zsock
withszsock
(emacs can help with this):Next we need to create the wrapper function in the .c file:
Notice how we cast between
zsock_t
andszsock_t
and use the internal zsock functions. It's safe to do so because the swift compiler won't be reading it, just the c compiler.Next up, there were a bunch of varargs functions. This worked for me:
Good luck to anyone reading wrapping a library in swift!