4

我已经为此苦苦挣扎了好几个星期了。为什么以下 macOS 设置会出现 Alamofire 链接器错误?

重新创建链接器错误的步骤:

  • 创建新的 macOS 命令行应用程序
  • 从终端更新运行 pod init
  • 创建以下 podfile:

    platform :osx, '10.10' target 'testMacOS' do use_frameworks!pod 'Alamofire', '~> 4.0' 结束

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

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

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

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

4

3 回答 3

12

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

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

此外,“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
于 2017-05-03T15:58:17.513 回答
3

问题: 使用 Xcode 8.1,我没有意识到 macOS 命令行应用程序不支持框架(动态库),就像 iOS 或桌面 macOS 应用程序使用捆绑包一样。

解决方案: 我通过从 github 上取下源代码并在我的工作区中编译它来解决这个问题。那行得通。导入静态库也有效。

于 2016-12-06T08:58:10.960 回答
2

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

于 2016-12-08T18:24:26.633 回答