Skip to content

Instantly share code, notes, and snippets.

Last active Feb 4, 2017
What would you like to do?

Creating a Package

You deal with three kinds of packages: wrapping headers and libraries, building custom libraries, and wrapping Swift. This is the first type.

  1. Create a new folder. Name it, e.g. BasePackage
  2. Add four text files: base.h, Makefile, module.modulemap, Package.swift

Building the Package.swift file

import PackageDescription

let package = Package(
    name: "BasePackage"
  • Adjust the package name as desired. Here it is BasePackage.

Building the header file

Rename as desired, add any system includes

// Add required includes
#include <math.h>

Building the file

Thanks, aciidb, for cleaning up the's header line

module BasePackage {
   header "base.h"
   link "m"
   export *
  • adjust the module name as needed
  • change the folder path to match the path to the header file
  • include all libraries to link (here -lm)

Makefile for Package

You can just copy and use this as-is

	git init ; git add . ; git commit -m "Commit" ; git tag 1.0.0

	rm -rf .git
	rm *~

	rm *~

Building a module-consuming program

  1. Create a directory
  2. Add Package.swift text file
  3. Create Sources folder. Add main.swift text file to sources.
  4. Add Makefile text file

Edit Packages.swift

import PackageDescription
let package = Package (
    name: "test",
    dependencies: [
        .Package(url: "/home/erica/Packages/BasePackage", majorVersion:1)
  • Change the executable name from test
  • Change the url to point to the custom module folder

Edit main.swift

Use the package you actually want

import BasePackage

print("Hello world")

Edit Makefile

Here's a basic Makefile for building your app. Edit the target name.

	swift build

	cp .build/debug/$(TARGET) .

clean :
	rm -rf Packages
	rm -rf .build
	rm *~ Sources/*~ $(TARGET)

        rm *~ Sources/*~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment