Xcode:基于架构的条件构建设置(Device (ARM) vs Simulator (i386))

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1211854/
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-14 18:42:33  来源:igfitidea点击:

Xcode: Conditional Build Settings based on architecture (Device (ARM) vs Simulator (i386))

iphonexcodecompiler-constructionbuildlinker

提问by noamtm

I'm building an iPhone app that has to run on both the simulator and the device. However I'm using an externally compiled library that has one version for the simulator and one for the device (different CPU).

我正在构建一个必须在模拟器和设备上运行的 iPhone 应用程序。但是,我使用的是一个外部编译的库,该库有一个版本用于模拟器,另一个用于设备(​​不同的 CPU)。

How can I do it? I'm coming from Visual C++ so I'm new to Xcode, and I can't find the way to do it.

我该怎么做?我来自 Visual C++,所以我是 Xcode 的新手,我找不到方法。

EDIT, March 2016: this question was asked on July 2009, almost 6 years ago. Much has changed in Xcode since, but I guess some stuff still holds. The now-accepted answer, for example, wasn't an option in Xcode v3.

编辑,2016 年 3 月:这个问题是在大约 6 年前的 2009 年 7 月提出的。从那以后,Xcode 发生了很大变化,但我想有些东西仍然存在。例如,现在接受的答案不是 Xcode v3 中的一个选项。

回答by clozach

I had this problem when integrating Adobe Omniture's "AppMeasurement" library, which currently comes compiled for 3 architectures: libAppMeasurement-iOSSimulator.a, libAppMeasurement-iOSDevice.a, and libAppMeasurement-iOSDevice-armv7.a.

我在集成 Adob​​e Omniture 的“AppMeasurement”库时遇到了这个问题,该库目前为 3 种架构编译:libAppMeasurement-iOSSimulator.a、libAppMeasurement-iOSDevice.a 和 libAppMeasurement-iOSDevice-armv7.a。

While the other answers here are substantially correct, I ended up having to go elsewhere to truly understand and then fix the problem.

虽然这里的其他答案基本正确,但我最终不得不去其他地方真正理解并解决问题。

Step 1. Understanding the issues

步骤 1. 了解问题

This blog postdoes a great job of explaining the overall issue. It gives start-to-finish instructions for solving the problem in Xcode 3. See below for Xcode 4.

这篇博文很好地解释了整个问题。它给出了解决Xcode 3 中问题的从头到尾的说明。请参阅下面的 Xcode 4。

Note:You might try skipping the bit where he says to add the static libraries and then delete them. The next time I do this, I'll probably just add the header file(s), then skip straight to editing Other Linker Flags.

注意:您可以尝试跳过他所说的添加静态库然后删除它们的部分。下次我这样做时,我可能只添加头文件,然后直接跳到编辑其他链接器标志。

Step 2. Conditional Build Settings in Xcode 4

步骤 2. Xcode 4 中的条件构建设置

This StackOverflow pageexplains the new way to set conditional build settings in Xcode 4. Tip:The text fields on the Build Settings tab are drag-&-drop enabled; once you have your conditional build setting ready for editing under Other Linker Flags, you can just drag the static library file right onto the text field and Xcode will automatically enter a (hopefully relative) path.

这个StackOverflow 页面解释了在 Xcode 4 中设置条件构建设置的新方法。提示:Build Settings 选项卡上的文本字段已启用拖放;一旦您准备好在其他链接器标志下编辑条件构建设置,您只需将静态库文件拖到文本字段上,Xcode 将自动输入(希望是相对的)路径。

Here's a screenshot of my Other Linker Flags once I got the "missing required architecture i386" warning to go away with no build errors:

这是我收到“缺少所需的架构 i386”警告后我的其他链接器标志的屏幕截图,该警告消失且没有构建错误:

enter image description here__

在此处输入图片说明__

回答by Louis Gerbarg

You have 3 options:

您有 3 个选择:

  1. If you control click on the name of build setting inside the Inspect Window (where you can change compiler settings, etc) it will bring an option to conditionalize that setting. Just go to the linker flags you want to change, and conditionalize them by SDK, then enter the specific library for each SDK.

  2. Alternatively you can take the library and install it at the same path in each SDKs root ("/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/usr/lib/" and "/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/usr/lib"). Since SDK relative library search paths are used the appropriate version will be pulled in for either build.

  3. You can lipo together two libraries into one fat library. That is probably a bad idea, but if you want to do it checkout the manpage.

  1. 如果您控制单击检查窗口内的构建设置名称(您可以在其中更改编译器设置等),它将带来一个条件化该设置的选项。只需转到要更改的链接器标志,并通过 SDK 对其进行条件化,然后输入每个 SDK 的特定库。

  2. 或者,您可以获取库并将其安装在每个 SDK 根目录中的相同路径(“/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/usr/lib/”和“/Developer/Platforms/ iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/usr/lib”)。由于使用了 SDK 相关库搜索路径,因此将为任一构建引入适当的版本。

  3. 您可以将两个库 lipo 合并为一个胖库。这可能是一个坏主意,但如果您想这样做,请查看联机帮助页

回答by user746403

For someone comes across the warning like "[lib_for_sim_or_device] not not built for the architecture ...", the warning arises when dragging the 3rd party library folder into the project.

如果有人遇到“[lib_for_sim_or_device] not built for the architecture...”之类的警告,则将第 3 方库文件夹拖到项目中时会出现警告。

Behind the scene the XCode automatically add the library files into 'target setting -> Build Phrases -> Link Binary with Libraries' sections, which causes linking with both libraries.

在幕后,XCode 自动将库文件添加到“目标设置 -> 构建短语 -> 将二进制与库链接”部分,这会导致与两个库的链接。

To fix that, remove those entries from 'Link Binary with Libraries', and then follow the guide above on conditional building setting for sim/device'

要解决此问题,请从“Link Binary with Libraries”中删除这些条目,然后按照上面有关 sim/device 的条件构建设置的指南进行操作

Hope it helps!

希望能帮助到你!

回答by cdespinosa

The recommended way to do this is to not add the library to your project and target, but instead to set the Other Linker Flags to include separate, direct references to the link library per configuration.

推荐的方法是不要将库添加到您的项目和目标中,而是设置其他链接器标志以包含对每个配置的链接库的单独、直接引用。

For Debug:

对于调试:

  OTHER_LINKER_FLAGS = -l/Path/To/My/Debug/Library.dylib

For Release

发布

  OTHER_LINKER_FLAGS = -l/Path/To/My/Release/Library.dylib

You can of course use references to other build settings to make these paths relative to something durable, or use a Source Tree to an external source tree.

您当然可以使用对其他构建设置的引用来使这些路径相对于持久的东西,或者使用源树到外部源树。

回答by zaph

For option 1 (see Louis Gerbarg answer) in Xcode 3.2.1 select the "Other Linker Flags" and then select "Add Build Setting Condition" from the dropdown menu at the lower left of the build setting window. See cdespinosa answer for "Other Linker Flags" syntax)

对于 Xcode 3.2.1 中的选项 1(参见 Louis Gerbarg 答案),选择“其他链接器标志”,然后从构建设置窗口左下方的下拉菜单中选择“添加构建设置条件”。有关“其他链接器标志”语法,请参阅 cdespinosa 答案)

Or you can also "Add Build Setting Condition" to "Library Search Paths" if you have the device/simulator libraries in separate directories.

或者,如果设备/模拟器库位于单独的目录中,您也可以将“添加构建设置条件”到“库搜索路径”。

回答by Peter

The problem with other linker flags and adding libraries there is controlling the link order of libraries which can be important. It seems that the linker flag version means these libraries will come first so if you are managing other libraries in xcode that have to come first, you then have to abandon that and move everything to the other linker flags...!-P

其他链接器标志和在那里添加库的问题是控制库的链接顺序,这可能很重要。似乎链接器标志版本意味着这些库将首先出现,因此如果您在 xcode 中管理必须首先出现的其他库,那么您必须放弃它并将所有内容移至其他链接器标志......!-P

回答by opyh

In my XCode 3.2.3, the correct naming seems to be OTHER_LDFLAGS, not OTHER_LINKER_FLAGS.

在我的 XCode 3.2.3 中,正确的命名似乎是 OTHER_LDFLAGS,而不是 OTHER_LINKER_FLAGS。