[swift-users] Importing Empty C Structs

Joe Groff jgroff at apple.com
Fri Feb 12 11:12:10 CST 2016


> On Feb 11, 2016, at 9:41 PM, Jeff Kelley via swift-users <swift-users at swift.org> wrote:
> 
> I’m trying to use the Swift Package Manager to import libgit2 and am running into some issues. I have libgit2 installed via Homebrew and have configured the module map thusly:
> 
>> module CGit2 [system] {
>>     header "/usr/local/include/git2.h"
>>     link "git2"
>>     export *
>> }
> 
> I can then use this with the SPM to initialize the Git system:
> 
>> import CGit2
>> 
>> let result = git_libgit2_init()
>> 
>> defer { git_libgit2_shutdown() }
>> 
>> print("Result: \(result)")
> 
> This all works fine. When I start to try to use it—for instance, opening a repository:
> 
>> var repo: git_repository = nil
>> 
>> let error = git_repository_open(&repo, "/Users/jeff/Projects/CGit2")
>> 
>> guard let repo = repo else { print("No repo!"); exit(error) }
> 
> I get an error: “use of undeclared type ‘git_repository’”
> 
> Here’s how git_repository is declared in /usr/local/include/git2/types.h:
> 
>> typedef struct git_repository git_repository;
> 
> An empty struct. Interesting. A quick sample project in Xcode shows that this line will not generate anything in the Swift generated interface. So it appears to me that this type is invisible to Swift.
> 
> Question: Is there a way to use libraries like this in Swift? Should I file a bug that typedef’d structs like this are not imported into the generated interface? Thanks in advance for any pointers!

If a struct is incomplete in C, it can only be manipulated via pointers. We don't import `git_repository`, but `git_repository*` should be imported as a typealias for COpaquePointer. We have plans to eventually improve the type safety here by importing the type, though even then, you would not be able to construct or pass around values of the incomplete type, only pointers.

-Joe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160212/2bddaae1/attachment.html>


More information about the swift-users mailing list