Computing.Net > Forums > Programming > Detecting Operating System in C++

Detecting Operating System in C++

Reply to Message Icon

Original Message
Name: Tiedye
Date: December 4, 2003 at 16:57:37 Pacific
Subject: Detecting Operating System in C++
OS: Windows/Linux
CPU/Ram: Athlon/512
Comment:

I searched Google and these forums for a bit, but I can't seem to figure out how to make my program detect which operating system it's running on. I'm using system("clear") for Linux and system("cls") for Windows to clear the screen, but my program needs to know how to tell which one to use. Any help would be appreciated.



Report Offensive Message For Removal

Response Number 1
Name: santa
Date: December 4, 2003 at 22:30:22 Pacific
Subject: Detecting Operating System in C++
Reply: (edit)

(first off, my brain shut off 1 hour ago but here's my two cents..)

can you add vb or perhaps take a peak at some vb functions for hints on logic or a lead to search further into it?

GetVersion() and GetVersionEx() or http://www.mvps.org/vbnet/index.html?code/system/getversionex.htm or uhm... search on msdn.microsoft.com or sourceforge.net? sorry i don't have any exact answer for you at the moment. :(


Report Offensive Follow Up For Removal

Response Number 2
Name: Ronin1
Date: December 4, 2003 at 22:45:00 Pacific
Subject: Detecting Operating System in C++
Reply: (edit)

?

Do you mean which platform your code is going to be compiled on? A linux/windows binary isn't going to run on Windows/linux OS I don't believe unless it's through an emulator.

You might try searching for "cross compiling" to find some source code samples. The samples there typically have such things as OS dependency, but I don't know the procedure myself.


Report Offensive Follow Up For Removal

Response Number 3
Name: Sord
Date: December 5, 2003 at 22:59:24 Pacific
Subject: Detecting Operating System in C++
Reply: (edit)

santa: vb is not only sh*t but doesnt run on any os other than windows

Tiedye: if you want to do it at runtime it could be a bit difficult, you could check for uname or other files specific to the different OSs, however the best way i think would be to use something like this

#define Linux
...
#ifdef Linux
system("clear");
#else
system("cls");
#endif

or

#define clearscreen "clear" (would change to cls for windows of course)
...
system(clearscreen)

Then just package your sources so the windows version defines as Windows/cls and the other as Linux/clear


Report Offensive Follow Up For Removal

Response Number 4
Name: null_user
Date: January 22, 2004 at 11:42:24 Pacific
Subject: Detecting Operating System in C++
Reply: (edit)

Assuming you are using GCC, there are predifind symbols that identify the platform. For Linux, you can use __linux__ or linux, but the first is recommended since it is POSIX compliant.

// Declare a function to handle clear screens
// independant of the operating system
// (Linux or Windows only)
void clear();

int main ()
{
...

// When you want to clear the console,
// call the clear() function
clear();

...

}
// Function
void clear()
{
//System specific code for linux/windows
#ifdef __linux__ || linux
system("cls");
#else
system("CLEAR");
#endif

}//end clear()

For more information look up "portable C++".


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Detecting Operating System in C++

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software