Canonical way to getifaddrs with SPM and Xcode


Hi all,

I would like to know the canonical way to call C’s getifaddrs, defined in ifaddr.h, using Swift 3. It should compile from both Xcode and SPM (Darwin and Glibc). As this header is not imported by the Darwin (/Glibc as well?) library, so the header would need to be imported by my own code. I tried two different approaches, but both failed;

1. Define a system module package;

module Cifaddrs [system] [extern_c] {
  header "/usr/include/ifaddrs.h"
  export *

With both Xcode/SPM it fails with:

/Applications/ error: redefinition of module 'Compression'
module Compression [system] [extern_c] {

2. Define a Clang module;

#import <ifaddrs.h>


In Xcode it fails with:

[…]Sources/Cifaddrs/include/Cifaddrs.h:1:9: Include of non-modular header inside framework module 'Cifaddrs'

Note that setting CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES doesn’t affect Swift compiler.

Building using ``swift build`` however works.

Both approaches feel like convoluted ways for something that would’ve been very easy with plain C. Many thanks in advantage for showing me an easier way :).