6
votes

I cannot get this macro to compile the correct code.

Here is the code: enter image description here

Here are the build settings (I'm doing a Release build): enter image description here Note that the GCC documentation says -Dname will define as 1, so I omitted the "=1" for Release: enter image description here

Here is the compile log showing that the definition (in yellow) was passed along on the command line: enter image description here

Here is my output log showing that the code is compiled as if ADD_CAMERA_FEATURE is not defined: enter image description here

If I put #define ADD_CAMERA_FEATURE 1 in the source, the #ifdef works as expected, but I also get a warning that I am redefining an existing macro. So XCode knows that the macro should exist from the build scheme settings, but still does not include the #ifdef branch of code.

Other details:

  1. XCode 5.1
  2. OS X 10.9.2
  3. iOS 7.1

My objective here is to have a target for building iOS 7 version of the app and a target for building a pre-iOS 7 version of the app, both from the same source. I have to support older devices that cannot be upgraded to iOS 7 for a while longer. Perhaps there is a better way to go about this. Any suggestions about how to accomplish this would be appreciated.

1
Did you clean your project? - bbarnhart
Just tried a clean and build. Same result. - Chuck Krutsinger
The compile log shows that you are doing a release build. Please try the same thing doing a debug build. Does it work properly there? (I ask because I can't reproduce the problem here.) - matt
Scheme settings? Are you sure about that? Maybe you mean configuration, target or project? - Steven Fisher
@ChuckKrutsinger Could you run that file through the preprocessor? I can see from the syntax coloring in the first screen shot that it thinks it's going to run the first NSLog. The preprocessor should confirm that for us. - matt

1 Answers

5
votes

Found the problem. It has to do with targets and dependencies. I created a new target to compile the source file and added the preprocessor definition to that target. That compiled object then got linked into a static library being used as a framework. So I also created a new target for the static library. Unfortunately, I overlooked that the static library target was still depending on the original compile step that did not include the preprocessor definition. As a result, even though I was building the object file correctly, the new object file was not the one being linked into the project at runtime. So under Build Phases for the static library, I needed to change the target dependency to the correct object file and everything began to work. Thanks @matt and @StevenFisher for pointing me toward the right settings.

enter image description here