• 9
• 9
• 11
• 13
• 9

# Cross platform execution branching?

This topic is 928 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I was wondering what is the best way of going about executing code that is dependent on the current run time platform?

The one way off the top of my head I can think of is something like this:


int main()
{
//#define IOS
#define WINDOWS
//#define ANDROID

#ifdef IOS
DoIOSCode();
#elseifdef WINDOWS
DoWindowsCode();
#elseifdef ANDROID
DoAndroidCode();

return 1;
}



But I don't think #defines are globally recognized right?

##### Share on other sites

But I don't think #defines are globally recognized right?

No, but instead of defining those in a file, you could feed it "globally" through the compiler.  Most compilers will allow you defined values as part of the compile settings.

Edited by Rattrap

##### Share on other sites

You dont need to define these yourself. Most compilers have premade defines, which you can use to detect the OS, compiler and some language features.

##### Share on other sites

Projects often create a header that (among other things) checks the built in macros in a series of ifdefs and then defines own macros for the platforms like MYPROJECT_WIN32, MYPROJECT_LINUX, ect. to make it consistent because the built in platform or compiler macros often use different naming styles.

And defines are recognized from the point where they are defined till they are undefined. They happen in preprocessor and are just pure text operation, they don't respect scopes, namespaces or anything. Include is a preprocessor feature too and is basically like a paste of entire file in place of the #include <> line, so a define in included file affects everything below it and in the files that included it. You can play with 'cpp' or 'gcc -E' on Linux (or Windows or any system where you can get these tools) to see the result of just preprocessing being ran on a file.

Edited by FRex

##### Share on other sites

You dont need to define these yourself. Most compilers have premade defines, which you can use to detect the OS, compiler and some language features.

And for convenience:

#if defined(_WIN64)
/* Microsoft Windows (64-bit) */
#elif defined(_WIN32)
/* Microsoft Windows (32-bit) */
/* note that _WIN32 is ALWAYS defined in both 32 and 64 bit applications, so if you only have _WIN32 and not _WIN64, you'll capture both with _WIN32 */
#elif __ANDROID__
/* Android goodness */
/* Additional defines available for specific API levels */
#elif __APPLE__
/* Apple stuff */
/* Additional defines available for more specific stuff */
#endif