编译前的 Xcode 4 自定义构建步骤
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8458009/
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
Xcode 4 custom build step before compile
提问by user408952
I'm trying to integrate a tool into Xcode 4 that generates a C header from a descriptor file. In Xcode 3 it worked to add a custom build step for files with a specific extension. Those files then get compiled before the .m/.mm/.cpp files that included them. When I try to do this with Xcode 4 it seems like it runs my custom step after compiling the other source. This doesn't work of course. Is there any way to tell Xcode to run the step before?
我正在尝试将一个工具集成到 Xcode 4 中,该工具从描述符文件生成一个 C 头文件。在 Xcode 3 中,它可以为具有特定扩展名的文件添加自定义构建步骤。然后这些文件在包含它们的 .m/.mm/.cpp 文件之前被编译。当我尝试使用 Xcode 4 执行此操作时,它似乎在编译其他源代码后运行我的自定义步骤。这当然行不通。有什么方法可以告诉 Xcode 之前运行该步骤吗?
Here's a simple repro setup:
这是一个简单的重现设置:
My main.m contains a:
我的 main.m 包含一个:
#include <mytest.h>
and I've added ${DERIVED_FILE_DIR}
to the header search path. When I compile this project I get the following:
我已经添加${DERIVED_FILE_DIR}
到标题搜索路径。当我编译这个项目时,我得到以下信息:
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?
所以这条规则确实有效,但执行得太晚了。有什么办法可以改变这种行为吗?
回答by Gordon Dove
I already have my Revival badge, but here goes :)
我已经有了我的复兴徽章,但这里有 :)
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. Simply go into the build rule and drag the .abc to the top of the list.
构建规则对源文件 (.abc) 做出响应,并且正在按照“编译源”构建步骤中列出的顺序处理源文件。只需进入构建规则并将 .abc 拖到列表顶部。
One small step towards my archeologist badge, I hope.
我希望是朝着我的考古学家徽章迈出的一小步。
回答by Jules Looker
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.
而不是添加构建规则:在构建阶段选项卡中,使用添加构建阶段按钮,选择添加运行脚本。将添加一个运行脚本阶段,您可以在其中实现您的脚本要求。可以将新的运行脚本阶段拖到 Compile Sources 阶段之前,这是您希望执行功能的时间。
回答by ovidiu
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. I usually create a new group called "Derived Files" and place such files under it.
在 Finder 中打开 DerivedData 目录,选择 mytest.h 文件并将其拖到项目中的 XCode 中。我通常会创建一个名为“派生文件”的新组并将此类文件放在其下。
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. 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.
在项目中引用该文件后,需要将其添加到主目标Compile Sources 规则中。选择项目的主目标,如果还没有展开,则展开Compile Sources,将新添加的派生文件拖到文件列表中进行编译。
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.
更新:实际上看起来上面的内容并不总是有效。最可靠的方法是使用构建阶段而不是构建规则。