triple7
triple7

Reputation: 574

Swift: can't find function in imported c library

I've been gnawing bones on this for the past day and can't find a solution to it.

I'm creating a swift wrapper for cfitsio which is a fits format for astronomers:

https://heasarc.gsfc.nasa.gov/fitsio/

I've created a basic swift wrapper to access the above as cFitsIO, as a module, following the below tutorial: https://www.hackingwithswift.com/articles/87/how-to-wrap-a-c-library-in-swift

When I do swift run on the wrapper's main.swift file, I can see that the import is successful, but one of the functions, fits_open_file cannot be resolved. The reason why I say the import works is because swift proposes another function which is in the c library as an alternative.

The cfitsio page says to only include fitsio.h, so I'm expecting all functions to be called from there.

However upon cloning the GitHub and performing some grep, I can't find any signature for the fits_open_file function.

one more thing, when I create the swift wrapper, I use the pig-config --type system-module as I can also download cfitsio via brew, which puts it in my system /usr/local/include folder.

I read somewhere else about Xcode not properly finding nested headers, in the following link, but fitsio has a lot of header files, some with constants some with macros etc. I would expect a cleaner way of doing this import rather than tediously go through what each header file should have?

https://medium.com/shopify-mobile/wrapping-a-c-library-in-swift-part-1-6dd240070cef

Upvotes: 1

Views: 439

Answers (1)

Anatoli P
Anatoli P

Reputation: 4891

After a quick look at the source code provided on the nasa.gov site you referred to, one can see that fits_open_file is actually a macro defined in longnam.h as a call to ffopentest. The longnam.h header is included in fitsio.h, so the macro would be available if it was used in C/C++ code.

The problem is that Swift automatically imports only simple, constant-like macros, see this Apple doc: https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/using_imported_c_macros_in_swift.

Here is a related SO post: Accessing C macros in Swift

Upvotes: 1

Related Questions