Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

Hiw to determine the os?( PLEASE HELP.............)

I need to create a program which will run both under linux and windows system.  I know i need to do conditional compilation but the problem is how do i determine under what os is the program running? Please help...

0
toks725
Asked:
toks725
  • 2
  • 2
1 Solution
 
akshayxxCommented:
>> know i need to do conditional compilation
if u r ready to accept conditional compilation .. then u have to manually tell what OS you r running

in ur program  u can do this

#ifdef WIN32
//write windows code
printf("this shud be compiled on windows\n");
#endif
#ifdef UNIX
//write UNIX code
printf("this shud be compiled on unix\n");
#endif

and while compiling u can give this command
gcc -DWIN32      .. if u compile on windows
gcc -DUNIX      .. if u compile on unix

replace gcc with ur favorite compiler .. and if u using MSVC then they have option of setting -DWIN32 inproject settings

also this does not says that if on linux u cant do
gcc -DWIN32 .. it wont complain with the command .. but in most cases the windows specific code will give compilation errors..
0
 
frogger1999Commented:
If you want to use the Micorsoft "standard" way of checking you are on windows then use _WIN32 (Visual C++ always defines this and cygwin gcc will with the -mwin32 compile flag on)

and assuming that you are only compiling for windows or linux then this will suffice.

#ifdef _WIN32
//write windows code
printf("this shud be compiled on windows\n");
#else
//write UNIX code
printf("this shud be compiled on unix\n");
#endif
0
 
jmcgOwnerCommented:
Nothing has happened on this question in more than 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by frogger1999.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
 
akshayxxCommented:
jmcg: didnt my comment already covered whatever frogger1999 posted later on?
0
 
jmcgOwnerCommented:
Frogger identified preprocessor symbols which the compiler itself provides (at least some of the time). All of the compilers I've used have supplied at least one platform identifier symbol, but it can be a trick to figure out which symbol is the _right_ one to use for maximum portability of the source code. Contrary to what Frogger showed, it's fairly important to have a positive symbol match for each OS-dependent alternative with a final # else that throws an error saying that none of the OS-dependent symbols was defined.

Introducing your own symbols does not strike me as good practice in general, but it certainly can work.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now