https://github.com/luispadron/swift-package-defines-example
Example Swift Package using SWIFT_PACKAGE define
https://github.com/luispadron/swift-package-defines-example
Last synced: 9 months ago
JSON representation
Example Swift Package using SWIFT_PACKAGE define
- Host: GitHub
- URL: https://github.com/luispadron/swift-package-defines-example
- Owner: luispadron
- Created: 2024-09-26T02:34:33.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-15T16:04:40.000Z (over 1 year ago)
- Last Synced: 2025-08-21T09:30:10.128Z (10 months ago)
- Language: Swift
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Example Swift Package with SWIFT_PACKAGE define usage
This repository showcases two targets, a Swift and Objective-C target, that use the `SWIFT_PACKAGE` define to conditionally compile code.
Defined in the Objective-C target's header file behind the `SWIFT_PACKAGE` define:
```c
#if SWIFT_PACKAGE
int foo();
#endif
```
Then used in the Swift target:
```swift
import Foo
public func fooSwift() {
print("FooSwift")
print("ObjC foo: \(foo())")
}
```
Without proper compiler options, the defines are not set correctly and `foo` ends up being undefined.
```swift
swift build
```
Fails with:
```sh
swift-package-defines-example/Sources/FooSwift/Foo.swift:5:24: error: cannot find 'foo' in scope
3 | public func fooSwift() {
4 | print("FooSwift")
5 | print("ObjC foo: \(foo())")
| `- error: cannot find 'foo' in scope
6 | }
7 |
```
If we add `cSettings` to the `Package.swift` file, we can set the `SWIFT_PACKAGE` define:
```swift
.target(
name: "FooSwift",
dependencies: ["Foo"],
cSettings: [
.define("SWIFT_PACKAGE"),
]
),
```
If we run `swift build --verbose` we can see the compiler options being set by SwiftPM:
```sh
-Xcc -DSWIFT_PACKAGE
```
When the C compiler is invoked and
```sh
-D SWIFT_PACKAGE
```
When the Swift compiler is invoked.