I am using .dylib for using c function in swift package. I am using dlopen function of swift for using from c function . c function (i.e uplink function ) will return a structure . for this method i have to create typealias of c function .
C struture
typedef struct Uplink { long _handle; } UplinkRef;
C function declaration
extern UplinkRef uplink(UplinkConfig p0, char** p1);
uplink function in c will return UplinkRef structure.
So i have declared UplinkRef structure in swift for typealias
UplinkRef struct in swift
struct UplinkRef{
var _handle = -1
}
I have to create typealias in swift so that i can call c function
typealias uplink = @convention(c) ()->UplinkRef
Swift code that i am using is
let handle = dlopen("test.dylib", RTLD_LOCAL|RTLD_NOW)
if(handle != nil){
print("Handle")
let sym = dlsym(handle, "uplink")
if(sym != nil){
let uplink = unsafeBitCast(sym, to: uplink.self)
}
}
- Anyother method that i can use for calling c function from .dylib file ?
- How can i define structure (i.e UplinkRef) as returned type of typealise ?
i have used code below for declaring typealias
But getting error while bitCasttypealias Uplink1 = (()->(UplinkRef))
let uplink = unsafeBitCast(sym, to: uplink.self)
- How can i define structure (i.e UplinkRef) as argument of typealias ?
- Can i add Bridging-Header file in swift package because i was getting error of 2 different language not allowed ?