DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   how to identify which OS I am using (http://www.daniweb.com/forums/thread148611.html)

onemanclapping Oct 1st, 2008 9:20 pm
how to identify which OS I am using
 
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 :)

dmanw100 Oct 1st, 2008 9:30 pm
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.

onemanclapping Oct 1st, 2008 9:43 pm
Re: how to identify which OS I am using
 
Quote:

Originally Posted by dmanw100 (Post 703116)
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.

dmanw100 Oct 1st, 2008 9:52 pm
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.

Ancient Dragon Oct 1st, 2008 9:55 pm
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.
#ifdef _WIN32
// do something for MS-Windows
#elif defined(_UNIX)
// beging compiled for *nix
#edndif

Ancient Dragon Oct 1st, 2008 9:57 pm
Re: how to identify which OS I am using
 
Quote:

Originally Posted by dmanw100 (Post 703128)
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.

onemanclapping Oct 1st, 2008 10:05 pm
Re: how to identify which OS I am using
 
Quote:

Originally Posted by Ancient Dragon (Post 703129)
Inside the program you can use that macro to determine which operating system its being compiled for.
#ifdef _WIN32
// do something for MS-Windows
#elif defined(_UNIX)
// beging compiled for *nix
#edndif

Once again, thank you very much for your help in another thread of mine!
so you think this should work?
void clearScreen()
{
        #ifdef _WIN32
        system("cls");
        #elif defined(_UNIX)
        system("clear");
        #endif
}
I'm sorry to be asking but I don't have a windows system at the moment to try it :/

Ancient Dragon Oct 1st, 2008 10:10 pm
Re: how to identify which OS I am using
 
That will work providing you create the makefiles for each os as I described previously.

Duoas Oct 1st, 2008 11:40 pm
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
#if defined(__WIN32__) || defined(__WIN32) || defined(_WIN32) || defined(WIN32)
  #define __WIN32__
#endif

...

#ifdef __WIN32__
  // windows stuff here
#elif defined( ... )
  // other stuff here
#else
  // generic stuff here
#endif
Hope this helps.

onemanclapping Oct 2nd, 2008 3:27 pm
Re: how to identify which OS I am using
 
Quote:

Originally Posted by Ancient Dragon (Post 703135)
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:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

void clearScreen()
{
        #ifdef _WIN32
        system("cls");
        #elif defined(_UNIX)
        system("clear");
        #endif
}

int main()
{
        cout << "coco" << endl;
        cin.get();
        clearScreen();
        cout << "coco2" << endl;
}

and the result:
omc@linus-marx:~$ /home/omc/workspace/LigaSagres/Debug/LigaSagres
coco

coco2
omc@linus-marx:~$

so it didn't work. I guess maybe because I have not created "the makefiles for each os"


All times are GMT -4. The time now is 3:54 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC