You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftPM生成Xcode项目后静态链接实现方案(含工具废弃说明)

Solution for Static Linking SwiftPM Static Libraries in Xcode 9/Swift 4

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 Type setting and change it from Dynamic Library to Static 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

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 TinkerEvalStaticLib is listed here (it should be, but double-check)
  • Switch to Build Settings for this target:
    • Find Other Linker Flags and add -all_load (or -force_load $(BUILT_PRODUCTS_DIR)/libTinkerEvalStaticLib.a if 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

Step 3: Validate the Build

  • Clean the project with Cmd+Shift+K to 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 .xcodeproj file
  • 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_load to your main target's Other Linker Flags to 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 Paths in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:50:05