![]() |
| ||
| 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 :) |
| ||
| 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. |
| ||
| Re: how to identify which OS I am using Quote:
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. |
| ||
| 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. |
| ||
| 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 |
| ||
| Re: how to identify which OS I am using Quote:
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. |
| ||
| Re: how to identify which OS I am using Quote:
so you think this should work? void clearScreen()I'm sorry to be asking but I don't have a windows system at the moment to try it :/ |
| ||
| Re: how to identify which OS I am using That will work providing you create the makefiles for each os as I described previously. |
| ||
| 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)Hope this helps. |
| ||
| Re: how to identify which OS I am using Quote:
I did: #include <iostream> and the result: omc@linus-marx:~$ /home/omc/workspace/LigaSagres/Debug/LigaSagres 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