| | |
how to identify which OS I am using
![]() |
•
•
Join Date: Feb 2008
Posts: 25
Reputation:
Solved Threads: 0
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
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.
•
•
Join Date: Feb 2008
Posts: 25
Reputation:
Solved Threads: 0
•
•
•
•
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.
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.
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)
#ifdef _WIN32 // do something for MS-Windows #elif defined(_UNIX) // beging compiled for *nix #edndif
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Feb 2008
Posts: 25
Reputation:
Solved Threads: 0
•
•
•
•
Inside the program you can use that macro to determine which operating system its being compiled for.
C++ Syntax (Toggle Plain Text)
#ifdef _WIN32 // do something for MS-Windows #elif defined(_UNIX) // beging compiled for *nix #edndif
so you think this should work?
C++ Syntax (Toggle Plain Text)
void clearScreen() { #ifdef _WIN32 system("cls"); #elif defined(_UNIX) system("clear"); #endif }
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
Hope this helps.
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)
#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
•
•
Join Date: Feb 2008
Posts: 25
Reputation:
Solved Threads: 0
•
•
•
•
That will work providing you create the makefiles for each os as I described previously.
I did:
C++ Syntax (Toggle Plain Text)
#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:
C++ Syntax (Toggle Plain Text)
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"
Last edited by onemanclapping; Oct 2nd, 2008 at 3:28 pm.
![]() |
Similar Threads
- Identify a 16-bit Program (Windows tips 'n' tweaks)
- Can you identify this motherboard? (Motherboards, CPUs and RAM)
- Using a Variable to Identify an Instance (Python)
- Scam Job Emails And How To Identify Them (IT Professionals' Lounge)
- Database error cord identify (Java)
- How can I identify a Broadcast Storm - Pls Help (Networking Hardware Configuration)
- c++ arrays, help identify element place (C++)
- How to identify running programs (Windows 95 / 98 / Me)
Other Threads in the C++ Forum
- Previous Thread: Twofish Crypto algorithm
- Next Thread: Gaussian Noise function for images
| Thread Tools | Search this Thread |
addition api array base based binary bitmap bootloader c++ c/c++ char class classes code coding compile console conversion count data delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embed encryption error erroraftercompilation excel file forms fstream function functions game getline givemetehcodez gmail graph gui homework homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node output pointer problem program programming project python random read recursion reference rpg std::coutwstring string strings systrayfail temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






