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
How to compile a project with app and library in the same workspace with different configuration names?
提问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.xcodeproj和Component.xcodeproj的工作区。我的应用程序具有三种配置:Debug、App Store和In House但该组件只有两种配置:Debug和Release
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 Store或In 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 Store和In 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.
我建议您修改项目级别的所有设置,以便您的所有目标都可以继承这些设置。
Add a new
DEFAULT_CONFIGURATION
user-defined setting and define your configuration mapping. This is how it should look like:Set
FRAMEWORK_SEARCH_PATHS
to$(BUILD_DIR)/$(DEFAULT_CONFIGURATION)-$(PLATFORM_NAME)
for all configurations, add Any OS X SDKvariants and set the value to$(BUILD_DIR)/$(DEFAULT_CONFIGURATION)
. SetHEADER_SEARCH_PATHS
to$(FRAMEWORK_SEARCH_PATHS)/include
andLIBRARY_SEARCH_PATHS
to$(FRAMEWORK_SEARCH_PATHS)
. This is how it should look like: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)'
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 */
) findComponent.framework
and update itspath
like this:name = Component.framework; path = "../$(DEFAULT_CONFIGURATION)/Component.framework"; sourceTree = BUILT_PRODUCTS_DIR; };
Also make sure that the
sourceTree
is set toBUILT_PRODUCTS_DIR
, i.e. relative to built products. Once you edited the project file, this should look like:
添加新的
DEFAULT_CONFIGURATION
用户定义设置并定义您的配置映射。它应该是这样的:设置
FRAMEWORK_SEARCH_PATHS
要$(BUILD_DIR)/$(DEFAULT_CONFIGURATION)-$(PLATFORM_NAME)
在所有配置中,添加任何OS X SDK变种,并设置值$(BUILD_DIR)/$(DEFAULT_CONFIGURATION)
。设置HEADER_SEARCH_PATHS
要$(FRAMEWORK_SEARCH_PATHS)/include
和LIBRARY_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)'
如果组件是作为静态库分发的,那么您就完成了。如果组件作为框架出现,您必须通过在文本编辑器中编辑 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 和命令行中构建。