Cross platform execution branching?

Started by
4 comments, last by alvaro 8 years, 6 months ago

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?

Advertisement

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.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

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.

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.

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
Depending on the nature of your project, there's another solution that you might be able to use. In my chess engine I put all the platform-specific functions in a header file called "platform.h" and I then provide several implementations for those functions, in files named windows_platform.cpp and posix_platform.cpp . Then I make sure the build system for each platform uses the appropriate implementation.

This topic is closed to new replies.

Advertisement