1

I'm new to swift and would like to learn more about C integration with swift. It should be quite trivial by adding module.modulemap and file.h file into its Sources area. The tricky part is how to instruct swift build command to pick up proper include path to look-up C header files. Here is an example. I followed fast.ai course and they provided SwiftSox example how to integrate C sox library. So, we create a new swift package. Then we create sox area:

Sources/sox/
├── module.modulemap
└── soxu.h

The module.modulemap file contains

module sox [system] {
   umbrella header "soxu.h"
   link "sox"
   export *
}

The soxu.h file has

#include <sox.h>

The Package.swift file has the following:

import PackageDescription

let package = Package(
name: "SwiftSox",
dependencies: [],
targets: [
    .target(
        name: "SwiftSox",
        dependencies: ["sox"]),
    .testTarget(
        name: "SwiftSoxTests",
        dependencies: ["SwiftSox"]),
    .systemLibrary( name: "sox", pkgConfig: "sox")
]
)

Now, my sox C library is installed in /opt/local area. And, when I try to build package I see that swift build picks-up Xcode include path instead of /opt/local/include and fails to compile since they are not compatible. So, I need to instruct swift build to pick-up /opt/local/include (similar to -I and -L in C/C++ world) areas for the build to resolve all my header files. How I can do it?

Here is an error I see

 swift build
/opt/local/include/sox.h:22:10: note: while building module 'Darwin' imported from /opt/local/include/sox.h:22:
#include <limits.h>
         ^
<module-includes>:357:9: note: in file included from <module-includes>:357:
#import "ncurses.h"
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include/ncurses.h:141:10: note: in file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include/ncurses.h:141:
#include <unctrl.h>
         ^
/opt/local/include/unctrl.h:61:63: error: unknown type name 'SCREEN'
NCURSES_EXPORT(NCURSES_CONST char *) NCURSES_SP_NAME(unctrl) (SCREEN*, chtype);
                                                              ^
/opt/local/include/sox.h:22:10: note: while building module 'Darwin' imported from /opt/local/include/sox.h:22:
#include <limits.h>
         ^
<module-includes>:357:9: note: in file included from <module-includes>:357:
#import "ncurses.h"
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include/ncurses.h:141:10: note: in file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include/ncurses.h:141:
#include <unctrl.h>
         ^
/opt/local/include/unctrl.h:61:53: error: function cannot return function type 'char *(int *, chtype)' (aka 'char *(int *, unsigned int)')
NCURSES_EXPORT(NCURSES_CONST char *) NCURSES_SP_NAME(unctrl) (SCREEN*, chtype);
                                                    ^
/opt/local/include/sox.h:22:10: note: while building module 'Darwin' imported from /opt/local/include/sox.h:22:
#include <limits.h>
         ^
<module-includes>:357:9: note: in file included from <module-includes>:357:
#import "ncurses.h"
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include/ncurses.h:141:10: note: in file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include/ncurses.h:141:
#include <unctrl.h>
         ^
/opt/local/include/unctrl.h:61:54: error: a parameter list without types is only allowed in a function definition
NCURSES_EXPORT(NCURSES_CONST char *) NCURSES_SP_NAME(unctrl) (SCREEN*, chtype);
                                                     ^
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "soxu.h"
        ^
/Users/vk/Work/Languages/Swift/tmp/SwiftSox/Sources/sox/soxu.h:1:10: note: in file included from /Users/vk/Work/Languages/Swift/tmp/SwiftSox/Sources/sox/soxu.h:1:
#include <sox.h>
         ^
/opt/local/include/sox.h:22:10: error: could not build module 'Darwin'
#include <limits.h>
         ^
/Users/vk/Work/Languages/Swift/tmp/SwiftSox/Sources/SwiftSox/main.swift:1:8: error: could not build Objective-C module 'sox'
import sox

The issue here is that swift build picks ncurses.h from Xcode area instead of /opt/local/include and it is not compatible with the rest of include files from /opt/local.

Valentin
  • 1,492
  • 3
  • 18
  • 27

0 Answers0