how to identify which OS I am using

Reply

Join Date: Feb 2008
Posts: 25
Reputation: onemanclapping is an unknown quantity at this point 
Solved Threads: 0
onemanclapping onemanclapping is offline Offline
Light Poster

how to identify which OS I am using

 
0
  #1
Oct 1st, 2008
hello. I'm doing a program in c++ and I need to use a system call to "clear" my screen.
for linux I use system("clear") but in windows the same is system("clr").
is there some way to find which OS is being used at the moment of execution so the program can decide whether it should use "clear" ou "clr"?
thanks in advance
Last edited by onemanclapping; Oct 1st, 2008 at 9:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: how to identify which OS I am using

 
0
  #2
Oct 1st, 2008
There is no way to determine what OS the program is running on. Perhaps make two functions, and if say the windows function fails, run the Linux function.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 25
Reputation: onemanclapping is an unknown quantity at this point 
Solved Threads: 0
onemanclapping onemanclapping is offline Offline
Light Poster

Re: how to identify which OS I am using

 
0
  #3
Oct 1st, 2008
Originally Posted by dmanw100 View Post
There is no way to determine what OS the program is running on. Perhaps make two functions, and if say the windows function fails, run the Linux function.
oh, ok, no problem.
how can my program tell if the windows function has failed? because if "clr" works and then I call "clear", there will be an output error message.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: how to identify which OS I am using

 
0
  #4
Oct 1st, 2008
The best way to do this would be know what OS your program will be working on. I think if you put the Windows call in a function and set it to return a bool true if successful and false if not (using an if statement) then you would be able to determine if you should call the other function.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,149
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: how to identify which OS I am using

 
0
  #5
Oct 1st, 2008
The program must know what operating system its running under because it has to be recompiled for each os. Add some sort of preprocessor directive that tells the program what os its being compiled for. For example, you might put the define _WIN32 in the makefile when compiled for MS-Windows and _UNIX in another make file when being compiled for *nix os. Inside the program you can use that macro to determine which operating system its being compiled for.
  1. #ifdef _WIN32
  2. // do something for MS-Windows
  3. #elif defined(_UNIX)
  4. // beging compiled for *nix
  5. #edndif
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,149
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: how to identify which OS I am using

 
0
  #6
Oct 1st, 2008
Originally Posted by dmanw100 View Post
The best way to do this would be know what OS your program will be working on. I think if you put the Windows call in a function and set it to return a bool true if successful and false if not (using an if statement) then you would be able to determine if you should call the other function.
\


Not really. If the program is being compiled for *nix then the MS-Windows win32 api functions are not even available to the program, so it is impossible for the program to call it.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 25
Reputation: onemanclapping is an unknown quantity at this point 
Solved Threads: 0
onemanclapping onemanclapping is offline Offline
Light Poster

Re: how to identify which OS I am using

 
0
  #7
Oct 1st, 2008
Originally Posted by Ancient Dragon View Post
Inside the program you can use that macro to determine which operating system its being compiled for.
  1. #ifdef _WIN32
  2. // do something for MS-Windows
  3. #elif defined(_UNIX)
  4. // beging compiled for *nix
  5. #edndif
Once again, thank you very much for your help in another thread of mine!
so you think this should work?
  1. void clearScreen()
  2. {
  3. #ifdef _WIN32
  4. system("cls");
  5. #elif defined(_UNIX)
  6. system("clear");
  7. #endif
  8. }
I'm sorry to be asking but I don't have a windows system at the moment to try it :/
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,149
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1435
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: how to identify which OS I am using

 
0
  #8
Oct 1st, 2008
That will work providing you create the makefiles for each os as I described previously.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: how to identify which OS I am using

 
0
  #9
Oct 1st, 2008
The exact macro to test for depends on OS and compiler. Here is a handy reference for Pre-defined C/C++ Compiler Macros.

Unless you have been careful to predefine specific macros in your makefiles as AD instructed, you should test for all the possible macros that you might expect. On Windows, I usually have something like
  1. #if defined(__WIN32__) || defined(__WIN32) || defined(_WIN32) || defined(WIN32)
  2. #define __WIN32__
  3. #endif
  4.  
  5. ...
  6.  
  7. #ifdef __WIN32__
  8. // windows stuff here
  9. #elif defined( ... )
  10. // other stuff here
  11. #else
  12. // generic stuff here
  13. #endif
Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 25
Reputation: onemanclapping is an unknown quantity at this point 
Solved Threads: 0
onemanclapping onemanclapping is offline Offline
Light Poster

Re: how to identify which OS I am using

 
0
  #10
Oct 2nd, 2008
Originally Posted by Ancient Dragon View Post
That will work providing you create the makefiles for each os as I described previously.
what do you mean by "create the makefiles for each os"? sorry, I'm a bit of a noob :/

I did:
  1. #include <iostream>
  2. using std::cout;
  3. using std::cin;
  4. using std::endl;
  5.  
  6. void clearScreen()
  7. {
  8. #ifdef _WIN32
  9. system("cls");
  10. #elif defined(_UNIX)
  11. system("clear");
  12. #endif
  13. }
  14.  
  15. int main()
  16. {
  17. cout << "coco" << endl;
  18. cin.get();
  19. clearScreen();
  20. cout << "coco2" << endl;
  21. }

and the result:
  1. omc@linus-marx:~$ /home/omc/workspace/LigaSagres/Debug/LigaSagres
  2. coco
  3.  
  4. coco2
  5. omc@linus-marx:~$

so it didn't work. I guess maybe because I have not created "the makefiles for each os"
Last edited by onemanclapping; Oct 2nd, 2008 at 3:28 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC