?
Solved

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

Posted on 2003-03-05
6
Medium Priority
?
191 Views
Last Modified: 2010-04-15
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
Comment
Question by:toks725
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8070268
>> 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
 
LVL 1

Accepted Solution

by:
frogger1999 earned 400 total points
ID: 8077180
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
 
LVL 20

Expert Comment

by:jmcg
ID: 10088096
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 10089874
jmcg: didnt my comment already covered whatever frogger1999 posted later on?
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10089885
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question