Sign in to follow this  
noodleBowl

Cross platform execution branching?

Recommended Posts

noodleBowl    713

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 this post


Link to post
Share on other sites
Rattrap    3385

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 this post


Link to post
Share on other sites
FRex    1778

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 this post


Link to post
Share on other sites
frob    44962

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

Share this post


Link to post
Share on other sites
alvaro    21263
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this