XCode ignores target-level preprocessor macro - xcode

In XCode 4.5, my preprocessor macro gets ignored when defined at target level, but honored if defined at project level. It's a simple symbol definition (RESTKIT_GENERATE_SEED_DB) to be used in #ifdef. Using "levels" display, XCode shows my symbol in the resolved section. Also, I have double-checked that my current scheme is running the Debug config where macro is defined.

This was a newbie mistake. The setup was that I had two targets, one with the macro defined and one without. The reason the macro was not picked up at target level was that I never ran that target. I thought that you ran it by selecting the target in the target list under TARGETS. I realize now that in order to run a target you must select it the scheme menu next to where you select the device target.

Related

How does Xcode decide what preprocessor macros and other build settings to use for autocomplete?

My Xcode project has different build configurations, and they define different preprocessor macros. Autocomplete doesn't work in #ifdef blocks that are ignored by the current preprocessor flags, so I want to control which build configuration Xcode uses for autocomplete. How does Xcode decide?
After some experimenting, here's what the rules appear to be (on Xcode 13.0 beta 13A5155e):
If a configuration named Debug exists, it is used.
Otherwise, the configuration in the "Use for command line builds" menu is used.
Interestingly this entirely ignores the configuration(s) selected in the current scheme. Autocomplete still works if you delete every scheme.

XCTest with multi targets project

I have a XCode(in Swift) project with multi targets. Each target has its own macro that program can know which target is being run.
For example,
FreeVersion target has a macro "FREE"
PaidVersion target has a macro "PAID"
I would like to make test code with XCTest but it seems like XCTest source cannot be related to a specific target.
What is the best practice for using XCTest in such a situation?
Actually, the test bundle is associated with a specific app target through the TEST_HOST build setting. So I think it should be possible to set up two test targets. Each can have its own prefix header (pch) to set the macro you need, and specify their TEST_HOST.
If you duplicate your current test target, be sure to check the new build settings carefully. I've found that Xcode likes to change some settings in the duplicate but not others.

Add preprocessor macro to a target in xcode 6

Probably this is pretty simple, but I can't find a way to define a preprocessor macro for a target in Xcode 6.
I've done a screenshot to show where it is in Xcode, because it's easier :)
Select project file
Select the target you want
Go to Build Settings
Search for 'preprocessor'
Add your preprocessor macro either for Debug, Release, or both.
In Xcode 9 you have to add a preprocessor macros to Project, not Target. Also don't forget to add "D" as the firs letter. For example, "DDebug", then it works.

Xcode 5 Objective-C Automatic Reference Counting setting

In Xcode 5, I create a project named myproject, in the project's Build Settings I can set Objective-C Automatic Reference Counting to YES in order to use ARC. Then there is a target also named myproject, what if in the target's Build Settings page I set Objective-C Automatic Reference Counting to NO, does this mean eventually I'll use manual memory management? Do I have to set both of the two boolean flag to the same value, either YES for ARC or NO for manual memory management?
Project settings will get applied to all your targets.
Individual target settings will override project settings.
SO if you put YES in project settings and NO in only one particular target..effectively your boolean value is NO for that target. And it'll remain YES for all other targets (where you haven't modified the settings)
Yes, both should have the same value (YES or NO). The first is for the project (so xcode warns you when you write code not compatible with arc) and the second in the target settings is for the compiler, so the compiler can add all the retain/release code for you if you have ARC YES.

New build settings with LLVM Compiler on macros

I just created a new cocoa project on Xcode 4.3.3. The preprocessor macros for the Apple LLVM compiler 3.1 settings have a DEBUG=1 $(inherited) value assigned. I removed it and add it again, and now I'm getting an error when compiling :
clang: error: no such file or directory: 'DEBUG=1'
I search for the value on the project settings and I saw that the value is also defined in "Other warning flags"
My questions are:
What is the difference between just having DEBUG vs DEBUG=1?
What does $(inherited) do?
What is it also doing on the other warning flags?
First, if you're getting a compilation error, then you most likely put the macro back in the incorrect place in the project settings. Please ensure you've put it into the Debug configuration branch of the Preprocessor Macros item under the Apple LLVM compiler x.x - Preprocessing section.
For your other questions:
The first version just defines the macro DEBUG so it's essentially empty. You can test for whether it exists or does not exist, but not much. The second sets it to 1 so that the preprocessor can actually do comparisons like #if DEBUG && SHOULD_DIE_ON_ERROR where you might abort if the application comes across some validation error, but only if SHOULD_DIE_ON_ERROR is set and 1 and you're running in debug mode.
The $(inherited) just brings in other macros you're inheriting from further up the chain. So if your project defines some items and your target defines some more, the target gets the project's settings as well without having to re-define them.
It shouldn't be effecting the warning flags at all. If anything, it determines code paths in header files you include (like the cocoa frameworks) which may use different implementations for things or may add debugging information to data structures, or whatever.

Resources