简体   繁体   English

编译前Xcode 4自定义构建步骤

[英]Xcode 4 custom build step before compile

I'm trying to integrate a tool into Xcode 4 that generates a C header from a descriptor file. 我正在尝试将一个工具集成到Xcode 4中,该工具从描述符文件生成C头。 In Xcode 3 it worked to add a custom build step for files with a specific extension. 在Xcode 3中,它为具有特定扩展名的文件添加了自定义构建步骤。 Those files then get compiled before the .m/.mm/.cpp files that included them. 然后在包含它们的.m / .mm / .cpp文件之前编译这些文件。 When I try to do this with Xcode 4 it seems like it runs my custom step after compiling the other source. 当我尝试使用Xcode 4执行此操作时,它似乎在编译其他源之后运行我的自定义步骤。 This doesn't work of course. 这当然不起作用。 Is there any way to tell Xcode to run the step before? 有没有办法告诉Xcode以前的步骤?

Here's a simple repro setup: 这是一个简单的repro设置:

在此输入图像描述

My main.m contains a: 我的main.m包含一个:

#include <mytest.h>

and I've added ${DERIVED_FILE_DIR} to the header search path. 我已将${DERIVED_FILE_DIR}添加到标题搜索路径中。 When I compile this project I get the following: 当我编译这个项目时,我得到以下内容:

在此输入图像描述

If I remove the include and build it again I get this: 如果我删除包含并再次构建它我得到这个:

在此输入图像描述

So the rule is indeed working, but it's executed way too late. 因此规则确实有效,但执行方式为时已晚。 Is there any way to change this behavior? 有没有办法改变这种行为?

I already have my Revival badge, but here goes :) 我已经拥有了Revival徽章,但是这里有:)

I just ran into this problem, and the answer is simple ( now I've spent a half a day staring at it). 我刚遇到这个问题,答案很简单(现在我花了半天时间盯着它)。

The build rule is responding to the source file (.abc), and the source file is being processed in the order it is listed in the 'Compile Sources' build step. 构建规则响应源文件(.abc),源文件按照“编译源”构建步骤中列出的顺序进行处理。 Simply go into the build rule and drag the .abc to the top of the list. 只需进入构建规则并将.abc拖到列表顶部即可。

One small step towards my archeologist badge, I hope. 我希望,向我的考古学家徽章迈出一小步。

Rather than adding a Build Rule: In the Build Phases tab, using the Add Build Phase button, choose Add Run Script. 而不是添加构建规则:在“构建阶段”选项卡中,使用“添加构建阶段”按钮,选择“添加运行脚本”。 A Run Script phase will be added in which you can implement your script requirements. 将添加一个运行脚本阶段,您可以在其中实现脚本要求。 The new run script phase can be dragged to before the Compile Sources phase which is when you want your functionality executed. 可以将新的运行脚本阶段拖到编译源阶段之前,这是您希望执行功能的时间。

You need to add an explicit dependency between the generated file and your target. 您需要在生成的文件和目标之间添加显式依赖项。

Open the DerivedData directory in Finder, select the mytest.h file and drag it in XCode inside your project. 在Finder中打开DerivedData目录,选择mytest.h文件并将其拖放到项目内的XCode中。 I usually create a new group called "Derived Files" and place such files under it. 我通常会创建一个名为“派生文件”的新组,并将此类文件放在其下。

To easily find the DerivedData directory, I change the project settings so it makes the directory project relative (see File -> Project Settings, the Build tab). 为了轻松找到DerivedData目录,我更改了项目设置,使其相对于目录项目(参见文件 - >项目设置,构建选项卡)。

After the file is referenced in the project, you need to add it to the main target Compile Sources rule. 在项目中引用文件后,您需要将其添加到主目标Compile Sources规则。 Select the project's main target, expand the Compile Sources if it's not already expanded, and drag the newly added derived file in the list of files to compile. 选择项目的主目标,展开编译源(如果尚未展开),并将新添加的派生文件拖放到文件列表中进行编译。

Lastly, since you really want to compile this file, you may want to rename it to .m or .c, instead of .h. 最后,由于您确实要编译此文件,因此您可能希望将其重命名为.m或.c,而不是.h。

Update: Actually looks like the above is not working all the time. 更新:实际上看起来上面并不是一直都在工作。 The most reliable way is to use a build phase instead of a build rule. 最可靠的方法是使用构建阶段而不是构建规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM