Sign in to follow this  

Simple C++ on multiple platform question

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was asked this a few days ago, and while I know what *I* would do to solve it, I've only done C for one semester and am just getting started in my first semester of C++ and don't know that my idea would be the "appropriate" way to go: if I want to write a program that runs on multiple platforms, how do I determine what I'm running on, so I know what commands to issue (eg - clear the screen is CLS on DOS but clear on linux)? The code example given used lots of tests - if (running on DOS) cls else if (running on linux) clear. Ugly because adding a new platform would mean you'd have to search ALL your code for platform-specific commands and add the new test and command in to all the appropriate places. My response was not to put that into the main code at all, but to have header and implementation files (which I now realise I should have said classes instead) - one for each platform, on the particular box, but all with the same name and all with the same functions. So my machine_specific header on DOS would have a function clearScreen() with a single command system("cls"); and my machine_specific header on linux would have the same function clearScreen() with the single command system("clear"); and in my code I'd #include <machine_specific> and call clearScreen() in the appropriate place. When I compile the program on each platform, it'll automatically grab the commands appropriate for that platform. Avoids lots of if tests, and if you want to add a new platform you (hopefully) just need to create a new header FOR that platform and compile, and hope there are no other statements that you have to start fidgeting with. I'm pretty comfortable with that, and the guy who posed the question tried this method on 4 different platforms and said it worked beautifully. Is this an acceptable way to program for multiple platforms, or is there a more appropriate / standard way? Thanks. Shaz

Share this post


Link to post
Share on other sites
That's fine. (Classes really won't help you here, either.) You can't meaningfully detect the platform at runtime; as I recently commented in General Programming, that's a bit like "detecting" whether you breathe air or water - that "decision" was made when you were born, and you know you breathe air by the fact that you are, well, breathing :) So this is what the #ifdefs are for.

You do well to encapsulate the platform-specific stuff in that way. Then you can use a single directive when you include the header:


#ifdef LINUX
#include "my_linux_widgets.h" // uses clearScreen to wrap clear(), etc.
#else
#include "my_windows_widgets.h" // uses clearScreen to wrap cls(), etc.
#endif


And if there are several source files that need to use the widgets, you can put *that* (i.e. just those 5 lines) into another header file, and just include your new "platform-agnostic" header:


#include "my_widgets.h" // that file has just the above 5 lines in it


You can then move the include guards into my_widgets.h. (You don't need include guards in all three files, as long as you always use my_widgets.h instead of using the platform-specific ones directly; but you will need them in either my_widgets, or in both platform-specific ones.)

However, you should also check if someone has already done the wrapping for you for the widgets you need. For example, platform-specific file and folder manipulation is probably best handled by using Boost::filesystem.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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