https://github.com/lemire/swiftcallingcheader
Calling a C header from Swift (example)
https://github.com/lemire/swiftcallingcheader
Last synced: 9 months ago
JSON representation
Calling a C header from Swift (example)
- Host: GitHub
- URL: https://github.com/lemire/swiftcallingcheader
- Owner: lemire
- Created: 2016-09-28T19:06:09.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2024-09-26T14:57:56.000Z (almost 2 years ago)
- Last Synced: 2025-02-01T21:11:12.438Z (over 1 year ago)
- Language: Swift
- Size: 8.79 KB
- Stars: 9
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Calling a C header from Swift (example)
I could not find a simple example of Swift code calling a C header. So I made one.
Usage:
```
swift build
.build/debug/SomeSwift
```
or
```
swift build --configuration release
.build/release/SomeSwift
```
## Explanation:
- Each subdirectory in "Sources" is effectively a subpackage.
- Create a package with your C code in it (``Sources/SomeC``).
- Create your Swift package (``Sources/SomeSwift``).
- Then establish a dependency in your ``Package.swift`` file : ``targets: [Target(name: "Bitset", dependencies: ["SwiftBitsetC"]),]``.
- In your C package, add an include directory with your header file. If you have C source code, put it directly in your C code package directory (``Sources/SomeC``). You need at least one source file (``somec.c``).
- In your Swift code, just do ``import SomeC`` and then you can call the C function as in ``SomeC.sayHello()``.
- Official apple document about this topic: https://www.swift.org/documentation/articles/wrapping-c-cpp-library-in-swift.html
That's it!
## For Xcode users (Mac only)
```bash
$ swift package generate-xcodeproj
generated: ./SwiftCallingCHeader.xcodeproj
$ open ./SwiftCallingCHeader.xcodeproj
```