Skip to content Skip to sidebar Skip to footer

C++ Identify Running Platform (android, Ios, Etc.)

As part of a multi-platform project (Android & iOS so far), we share a common C++ codebase for mathematical and algorithmic requirements. As the Android developper, I am facing

Solution 1:

You are mixing up compile time and runtime.

1) Includes are for compile time, so you can distinguish this e. g. via the build configuration and then select the appropriate include. This also is the normal way. Additionally you can swap some files and also use #ifdef directives for functionality that is only available on some platforms.

2) As for run time, it probably is a little more difficult to find out from generic C++, I would recommend to simply inject that information from the calling code: there has to be some place that knows where it is running, that code can then set a property in your code telling it where it is running.

You probably want to go for 1) though to already select your libraries appropriately.

Solution 2:

If you want to check your target only once, you may want to include STL headers through a single header dedicated for that.

For example, create a file stl_includes.h with following contents:

#ifndef STL_INCLUDES_H#define STL_INCLUDES_H#ifndef TARGET_PLATFORM// Prevent builds without defined target#error Target platform not defined!#elif TARGET_PLATFORM == TARGET_IOS// Your iOS headers here#include<...>#elif TARGET_PLATFORM == TARGET_ANDROID// Your Android headers here#include<...>#else#error Invalid build target!#endif#endif

This makes sure that every build must define a valid target platform and use the correct headers. Unknown or undefined targets will not compile. Define the valid build targets in some global header, preferably in some other header than the dedicated STL header so you can use them in other platform specific features as well.

Solution 3:

According to this page, here's what you need:

#ifdef __ANDROID__// Android Headers#elif __APPLE__// iOS Headers#endif

Post a Comment for "C++ Identify Running Platform (android, Ios, Etc.)"