![]() |
| ||
| 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) |
| ||
| 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. |
| ||
| 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. |
| ||
| 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. |
| ||
| 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: if (system( "clear" )) system( "cls" ); However, like ArkM said, better to do it the Right Way. |
| ||
| Re: how to identify which OS I am using Quote:
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! |
| ||
| Re: how to identify which OS I am using Quote:
"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. |
| ||
| 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. |
| ||
| 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! // clearscreen.cpp // clearscreen.hppEnjoy. |
| ||
| 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: #ifndef OMGCONIO_HUse 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. |
| All times are GMT -4. The time now is 11:09 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC