Canonical way to getifaddrs with SPM and Xcode


#1

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/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/module.modulemap:7:8: error: redefinition of module 'Compression'
module Compression [system] [extern_c] {
       ^

2. Define a Clang module;

Sources/Cifaddrs/include/Cifaddrs.h:
#import <ifaddrs.h>

Sources/Cifaddrs/Cifaddrs.c:
(empty)

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 :).

···


Bouke