Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 472
Brian P. Avatar asked Apr 23 '15 16:04

Brian P.


People also ask

What is conditional compilation?

If code is useful only during debugging, conditional compilation can be used to automatically remove the code for release builds. Prior to the advent of the Microsoft .NET platform, the traditional method of compiling conditionally using C++ or Visual Basic was to explicitly mark the regions of conditional code.

How to code a C or C++ program for multiple platforms?

How to code a C or C++ program which can easily be ported to several operating systems. The easiest way is to use some cross-platform framework library like Qt or POCO (or perhaps libsdl or GTK) which has been ported to several platforms and provides a common set of abstractions.

How do I conditionally compile a block of code?

You can conditionally compile blocks of code depending on the framework like this: Execute the .NET 5 executable in bin/Debug/net5.0/example.exe Execute the .NET Core 3.1 executable in bin/Debug/netcoreapp3.1/example.exe

Why do we need a compiler for C?

C is a mid-level language and it needs a compiler to convert it into an executable code so that the program can be run on our machine. How do we compile and run a C program? Below are the steps we use on an Ubuntu machine with gcc compiler.


1 Answers

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
like image 84
TLama Avatar answered Nov 29 '22 01:11

TLama