SwiftPM生成Xcode项目后静态链接实现方案(含工具废弃说明)
I get exactly where you're coming from—swift build gives you that nice self-contained static executable, but generate-xcodeproj defaults to dynamic frameworks which break when you run the binary standalone. Here's how to fix that for your setup:
Step 1: Convert the Static Library Target to Actual Static Output
First, let's fix the library project itself to match your SwiftPM configuration:
- Open the Xcode project generated by
swift package generate-xcodeproj - Locate your static library target (in your example,
TinkerEvalStaticLib) in the project navigator - Go to Build Settings for this target:
- Find the
Mach-O Typesetting and change it fromDynamic LibrarytoStatic Library - If there's a Copy Files build phase (usually set to copy frameworks to the tool's Frameworks directory), delete it—static libraries don't need this step
- Find the
Step 2: Adjust the Command Line Tool Target to Static Link the Library
Next, update your executable target to properly link the static library, matching swift build's behavior:
- Select your command line tool target (
TinkerEvalStaticCLI) - Head to Build Phases > Link Binary With Libraries—make sure
TinkerEvalStaticLibis listed here (it should be, but double-check) - Switch to Build Settings for this target:
- Find
Other Linker Flagsand add-all_load(or-force_load $(BUILT_PRODUCTS_DIR)/libTinkerEvalStaticLib.aif you need more granular control) — this ensures all symbols from the Swift static library are included in the final executable - Remove any dynamic-library-related paths from
Runpath Search Paths(like@executable_path/../Frameworks) since they're irrelevant for static linking
- Find
Step 3: Validate the Build
- Clean the project with
Cmd+Shift+Kto wipe old build artifacts - Build the project with
Cmd+B - Run the generated executable—you should see the same output as
swift build, with no dyld load errors
Using the Static Library as a Subproject in Non-SwiftPM Xcode Projects
If you want to bring this adjusted static library project into a non-SwiftPM Xcode project:
- In your main Xcode project, go to
File > Add Files to "[Your Project]"and select the adjusted static library.xcodeprojfile - In your main project's target, go to Build Phases > Link Binary With Libraries and add the static library product (it'll show up as
libTinkerEvalStaticLib.a) - Add
-all_loadto your main target'sOther Linker Flagsto ensure Swift symbols are linked correctly - Xcode should handle header paths automatically for Swift libraries, but if you run into issues, add the library's source directory to
Header Search Pathsin your main target's Build Settings
A Note on generate-xcodeproj Deprecation
Just a heads-up: PR 3062 marked generate-xcodeproj as deprecated, and it's since been removed from the Swift Package Manager command set. For newer Xcode versions, the recommended approach is to use File > Add Packages... to directly import SwiftPM dependencies into your Xcode project. But for your Xcode 9/Swift 4 setup, the steps above will get you the static-linked executable you need.
内容的提问来源于stack exchange,提问作者marc-medley

