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?
Here's a simple repro setup:
My main.m contains a:
#include <mytest.h>
and I've added ${DERIVED_FILE_DIR}
to the header search path. 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?
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.
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).
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.
Lastly, since you really want to compile this file, you may want to rename it to .m or .c, instead of .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.