在 macOS 命令行应用程序上设置框架 - 原因:找不到图像

Setting up a Framework on macOS Command Line apps - Reason: image not found

几周来我一直在努力解决这个问题。为什么以下 macOS 设置会出现 Alamofire 链接器错误?

重现链接器错误的步骤:

运行pod install。打开并构建工作区

错误:dyld:库未加载:@rpath/Alamofire.framework/Versions/A/Alamofire 原因:找不到图片

此时,这个错误是有道理的。您需要转到目标/通用/链接框架和库。然后添加 Alamofire。现在 Alamofire 位于工作区的 Framework 目录中。

构建并运行。同样的错误。为什么?

问题: 使用 Xcode 8.1,我没有意识到 macOS 命令行应用程序不以与 [=18 相同的方式支持框架(动态库) =] 或桌面 macOS 应用程序使用捆绑包。

解决方案: 我通过关闭源代码 github 并在我的工作区内编译它来解决这个问题。那奏效了。导入静态库也有效。

命令行工具确实 "support" 框架,但不像应用程序包那样。您需要将引用的框架放在 @rpath 中,实际上是 ~/Library/Frameworks//Library/Frameworks/

您需要手动设置@rpath,Pod 安装框架在“$(ProductDirectory)/$(FrameworkName)/$(FrameworkName).framework”。

例如,您的 Alamofire 框架位于 "$(ProductDirectory)/Alamofire/Alamofire.framework"。因此,您需要在您自己的目标的 Building Settings - Runpath Search Paths.[=11= 中添加 "@executable_path/Alamofire/" ]

此外,"Pods Project - Alamofire(Target)"还需要指定swift运行时动态库位置。在我的环境中,我需要将“/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx”添加到"Building Settings - Runpath Search Paths".

不过为了方便,你可以看看我的pod post_install代码。

post_install do |installer|
    files = Dir.glob("*.xcodeproj")
    proj_file = files[0]
    app_project = Xcodeproj::Project.open(proj_file)

    app_project.native_targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['LD_RUNPATH_SEARCH_PATHS'] = '$(inherited) @executable_path/../Frameworks @loader_path/Frameworks'
            prefix = ' @executable_path/'

            # For each pod, add the framework path to LD_RUNPATH_SEARCH_PATHS
            installer.pods_project.targets.each do |pod_target|
                config.build_settings['LD_RUNPATH_SEARCH_PATHS'] = config.build_settings['LD_RUNPATH_SEARCH_PATHS'] + prefix + pod_target.name + '/'

                pod_target.build_configurations.each do |pod_config|
#if you want embed swift stdlib into every framework, uncommend 1,2 and commend 3,4
#1
#pod_config.build_settings['ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES'] = 'YES'
#2
#pod_config.build_settings['LD_RUNPATH_SEARCH_PATHS'] = '$(inherited) @executable_path/../Frameworks @loader_path/Frameworks'

#3
pod_config.build_settings['ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES'] = 'NO'
#4
pod_config.build_settings['LD_RUNPATH_SEARCH_PATHS'] = '$(inherited) /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/'
                end
            end
        end
    end

    app_project.save
end