943,649 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5118
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Oct 2nd, 2008
0

Re: how to identify which OS I am using

Unix has various flags.
It doesn't have to do with makefiles.

Try writing instead of _UNIX:
_unix, unix, _UNIX_, UNIX... etc...

The best thing is: see what compiler are you using, and then look at its documentation, because compilers are setting those flags, and different compilers name different names (for exactly, or almost, the same thing)
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 2nd, 2008
0

Re: how to identify which OS I am using

I would sugguest, for the sake of portability, you never use system() calls. Instead clear the screen manually, or just print a large number of end lines.
Reputation Points: 352
Solved Threads: 108
Master Poster
skatamatic is offline Offline
772 posts
since Nov 2007
Oct 2nd, 2008
0

Re: how to identify which OS I am using

To do the actual OS test, I would just check if c:\boot.ini exists. In the case of vista, since it uses a different booting scheme, you might want to check for another windows-specific system file, altough you would need elevated permissions to look inside the windows folder (i think). To do this, just use the fstream, open the file to be tested via ios::in, and test if it actually opened with .is_open() . If the windows files don't exist, assume linux. This is a bit of a band-aid but is a fairly surefire method of getting the OS.
Reputation Points: 352
Solved Threads: 108
Master Poster
skatamatic is offline Offline
772 posts
since Nov 2007
Oct 2nd, 2008
0

Re: how to identify which OS I am using

I think we have an impressive example of a false problem.

If you want to clear console window in portable manner, use portable direct console i/o library (pdcurses, for example, or others). That's the only right answer. May be a suitable ersatz of such libraries is <conio.h> stuff implemented with minimal variations in most of C and C++ programming systems. If you need to use standard console i/o (stdio.h or iostream), never use clear screen operation in your program interface design: no such entity as a screen in C and C++ stream i/o models.

As usually you can't run executable module on different OS in native mode, so the question "is there some way to find which OS is being used at the moment of execution so the program can decide..." is a senseless sentence. The obvious answer is "your program compiled and linked for OS1 can't run on OS2 so it find nothing on OS2, moreover it can't find itself on OS2".

It's the other question: "How to write portable sources which can select proper pieces of codes for different OS by C++ conditional compilation features?". Download good portable library (or system) and look at its configuration header. Of course, it looks like Ancient Dragon's example. But it's the other story and it's not so simple in common case.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Oct 2nd, 2008
0

Re: how to identify which OS I am using

If this is just some homework problem, you could try one, then the other. Only one will work:
C++ Syntax (Toggle Plain Text)
  1. if (system( "clear" )) system( "cls" );

However, like ArkM said, better to do it the Right Way.
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

Click to Expand / Collapse  Quote originally posted by ArkM ...
Download good portable library (or system) and look at its configuration header.
Thanks for your help. Which library do you suggest? Because just printing empty lines won't really work, as this way the program contents will always apeear at the bottom of the screen and that's no good for me :/

Is there any method I can do this without using a library? Because it is for this term's project that we should do ourselves, and maybe the teacher won't find it funny for me to use libraries made by others...

Thanks again!
Reputation Points: 10
Solved Threads: 0
Light Poster
onemanclapping is offline Offline
25 posts
since Feb 2008
Oct 2nd, 2008
0

Re: how to identify which OS I am using

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"
What do you mean by "it didn't work" ? I don't know what that stuff is that you posted.

"makefile for each os" means that you have to compile your program in each os -- once under *nix and again under MS-Windows. You can't compile once and run on both operating systems -- that won't work.

If you use g++ compiler then you can probably have one source file and use the same compiler on both os. You will need a version of g++ for unix and another version of g++ for MS-Windows.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,948 posts
since Aug 2005
Oct 2nd, 2008
1

Re: how to identify which OS I am using

for example on Linux you could compile using

g++ -D_LINUX file.cpp -o output # turns on the #define _LINUX

and on windows using

g++ -D_WIN32 file.cpp -o output #turns on #define _WIN32

So if you had a makefile on both systems, you could set your compiler flags to reflect the defines you need.
Last edited by stilllearning; Oct 2nd, 2008 at 8:26 pm.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 3rd, 2008
0

Re: how to identify which OS I am using

Sigh. You know, you really shouldn't be dinking with the screen at all. But if all you want is something to clear the screen in Win32 and POSIX, here are a couple of functions. Your teacher will know you didn't write them yourself!

C++ Syntax (Toggle Plain Text)
  1. // clearscreen.cpp
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. #if defined(__WIN32__) || defined(__WIN32) || defined(_WIN32) || defined(WIN32) || defined(__TOS_WIN__) || defined(__WINDOWS__)
  7. #ifndef __WIN32__
  8. #define __WIN32__
  9. #endif
  10. #include <windows.h>
  11. #else
  12. #include <unistd.h>
  13. #ifdef _POSIX_VERSION
  14. #ifndef __UNIX__
  15. #define __UNIX__
  16. #endif
  17. #include <term.h>
  18. #endif
  19. #endif
  20.  
  21. #if !defined(__WIN32__) && !defined(__UNIX__)
  22. #warning Works best on a Windows or POSIX platform.
  23. #endif
  24.  
  25. void clearscreen()
  26. {
  27. #if defined(__WIN32__)
  28.  
  29. HANDLE hStdOut;
  30. CONSOLE_SCREEN_BUFFER_INFO csbi;
  31. DWORD count;
  32. DWORD cellCount;
  33. COORD homeCoords = { 0, 0 };
  34.  
  35. hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  36. if (hStdOut == INVALID_HANDLE_VALUE) return;
  37.  
  38. /* Get the number of cells in the current buffer */
  39. if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  40. cellCount = csbi.dwSize.X *csbi.dwSize.Y;
  41.  
  42. /* Fill the entire buffer with spaces */
  43. if (!FillConsoleOutputCharacter(
  44. hStdOut,
  45. (TCHAR) ' ',
  46. cellCount,
  47. homeCoords,
  48. &count
  49. )) return;
  50.  
  51. /* Fill the entire buffer with the current colors and attributes */
  52. if (!FillConsoleOutputAttribute(
  53. hStdOut,
  54. csbi.wAttributes,
  55. cellCount,
  56. homeCoords,
  57. &count
  58. )) return;
  59.  
  60. /* Move the cursor home */
  61. SetConsoleCursorPosition( hStdOut, homeCoords );
  62.  
  63. #elif defined(__UNIX__)
  64.  
  65. static char* console_clearscreen = 0;
  66. if (!console_clearscreen)
  67. {
  68. int result;
  69. setupterm( NULL, STDOUT_FILENO, &result );
  70. if (result > 0)
  71. console_clearscreen = tigetstr( "clear" );
  72. }
  73. if (console_clearscreen)
  74. putp( console_clearscreen );
  75.  
  76. #else
  77. std::cout << std::string( 100, '\n' );
  78. #endif
  79. }
  80.  
  81. // end clearscreen.cpp
C++ Syntax (Toggle Plain Text)
  1. // clearscreen.hpp
  2.  
  3. #ifndef CLEARSCREEN_HPP
  4. #define CLEARSCREEN_HPP
  5.  
  6. void clearscreen();
  7.  
  8. #endif
  9.  
  10. // end clearscreen.hpp
Enjoy.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Oct 3rd, 2008
0

Re: how to identify which OS I am using

I think Duodas have posted a good example of multilevel environment recognition with subsequent macros standartisation (#define __WIN32__, for example). More complicated example from the Boost library:
http://www.boost.org/doc/libs/1_36_0...orm_config.hpp

The key point is: don't dissipate OS/compiler dependencies over all your codes. Incapsulate all this stuff in the only .h header file. Declare your own portable functions (and data structures) in this file. For example, suppose its name is omgconio.h:
C++ Syntax (Toggle Plain Text)
  1. #ifndef OMGCONIO_H
  2. #define OMGCONIO_H
  3. ...
  4. /** Explain ClrScr functionality */
  5. void ClrScr();
  6. int Peek();
  7. bool KbHit();
  8. int GetCh();
  9. ...
  10. #endif
Use only these functions calls in your source(s).

Create omgconio.cpp file with these functions inplementation. Place all OS/compiler selecting conditional compilation directives in this file only. Add omgconio.h and omgconio.cpp files to your projects.

Now you know where is OS/compiler dependency incapsulated if you will be ready to expand or correct your platform independent direct console i/o support.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 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