Search code examples
cross-platformc++builderconditional-compilation

How to conditionally compile code for different platforms in C++Builder?


What are the platform conditional defines for Android, iOS, Win32, Win64 in C++Builder? I've found only examples for Delphi.


Solution

  • The so called manifest constants are documented on this help page. The platform ones I've listed here:

    ┌─────────────┬───────┬──────────────────────────────┐
    │ Macro       │ Value │ Description                  │
    ├─────────────┼───────┼──────────────────────────────┤
    │ _Windows    │ 1     │ Windows platform             │
    ├─────────────┼───────┼──────────────────────────────┤
    │ __WIN32__   │ 1     │ 32-bit Windows platform      │
    ├─────────────┼───────┼──────────────────────────────┤
    │ _WIN64      │ 1     │ 64-bit Windows platform      │
    ├─────────────┼───────┼──────────────────────────────┤
    │ __arm__     │       │ 32-bit ARM compiler          │
    ├─────────────┼───────┼──────────────────────────────┤
    │ __arm64__   │       │ 64-bit ARM64 compiler        │
    ├─────────────┼───────┼──────────────────────────────┤
    │ __APPLE__   │       │ Apple platform               │
    ├─────────────┼───────┼──────────────────────────────┤
    │ __MACH__    │       │ MAC OSX platform             │
    ├─────────────┼───────┼──────────────────────────────┤
    │ __ANDROID__ │       │ Android platform             │
    └─────────────┴───────┴──────────────────────────────┘
    

    These macros are compiler intrinsic, so they have no header file to include. An example:

    #if _Windows
      // Windows platform
    #elif __APPLE__
      // Apple platform
    #elif __ANDROID__
      // Android platform
    #else
      #error Not a supported platform
    #endif