https://github.com/vaporexampleslab/tinkerevalstaticcli
Sample code for StackOverflow question.
https://github.com/vaporexampleslab/tinkerevalstaticcli
Last synced: 3 months ago
JSON representation
Sample code for StackOverflow question.
- Host: GitHub
- URL: https://github.com/vaporexampleslab/tinkerevalstaticcli
- Owner: VaporExamplesLab
- License: unlicense
- Created: 2018-01-21T03:17:01.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-01-05T23:01:29.000Z (over 4 years ago)
- Last Synced: 2024-12-31T21:26:12.959Z (over 1 year ago)
- Language: Swift
- Homepage: https://stackoverflow.com/questions/48363996
- Size: 11.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TinkerEvalStaticCli
**Question:** After `generate-xcodeproj`, what Xcode settings are needed to statically link a Swift Package Library into a simple command line executable … similar to what is produced by corresponding `swift build`?
**Goal:** Produce a statically linked executable with Xcode like what is produced by `swift build` from the command line _… for the same module product._
The issue is that the `generate-xcodeproj` produced project creates a dynamically linked product (not expected) while `swift build` produces a statically linked product (as expected).
**Example:** Consider two Swift Manager Packages (SPM) packages:
1. static library: [`TinkerEvalStaticLib`⇗](https://github.com/VaporExamplesLab/TinkerEvalStaticLib) with `swift package init --type library` as the starting point.
2. command line executable: [`TinkerEvalStaticCli`⇗](https://github.com/VaporExamplesLab/TinkerEvalStaticCli) with `swift package init --type executable` as the starting point.
`TinkerEvalStaticLib` is simple and expressly set to a type `.static`.
_TinkerEvalStaticLib.swift_
``` swift
public class TinkerEvalStaticLib {
public static func printSomething() -> String {
let s = "Library says, 'Hello.'"
print(s)
return s
}
}
```
_Package.swift_
``` swift
…
products: [
.library(name: "TinkerEvalStaticLib",
type: .static, // expressly set to be a static library
…
```
`TinkerEvalStaticCli` is also simple and has `TinkerEvalStaticLib` as a dependency.
_main.swift_
``` swift
import TinkerEvalStaticLib
let _ = TinkerEvalStaticLib.printSomething()
```
_Package.swift_
``` swift
…
name: "TinkerEvalStaticCli",
dependencies: [
.package(
url: "git@github.com:VaporExamplesLab/TinkerEvalStaticLib.git",
.branch("master") ),
],
…
```
**Observation:**
1. In the `TinkerEvalStaticCli` directory, `swift build` will create a standalone relocatable, statically linked executable.
```
prompt> ./TinkerEvalStaticCli_spm
Library says, 'Hello.'
```
2. However, the Xcode project created with `swift package generate-xcodeproj` creates a dynamically linked executable that requires the separate library.
```
prompt> ./TinkerEvalStaticCli_xc
dyld: Library not loaded: @rpath/TinkerEvalStaticLib.framework/Versions/A/TinkerEvalStaticLib
Referenced from: /Users/…/./TinkerEvalStaticCli_xc
Reason: image not found
Abort trap: 6
```
**Note:** One would expect that an `swift package generate-xcodeproj` generated Xcode project would build a static linked binary (instead of a dynamic linked binary) similar to the static linked binary produced by the corresponding `swift build`. So, this question is for a workaround to a current SPM `generate-xcodeproj` limitation.