Damian
Damian

Reputation: 5561

Porting C code with #ifdef and #ifndef (C preprocessor)

I'm porting an iPhone game to Mac and I'm writing a file with common defines that has the following:

// first reset all defines

#undef TARGET_IPHONE
#undef TARGET_MAC

// set defines

#if TARGET_OS_MAC
#if TARGET_OS_IPHONE
#define TARGET_IPHONE
#else
#define TARGET_MAC
#endif
#endif

#ifdef TARGET_IPHONE
#error err1
#endif

#ifndef TARGET_IPHONE
#error err2
#endif

But when building for iPhone, both err1 and err2 are thrown by the compiler.

I don't get it, what's the problem there?

EDIT: After about an hour of trying things with no luck, I had to add my own define to xcode build options.

Upvotes: 1

Views: 1877

Answers (2)

hogwell
hogwell

Reputation: 41

This looks like either a compiler bug, or you need special permission from Apple to use #ifndef (that's a joke (I think).)

You might try this equivalent syntax that might work with Apple's compiler:

#if !defined(TARGET_IPHONE)

or

#if !TARGET_IPHONE

or even

#ifdef TARGET_IPHONE

#else

This is worrisome, though - I'd look for an updated or different compiler.

Upvotes: 0

Bernd Elkemann
Bernd Elkemann

Reputation: 23560

Your code is fine. On my compiler (gcc mingw) #error err2 is thrown. And if i insert

#define TARGET_OS_MAC 1
#define TARGET_OS_IPHONE 1

where your // set defines is, #error err1 is thrown.

Upvotes: 1

Related Questions