This is an example package demonstrating how to use the C and C++ interoperability in Swift Package.
.
├── MathUtils.swift
│ ├── Package.swift
│ ├── Sources
│ │ ├── ExternalMathUtilsC
│ │ │ ├── include
│ │ │ │ └── math_utils.h
│ │ │ ├── lib
│ │ │ │ └── libmath_utils.a
│ │ │ ├── math_utils.pk
│ │ │ └── module.modulemap
│ │ ├── ExternalMathUtilsCxx
│ │ │ ├── include
│ │ │ │ └── complex_math.h
│ │ │ ├── lib
│ │ │ │ └── libcomplexmath.a
│ │ │ └── module.modulemap
│ │ ├── MathUtils
│ │ │ └── MathUtils.swift
│ │ ├── MathUtilsC
│ │ │ ├── cipher_kit.c
│ │ │ └── include
│ │ │ ├── cipher_kit.h
│ │ │ └── module.modulemap
│ │ ├── MathUtilsCxx
│ │ │ ├── include
│ │ │ │ ├── MathUtilsCxx.hpp
│ │ │ │ └── module.modulemap
│ │ │ └── MathUtilsCxx.cpp
│ │ └── MathUtilsExecutable
│ │ └── MathUtilsExecutable.swift
│ └── Tests
│ └── MathUtilsTests
│ └── MathUtilsTests.swift
├── MathUtilsC
│ ├── Makefile
│ ├── math_utils.c
│ ├── math_utils.h
├── MathUtilsCxx
│ ├── complex_math.cpp
│ ├── complex_math.h
│ └── Makefile
└── README.md
The /MathUtilsC
and /MathUtilsCxx
are just simple c library and c++ library that can be compiled using the companion Makefile
s.
The MathUtils.swift
is a swift package that references the two compiled static library above, in /MathUtils.swift/Sources/ExternalMathUtilsC
and /MathUtils.swift/Sources/ExternalMathUtilsCxx
, and contains a c++ target using the ExternalMathUtilsC`` and a swift target all three c/++ libraries. It also provides an executable
MathUtilsExecutable` that outputs the results by calling the swift target which uses the c and c++ library under the hood.
Note that extern "C"
is needed for the c headers in /MathUtils.swift/Sources/ExternalMathUtilsC
(see here).
cd MathUtils.swift
swift run