-1

I have code that says (literally):

#define BUILD_PLATFORM ios
#if BUILD_PLATFORM==macos
#import <AppKit/AppKit.h>
#elif BUILD_PLATFORM==ios
#import <UIKit/UIKit.h>
#endif

However when I attempt to build the project, it still tries to import AppKit/AppKit.h, giving an error that the header cannot be found.

What am I doing wrong?

pfx
  • 20,323
  • 43
  • 37
  • 57
sssilver
  • 2,549
  • 3
  • 24
  • 34

2 Answers2

1

The problem with C-preprocessor is that it can compare only numbers. Both ios and macos are literals that cannot really be compared. You would have to define them first, e.g.

#define ios 1
#define macos 2

However, if you do that, please, use better names that won't conflict with your code.

If you want to know how Apple does it, see file "Availability.h" which is accessible for both iOS and Mac OS and probably it's the thing you should be using, e.g:

#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
#import <AppKit/AppKit.h>
#else
#import <UIKit/UIKit.h>
#endif
Sulthan
  • 128,090
  • 22
  • 218
  • 270
0

When using CMake, the following works:

CMakeLists.txt:

add_definitions(-DPLATFORM=${PLATFORM})  # Set to the target platform

Objective C:

#if defined(PLATFORM_macos)
#import <AppKit/AppKit.h>
#elif defined(PLATFORM_ios)
#import <UIKit/UIKit.h>
#endif
sssilver
  • 2,549
  • 3
  • 24
  • 34