Can xcode pass compile flags to depended target from the depending one?

1.4k views Asked by At

Here is the case I'm faced with when building my mac app.

The project has three targets: target A is comprised of UI src code and produces the final app; target B produces command-line test program that executes some unit tests(I'm not using the auto generated XXXTests target); and target C is comprised of backend src codes, and produces a static library for target A and target B to link to.

The problem is that target B need target C being built with flag "-DZS_TEST", but target A need target C being built without that flag. So, how to set up xcode project/target settings so that I don't have to change any setting when switching from building target A and target B? Because changing build setting causes git reporting unstaged files, which feels unnecessary because I just want to build a different target.

I'm wondering if xcode can forward flag from depending target to depended target then I can let B forward "-DZS_TEST=1" to C and let target A forward "-DZS_TEST=0" to C.

Or is there better way of dividing the project into different targets?

I googled and found many xcode build setting related articles but they don't share the specific need as this project. So thanks in advance.

2

There are 2 answers

0
bestOfSong On

I end up using two kernel targets, i.e. target c1, and c2 which use the same source code but different compile flags and c1 build with ZS_TEST defined but c2 not. Then target A and B depend on whichever they need.

0
Gamma Draconis On

What I do is set up a separate configuration, call it "Test", say, as a copy of the standard Debug configuration. Put your additional flags into the Test configuration in Target C, and B if necessary.

Edit the scheme for Target B and have Run build the Test configuration. The configuration gets passed on to the dependencies, so their Test configurations get built and everything should work correctly.