Swift Language Working with C and Objective-C Use a module map to import C headers

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any Swift Language Question?