943,682 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5118
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 1st, 2008
0

how to identify which OS I am using

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
onemanclapping is offline Offline
25 posts
since Feb 2008
Oct 1st, 2008
0

Re: how to identify which OS I am using

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.
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008
Oct 1st, 2008
0

Re: how to identify which OS I am using

Click to Expand / Collapse  Quote originally posted by dmanw100 ...
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
onemanclapping is offline Offline
25 posts
since Feb 2008
Oct 1st, 2008
0

Re: how to identify which OS I am using

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.
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008
Oct 1st, 2008
0

Re: how to identify which OS I am using

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.
C++ Syntax (Toggle Plain Text)
  1. #ifdef _WIN32
  2. // do something for MS-Windows
  3. #elif defined(_UNIX)
  4. // beging compiled for *nix
  5. #edndif
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Oct 1st, 2008
0

Re: how to identify which OS I am using

Click to Expand / Collapse  Quote originally posted by dmanw100 ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Oct 1st, 2008
0

Re: how to identify which OS I am using

Inside the program you can use that macro to determine which operating system its being compiled for.
C++ Syntax (Toggle Plain Text)
  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?
C++ Syntax (Toggle Plain Text)
  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 :/
Reputation Points: 10
Solved Threads: 0
Light Poster
onemanclapping is offline Offline
25 posts
since Feb 2008
Oct 1st, 2008
0

Re: how to identify which OS I am using

That will work providing you create the makefiles for each os as I described previously.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Oct 1st, 2008
0

Re: how to identify which OS I am using

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
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Oct 2nd, 2008
0

Re: how to identify which OS I am using

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
onemanclapping is offline Offline
25 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Twofish Crypto algorithm
Next Thread in C++ Forum Timeline: Gaussian Noise function for images





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC