CheckOS (Utillity for platform specific coding)

Discussion about everything. New games, 3d math, development tips...
Post Reply
Systemerror
Posts: 98
Joined: Fri Oct 03, 2008 1:25 pm
Location: UK
Contact:

CheckOS (Utillity for platform specific coding)

Post by Systemerror »

CheckOS is a simple header file to include into your projects so your can code platform specific functionality easily, it works with "compiler pre-processor macros" so it's easy to add your own macro if the desired doesn't exists for your compiler, current OS macro's are Windows 32 bit, Windows 64 bit, Linux and Unix.

It's coded in such a way so you can use an Integer Identifier for a platform to work with switch statements etc and is very simple to use, It comes with an example program/main file to show how to call the CheckOS function, work with switches, conditional if statements and the macros it's self, also a binary version of the example program compiled on win32 and a screen shot (just in-case you're on another platform).






Download http://virtualworld.synthasite.com/reso ... heckOS.zip

Header:

Code: Select all

 
#include<iostream>


using namespace std;

#if defined _WIN32
#define PLATFORM "Windows32"
#elif defined _WIN64
#define PLATFORM "Windows64"
#elif defined _linux
#define PLATFORM "Linux"
#elif defined _unix
#define PLATFORM "Unix"
#endif


void CheckOS();

int Platform_Int;

void CheckOS()
{

	       
			
		   
	    cout<<"http://virtualworld.synthasite.com\n\n";
	    cout<<"Checking platform...\n\n"; 
        if(PLATFORM == "Windows32")
		{
        cout<<"\n|Windows 32 bit OS detected|\n"; 
		Platform_Int = 1;
		}
        else if(PLATFORM == "Windows64")
		{
        cout<<"\n|Windows 64 bit OS detected|\n";
		Platform_Int = 2;
		}
        else if(PLATFORM == "Linux")
		{
	    cout<<"|Platform Linux detected|\n";
		Platform_Int = 3;
		}
        else if(PLATFORM == "Unix")
		{
		cout<<"|Platform Unix detected|\n";
		Platform_Int = 4;
		}

		
		else
		{
			cout<<"Could not detect OS version with macros from CheckOS.h please check"
			"\nthe compiler pre-processor macros and append them to CheckOS.h\n\n";
			Platform_Int = 0;
		}
		
		
}







Example usage:

Code: Select all

 
/*
__________________________________________________________________
A demonstration on how to use CheckOS.h on it's function and usage
Developed by http://virtualworld.synthasite.com all rights reserved
___________________________________________________________________
*/



//include OS check header
#include "CheckOS.h" 

int main()
{
	cout<<"Developed by http://virtualworld.synthasite.com\n";
	cout<<"This is an example program to show the usage with CheckOS.h\n";
	cout<<"Ok now Checking Operating System version:\n\n";
 
	//make one simple call with Check OS (The version will be printed in console
	CheckOS();

	/*
	PLATFORM macros will take and integer identifier so
	they can be used with switch statements etc

	 Hierachy:


	 OS     Integer Identifier
	      
	 WIN32 Platform_Int = 1
	 WIN64 Platform_Int = 2
	 Linux Platform_Int = 3
	 Unix  Platform_Int = 4

	 OS Macro Not found Platform_Int = 0
	*/


	cout<<"\nChecking on switch statement:\n";
	switch(Platform_Int)
	{
	case 0: cout<<"OS pre-processor compiler macro not found [fail]\n"; break;
	case 1: cout<<"Windows 32 tasks only\n";                            break;
	case 2: cout<<"Windows 64 tasks only\n";                            break;
	case 3: cout<<"Linux tasks only\n";                                 break;
	case 4: cout<<"Unix tasks only\n";                                  break;
	}



	//you could also easily check it with an if statement if you like, example

	cout<<"\nUsing conditional if statements:\n";

	if(Platform_Int == 1)
	cout<<"Ok the Platform_Int is 1, thus we are in windows 32\n";
	else if(Platform_Int == 2)
	cout<<"Ok the Platform_Int is 2, thus we are in windows 64\n";
	if(Platform_Int == 3)
	cout<<"Ok the Platform_Int is 3, thus we are in Linux \n";
	if(Platform_Int == 4)
	cout<<"Ok the Platform_Int is 1, thus we are in Unix\n";
	if(Platform_Int == 0)
	cout<<"Ok the Platform_Int is 0, thus can't can't compiler pre-processor macro\n";


	//And you can randomly print the OS version if you like, example
	cout<<"\nRandom platform macro print out:\n";
	cout<<"My platform is "<<PLATFORM<<" thankyou for asking.";


  cout<<"\n\n-----------Example program ended-----------\n\n";

  system("pause");

return 0;
}

Screeny of example on win32;

Image
-System error
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

I'm sorry, but I'm going to blast you for this one...

First off, your code is not correct, which will make it non-portable. It may have worked for you with whatever compiler you're using, but it isn't guaranteed to work. The problem is this....

Code: Select all

#if defined _WIN32 
#  define PLATFORM "Windows32" 
   // snip...
#endif 

void CheckOS() 
{ 
   if (PLATFORM == "Windows32") 
   { 
That conditional is essentially the same as writing

Code: Select all

   if ("Windows32" == "Windows32") 
This will only succeed if the compiler/linker applied an optimization that eliminates duplicate strings. In most cases this is okay, but it can be a problem. You should probably be using strcmp() instead. Even better, you'd put the conditional code inside CheckOS() and there would be no unnecessary overhead for the string comparisons.

Second, most code that needs to be called when running on a specific platform is only available on that platform. i.e., the function that you need to call when running on windows is only available on windows. That means the user would need to do some conditional compilation themselves to guard the use of that function, and that makes this code slightly less useful.

Third, it is also not really a great idea to generate console output in a function that is intended to be used to query system information. Not all programs have a console, and not everyone would want that information cluttering up their output.

Travis
Dark_Kilauea
Posts: 368
Joined: Tue Aug 21, 2007 1:43 am
Location: The Middle of Nowhere

Post by Dark_Kilauea »

This should hopefully be helpful:

Code: Select all

#ifndef PLATFORM_WIN
#	if defined( WIN32 ) || defined( _WINDOWS ) || defined(_WIN32)
#		define PLATFORM_WIN
#	endif
#endif

#ifndef PLATFORM_MAC
#   if defined( __APPLE__ ) || defined( __APPLE_CC__ )
#      define PLATFORM_MAC
#   endif
#endif

#ifndef PLATFORM_LINUX
#	if defined(_UNIX) || defined(__linux) || defined(_linux)
#		define PLATFORM_LINUX
#	endif
#endif
Simply check for the platform you need.
rogerborg wrote:Every time someone learns to use a debugger, an angel gets their wings.
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

And anyway, anyone can just use the platform's defines to check the platform...
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

He is using the platform defines, he wants to know the platform at runtime. I wouldn't recommend using a string here, maybe an enum or something? Though I don't think platform specific code should be determined at runtime, it's slightly pointless since you will most likely need to recompile for different platforms anyway.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
Post Reply