Importing C++ System Library to a C Target in order to use it from Swift

Hello,

I would like to use C++ system library from Swift.

Importing this SystemLibrary target directly into Swift target does not work, since the library does not provide extern “C” API.

I wanted to introduce my own C API for the System Library using following hierarchy:

SystemLibrary(CppAdPlugSO) <-(step1)- Target(CAdPlugAPI) <-(step2)- Target(MySwiftCode)

But I am unable to import CppAdPlugSO to the CAdPlugAPI target. The CAdPlugAPI target may be C or C++, however not Objective-C since I need this to work on other platforms.

I have tried importing C target to other C target before using SPM GitHub - mikolasstuchlik/ImportTest: Import C module into C module using SPM but I seem to be unable to import SystemLibrary target to C target.

Any advice, please?

You should be able to make your CAdPlugAPI target depend on your system library, just like any other target, as long as they are both within the same package. For an example, see the GtkCHelpers target that depends on the CGtk system library in SwiftGtk. For your CAdPlugAPI to import C++ headers, it would need to be C++ (but it can, of course, expose a C wrapper API).

1 Like

Thank you for your advice. I have created example package GitHub - mikolasstuchlik/SwiftAdPlug with following results on macOS and Linux.

Linux

mikolas@mikolas-desktop:~/Developer/SwiftAdPlug$ swift build
Building for debugging...
In file included from /home/mikolas/Developer/SwiftAdPlug/Sources/CAdPlug/CAdPlug.cpp:2:
In file included from /usr/include/adplug/adplug.h:27:
In file included from /usr/include/adplug/player.h:27:
/usr/include/adplug/fprovide.h:26:10: fatal error: 'binio.h' file not found

         ^~~~~~~~~
1 error generated.
[0/1] Compiling CAdPlug CAdPlug.cpp

macOS

mikolasstuchlik@10 SwiftAdPlug % swift build                  
In file included from /Users/mikolasstuchlik/Developer/SwiftAdPlug/Sources/CAdPlug/CAdPlug.cpp:2:
In file included from /usr/local/include/adplug/adplug.h:27:
In file included from /usr/local/include/adplug/player.h:27:
/usr/local/include/adplug/fprovide.h:26:10: fatal error: 'binio.h' file not found
#include <binio.h>
         ^~~~~~~~~
1 error generated.
[0/1] Compiling CAdPlug CAdPlug.cpp

This is the same error I have encountered previously. It looks like I am able to import libbinio from the
SPM, but unlike the adplug, I am using prefix libbinio/ in my include: #include <libbinio/binio.h>.

I have assumed, that there is an issue in the adplug and created symbolic link on my Linux system:

$ sudo ln -s /usr/include/libbinio/binio.h /usr/include/binio.h

After creating the symbolic link, the build passed. However this doesn't look clean to me.

I am still not sure whether I am doing something wrong, or whether this is an issue with the adplug library...