xcode 如何在具有不同配置名称的同一工作区中使用应用程序和库编译项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30884778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:19:35  来源:igfitidea点击:

How to compile a project with app and library in the same workspace with different configuration names?

xcodexcode-workspace

提问by 0xced

I am developing an app and I am using an open source component.

我正在开发一个应用程序,我正在使用一个开源组件。

I have a workspace containing both MyApp.xcodeprojand Component.xcodeproj. My app has three configurations: Debug, App Storeand In Housebut the component has only two: Debugand Release

我有一个包含MyApp.xcodeprojComponent.xcodeproj的工作区。我的应用程序具有三种配置:DebugApp StoreIn House但该组件只有两种配置:DebugRelease

In the Debugconfiguration, everything works fine, but I can't compile my app in App Storeor In Houseconfiguration because the configuration names do not match. I get a file not found error when trying to #import <Component/Component.h>

Debug配置中,一切正常,但我无法在App StoreIn House配置中编译我的应用程序,因为配置名称不匹配。尝试时出现文件未找到错误#import <Component/Component.h>

I need both App Storeand In Houseconfigurations and I would really like to avoid modifying the component's configurations in order to ease future updates of the component.

我需要App StoreIn House配置,我真的很想避免修改组件的配置,以简化组件的未来更新。

I know I could use CocoaPods to solve this issue but I would like to know if there is a simple solution in Xcode

我知道我可以使用 CocoaPods 来解决这个问题,但我想知道 Xcode 中是否有一个简单的解决方案

回答by 0xced

You can get your project to compile with some tweaks to your app's settings.

您可以通过对应用程序设置进行一些调整来编译您的项目。

I suggest you to modify all settings at the project level so that all your targets can inherit these settings.

我建议您修改项目级别的所有设置,以便您的所有目标都可以继承这些设置。

  1. Add a new DEFAULT_CONFIGURATIONuser-defined setting and define your configuration mapping. This is how it should look like:

    Default Configuration Screenshot

  2. Set FRAMEWORK_SEARCH_PATHSto $(BUILD_DIR)/$(DEFAULT_CONFIGURATION)-$(PLATFORM_NAME)for all configurations, add Any OS X SDKvariants and set the value to $(BUILD_DIR)/$(DEFAULT_CONFIGURATION). Set HEADER_SEARCH_PATHSto $(FRAMEWORK_SEARCH_PATHS)/includeand LIBRARY_SEARCH_PATHSto $(FRAMEWORK_SEARCH_PATHS). This is how it should look like:

    Search Paths Screenshot

    This step is quite tedious, it can be automated with the xcprojtool and by running this script in your project directory. Edit your configurations mapping as needed.

    #!/bin/bash
    
    CONFIGURATIONS=( "App Store:Release" "In House:Release" "Debug:Debug" )
    
    for CONFIGURATION in "${CONFIGURATIONS[@]}"; do
        xcproj --configuration "${CONFIGURATION%%:*}" write-build-setting DEFAULT_CONFIGURATION "${CONFIGURATION#*:}"
    done
    
    xcproj write-build-setting 'FRAMEWORK_SEARCH_PATHS' '$(BUILD_DIR)/$(DEFAULT_CONFIGURATION)-$(PLATFORM_NAME)'
    xcproj write-build-setting 'FRAMEWORK_SEARCH_PATHS[sdk=macosx*]' '$(BUILD_DIR)/$(DEFAULT_CONFIGURATION)'
    
    xcproj write-build-setting 'HEADER_SEARCH_PATHS' '$(FRAMEWORK_SEARCH_PATHS)/include'
    xcproj write-build-setting 'LIBRARY_SEARCH_PATHS' '$(FRAMEWORK_SEARCH_PATHS)'
    
  3. If the component is distributed as a static library, you are done here. If the component comes as a framework you have to update its path reference by editing your project.pbxproj file in a text editor. In the PBXFileReference section (under /* Begin PBXFileReference section */) find Component.frameworkand update its pathlike this:

    name = Component.framework; path = "../$(DEFAULT_CONFIGURATION)/Component.framework"; sourceTree = BUILT_PRODUCTS_DIR; };
    

    Also make sure that the sourceTreeis set to BUILT_PRODUCTS_DIR, i.e. relative to built products. Once you edited the project file, this should look like:

    Location Screenshot

  1. 添加新的DEFAULT_CONFIGURATION用户定义设置并定义您的配置映射。它应该是这样的:

    默认配置截图

  2. 设置FRAMEWORK_SEARCH_PATHS$(BUILD_DIR)/$(DEFAULT_CONFIGURATION)-$(PLATFORM_NAME)在所有配置中,添加任何OS X SDK变种,并设置值$(BUILD_DIR)/$(DEFAULT_CONFIGURATION)。设置HEADER_SEARCH_PATHS$(FRAMEWORK_SEARCH_PATHS)/includeLIBRARY_SEARCH_PATHS$(FRAMEWORK_SEARCH_PATHS)。它应该是这样的:

    搜索路径截图

    这一步非常乏味,它可以通过xcproj工具并在您的项目目录中运行此脚本来自动化。根据需要编辑您的配置映射。

    #!/bin/bash
    
    CONFIGURATIONS=( "App Store:Release" "In House:Release" "Debug:Debug" )
    
    for CONFIGURATION in "${CONFIGURATIONS[@]}"; do
        xcproj --configuration "${CONFIGURATION%%:*}" write-build-setting DEFAULT_CONFIGURATION "${CONFIGURATION#*:}"
    done
    
    xcproj write-build-setting 'FRAMEWORK_SEARCH_PATHS' '$(BUILD_DIR)/$(DEFAULT_CONFIGURATION)-$(PLATFORM_NAME)'
    xcproj write-build-setting 'FRAMEWORK_SEARCH_PATHS[sdk=macosx*]' '$(BUILD_DIR)/$(DEFAULT_CONFIGURATION)'
    
    xcproj write-build-setting 'HEADER_SEARCH_PATHS' '$(FRAMEWORK_SEARCH_PATHS)/include'
    xcproj write-build-setting 'LIBRARY_SEARCH_PATHS' '$(FRAMEWORK_SEARCH_PATHS)'
    
  3. 如果组件是作为静态库分发的,那么您就完成了。如果组件作为框架出现,您必须通过在文本编辑器中编辑 project.pbxproj 文件来更新其路径引用。在 PBXFileReference 部分(在 下/* Begin PBXFileReference section */)找到Component.framework并更新它,path如下所示:

    name = Component.framework; path = "../$(DEFAULT_CONFIGURATION)/Component.framework"; sourceTree = BUILT_PRODUCTS_DIR; };
    

    还要确保sourceTree设置为BUILT_PRODUCTS_DIR,即相对于内置产品。编辑项目文件后,它应该如下所示:

    位置截图

Your project should now build as expected.

您的项目现在应该按预期构建。

回答by gpios

I had this same problem, but I had multiple configurations (Debug, TestFlight, Release, Enterprise) in my app, and the bolded configurations would always fail to build cause it couldn't find the frameworks. I really didn't want to mess with the project settings of my sub projects in order to make updating them easy.

我遇到了同样的问题,但是我的应用程序中有多个配置(Debug、TestFlight、Release、Enterprise),并且粗体配置总是无法构建,因为它找不到框架。我真的不想弄乱我的子项目的项目设置,以便更容易地更新它们。

The answer I found was to just update the framework search path to account for the fact that the frameworks would be dropped in Release (or whatever the default configuration is set to) of the sub projects.

我找到的答案是只更新框架搜索路径以说明框架将在子项目的发布(或任何默认配置设置为)中删除的事实。

Specifically I set it to: $(BUILD_DIR)/Release-$(PLATFORM_NAME)

具体我设置为:$(BUILD_DIR)/Release-$(PLATFORM_NAME)

I set it to be recursive too. This works both for Simulator and Device, building in Xcode and command line.

我也将它设置为递归。这适用于模拟器和设备,在 Xcode 和命令行中构建。