Swift Language Use a module map to import C headers


Example

A module map can simply import mymodule by configuring it to read C header files and make them appear as Swift functions.

Place a file named module.modulemap inside a directory named mymodule:

directory structure

Inside the module map file:

// mymodule/module.modulemap
module mymodule {
    header "defs.h"
}

Then import the module:

// demo.swift
import mymodule
print("Empty color: \(Color())")

Use the -I directory flag to tell swiftc where to find the module:

swiftc -I . demo.swift   # "-I ." means "search for modules in the current directory"

For more information about the module map syntax, see the Clang documentation about module maps.