[swift-users] Importing empty C structs

Rick Mann rmann at latencyzero.com
Tue Apr 18 17:49:53 CDT 2017


> On Apr 18, 2017, at 09:11 , Joe Groff <jgroff at apple.com> wrote:
> 
> 
>> On Apr 17, 2017, at 6:19 PM, Rick Mann via swift-users <swift-users at swift.org> wrote:
>> 
>> I'm trying to make a module out of a C library and header file. It has at least one empty struct:
>> 
>> struct lgs_context_t {};
>> 
>> and a function:
>> 
>> LGS_EXPORT struct lgs_context_t* lgs_init(const lgs_context_params_t* params);
>> 
>> Swift sees the function, I can call it and assign the result to a variable, but Xcode (as usual) fails to show me what it thinks the type is. So I can't declare a class member to hold the returned pointer.
>> 
>> I'm trying to declare the member like this:
>> 
>>   var	ctx: lgs_context_t?		//  Use of undeclared type 'lgs_context_t'
>> 
>> I finally tried calling it like this:
>> 
>>   let ctx: UnsafeMutablePointer = lgs_init(...)
>> 
>> and the resulting error message gave me OpaquePointer. But couldn't it just typealias lgs_context_t to OpaquePointer?
>> 
>> Is there any way to do this in the modulemap?
>> 
>> Thanks!
> 
> The compiler uses OpaquePointer for pointers to incomplete types in C. It sounds like you left lgs_context_t declared but not defined in your header, in other words you wrote 'struct lgs_context_t;' without braces. If it were truly defined to be empty, then the type should have been imported.

It's defined with the braces (and nothing inside them).


-- 
Rick Mann
rmann at latencyzero.com




More information about the swift-users mailing list