Topics

Problem with #define in XCode 11 #define

Dave
 

Hi,

In the following code, kLTWGameWindowSizeH is not defined, however I didn’t get compiler error until I actually used kLTWGameRightViewSizeH?

#define kLTWGameRightViewPositionX kLTWGameTopViewPositionX
#define kLTWGameRightViewPositionY kLTWGameTopViewPositionY + kLTWGameWindowSizeH + 20
#define kLTWGameRightViewSizeH kLTWGameWindowSizeH - (kLTWGameTopViewPositionX * 2)
#define kLTWGameRightViewSizeV 64 + 20 + 20

Why is this? It seems to be happening since I updated to XCode 11.

Thanks for any help.

All the Best
Dave

Steve Mills
 

On Jan 2, 2020, at 05:43:19, Dave <dave@...> wrote:

In the following code, kLTWGameWindowSizeH is not defined, however I didn’t get compiler error until I actually used kLTWGameRightViewSizeH?

#define kLTWGameRightViewPositionX kLTWGameTopViewPositionX
#define kLTWGameRightViewPositionY kLTWGameTopViewPositionY + kLTWGameWindowSizeH + 20
#define kLTWGameRightViewSizeH kLTWGameWindowSizeH - (kLTWGameTopViewPositionX * 2)
#define kLTWGameRightViewSizeV 64 + 20 + 20
Impossible to say without knowing what kLTWGameTopViewPositionX and kLTWGameWindowSizeH are. You could also try to diagnose this by doing a preprocess on a file that includes these #defines and see what things are ending up as.

--
Steve Mills
Drummer, Mac geek

Keary Suska
 

The issue is, as I understand, that the preprocessor first build a list of all defines and their substitution values, and then performs substitution. It would not be until after that point during the compilation process that it could be known whether an undefined identifier is used. This is why macros can be nested, and can appear in any order.

Keary Suska
Esoteritech, Inc.

On Jan 2, 2020, at 4:01 AM, Steve Mills via Groups.Io <sjmills=mac.com@groups.io> wrote:

On Jan 2, 2020, at 05:43:19, Dave <dave@...> wrote:

In the following code, kLTWGameWindowSizeH is not defined, however I didn’t get compiler error until I actually used kLTWGameRightViewSizeH?

#define kLTWGameRightViewPositionX kLTWGameTopViewPositionX
#define kLTWGameRightViewPositionY kLTWGameTopViewPositionY + kLTWGameWindowSizeH + 20
#define kLTWGameRightViewSizeH kLTWGameWindowSizeH - (kLTWGameTopViewPositionX * 2)
#define kLTWGameRightViewSizeV 64 + 20 + 20
Impossible to say without knowing what kLTWGameTopViewPositionX and kLTWGameWindowSizeH are. You could also try to diagnose this by doing a preprocess on a file that includes these #defines and see what things are ending up as.

--
Steve Mills
Drummer, Mac geek



Fritz Anderson
 

Off-topic for the question:

#defines that evaluate to expressions can produce inscrutable bugs (or if you’re lucky, compilation errors) unless the expression is wrapped in parentheses.

— F

On 2 Jan 2020, at 5:43 AM, Dave <dave@...> wrote:

Hi,

In the following code, kLTWGameWindowSizeH is not defined, however I didn’t get compiler error until I actually used kLTWGameRightViewSizeH?

#define kLTWGameRightViewPositionX kLTWGameTopViewPositionX
#define kLTWGameRightViewPositionY kLTWGameTopViewPositionY + kLTWGameWindowSizeH + 20
#define kLTWGameRightViewSizeH kLTWGameWindowSizeH - (kLTWGameTopViewPositionX * 2)
#define kLTWGameRightViewSizeV 64 + 20 + 20

Why is this? It seems to be happening since I updated to XCode 11.

Thanks for any help.

All the Best
Dave



Bernie Maier
 

On Thu, 2 Jan 2020, at 10:43 PM, Dave wrote:
Hi,

In the following code, kLTWGameWindowSizeH is not defined, however I
didn’t get compiler error until I actually used kLTWGameRightViewSizeH?

#define kLTWGameRightViewPositionX kLTWGameTopViewPositionX
#define kLTWGameRightViewPositionY kLTWGameTopViewPositionY +
kLTWGameWindowSizeH + 20
#define kLTWGameRightViewSizeH kLTWGameWindowSizeH -
(kLTWGameTopViewPositionX * 2)
#define kLTWGameRightViewSizeV 64 + 20 + 20

Why is this? It seems to be happening since I updated to XCode 11.
I have no idea why this would produce an error in Xcode 11 only, but as other replies have mentioned macro expansion occurs before any C language parsing / lexing. So if kLTWGameRightViewPositionY is never referred to, or referred to after kLTWGameRightViewSizeH then the first error reported would be for when kLTWGameRightViewSizeH.

But it’s been that way in the C language since forever, so the Xcode version (Clang version really) should have nothing to do with it.

Cheers,
Bernie

dhoerl
 

- select the source file (.m, .c) in Xcode
- under the "Product" menu, select "Perform Action", then "Preprocess "<your file>"

Now you can see the file with all #defines expanded by Xcode (clang) into their "actual" values. This should make it clear what the problem is.

 

Using #define for numeric constants is so 1970s! It’s awkward and error-prone. Much better to define constants with either enum or static const:

enum { kFoo = kBar + 17 };

Or

static const int kFoo = kBar + 17;

(really, the only good uses for #define nowadays are constants used in #ifdef, or weird function-like macros you can’t do as an in-line function.)

—Jens