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

时间:2016-11-21 16:43:03

标签: xcode cocoapods afnetworking alamofire podfile

几个星期以来,我一直在努力解决这个问题。为什么以下macOS设置会产生Alamofire链接器错误?

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

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

    平台:osx,'10 .10' 目标' testMacOS'做 use_frameworks! pod' Alamofire','〜> 4.0' 端

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

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

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

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

3 个答案:

答案 0 :(得分:8)

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

例如,您的Alamofire框架位于“$(ProductDirectory)/Alamofire/Alamofire.framework”。因此,您需要将“@ executable_path / Alamofire /”添加到您自己的目标的建筑设置 - 运行路径搜索路径

此外,“Pods Project - Alamofire(Target)”还需要指定swift运行时动态库位置。在我的环境中,我需要在“构建设置 - 运行路径搜索路径”中添加“/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx”。

但为方便起见,您可以查看我的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

答案 1 :(得分:2)

使用Xcode 8.1的

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

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

答案 2 :(得分:1)

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