【问题标题】:How to link CocoaPods library without Xcode (integrate_targets false)如何在没有 Xcode 的情况下链接 CocoaPods 库(integrate_targets false)
【发布时间】:2019-03-06 04:34:22
【问题描述】:

我有一个没有 Xcode 的 swift 项目。我想使用来自 CocoaPods 的库。鉴于以下Podfile

platform :osx, '10.11'
install! 'cocoapods', :integrate_targets => false
target 'Foo' do
  pod "PlainPing"
end
pre_install do |installer|
  installer.analysis_result.specifications.each do |s|
    s.swift_version = '4.2' unless s.swift_version
  end
end

我可以轻松地将库构建到.a.swiftmodule 文件中:

pod install
cd Pods
xcodebuild

但使用来自swiftc 的编译库似乎很棘手,我无法猜测正确的搜索路径拼写或谷歌搜索它们。我最好的选择:

swiftc -I ./build/Release/PlainPing -L ./build/Release/PlainPing -lPlainPing main.swift 

失败

main.swift:2:8: error: cannot load underlying module for 'PlainPing'

-L 库搜索路径似乎正常工作,但 swiftc 缺少实际使用 .a 库文件的内容。

【问题讨论】:

    标签: swift xcode macos cocoa cocoapods


    【解决方案1】:

    手动编译 CocoaPod 的版本

    要编译包含 Swift 和 Objective-C 代码的 CocoaPods 库,请执行以下操作:

    1. 将您的main.swift 复制到Pods/PlainPing/Pod/Classes
    2. 编译PlainPingObjective-C代码
    3. 发射PlainPing.swiftmodule
    4. 编译PlainPing发射对象
    5. 编译应用程序,包括发射对象和main.swift

    脚本(复制并粘贴到终端):

     echo "1. Compiling Objective-C code" && \
     clang \
          -c \
          -fmodules \
          -fobjc-weak \
          -arch x86_64 \
          -w \
          SimplePing.m \
     && \
     echo "2. Emitting PlainPing.swiftmodule" && \
     swiftc \
          -emit-module \
          -module-name PlainPing \
          -import-objc-header SimplePing.h \
          PlainPing.swift SimplePingAdapter.swift \
     && \
     echo "3. Compiling PlainPing" && \
     swiftc \
          -emit-object \
          -module-name PlainPing \
          -import-objc-header SimplePing.h \
          PlainPing.swift SimplePingAdapter.swift \
     && \
     echo "4. Compiling app" && \
     swiftc \
          -o app \
          -I . \
          -L . \
          *.o main.swift \
     && \
     echo "5. Running app" && \
     ./app
    

    使用 xcodebuild 的版本

    确保在Podfile的第二行添加use_frameworks!

    脚本(复制并粘贴到终端):

    echo "Compiling Pods" && \
    pod install && \
    xcodebuild -project Pods/Pods.xcodeproj \
    && \
    echo "Compiling App" && \
    dir="build/Pods.build/Release/PlainPing.build/Objects-normal/x86_64" && \
    swiftc \
      -o app \
      -L "$dir" \
      -I "$dir" \
      main.swift \
      $(find $dir -name '*.o' -exec echo -n '{} ' \;) \
    && \
    echo "Running App" && \
    ./app
    

    示例 main.swift

    import Foundation
    import PlainPing
    
    PlainPing.ping("www.apple.com", withTimeout: 1.0, completionBlock: { (timeElapsed:Double?, error:Error?) in
        if let latency = timeElapsed {
            print("latency (ms): \(latency)")
        }
    
        if let error = error {
            print("error: \(error.localizedDescription)")
        }
    })
    
    RunLoop.main.run()
    

    【讨论】:

    • 它编译,非常感谢!一个后续问题,也许您也知道:在运行生成的应用程序时,它会因“库未加载”而失败。似乎“swiftc”动态链接到框架的库中。有什么方法可以强制它成为静态(嵌入)链接?
    • 你也可以在不生成框架的情况下尝试构建依赖,看看这个:stackoverflow.com/questions/24131476/…
    • 所有这些答案都类似于“CLI 应用程序不支持框架”。所以没有办法将 Cocoapods 库添加到我简单的 swift CLI 应用程序中以便它可以工作?
    • 似乎swiftc将“LC_RPATH”设置为“/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx”所以似乎没有简单的方法用于已编译的应用程序以加载框架。
    猜你喜欢
    • 2016-03-15
    • 2012-10-05
    • 1970-01-01
    • 2015-08-31
    • 2017-02-23
    • 2012-08-11
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多