Thread: determine the OS while running

  1. #1
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    determine the OS while running

    is there any easy way for a program to determine which OS is running it? if not, is there any way at all besides a user putting the information in at setup or something? what I eventually want to do is write something like this:
    Code:
    void ClearScreen()
    {
         if(OS=='W')
              system("cls");
         else if(OS=='L')
              system("clear");
         else
         {
               for(int i=500;i>1;i++)
                    std::cout<<std::endl;
         }
    }
    I already know clearing the screen should be avoided at all times, but I just wanted to create something that is a little more portable for the times where I really really need to clear the screen... (please don't post other ways to clear the screen... I really just want to know if/how you can determine which OS is running)
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    There most likely is some #defined value for Linux and for Windows, so you could probably use #if preprocessor directives.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    that's why i brought this up... I thought I used something like that once, but I can't seem to find the code I used it in
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    If the OSes are limited to Windows and Linux, you can just do this:
    Code:
    #ifdef __linux__
      linux stuff
    #else
      win32 stuff
    #endif
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User grady's Avatar
    Join Date
    Oct 2003
    Posts
    27
    Which symbols are defined depends on the compiler you are using. Googling for "gcc always defines" indicates __GNUC__ for gcc. Googling for `"always defined" visual c++` indicates _WIN32 will always be defined but i'm not sure. They say its defined for win32 applications which might mean 'not console applications', but then they say always defined. You will have to experiment.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > is there any easy way for a program to determine which OS is running it?
    No - the standard libraries of C and C++ are written to be as independent of the machine as possible (to make them as portable as possible).

    The answer is to call a function which is specific to your operating system, but then you run into "unresolved symbol" when you try and compile your code for something else (somewhat predictably).

    The real answer is as XSquared has said, and that is to use conditional compilation.
    To maintain some sense of sanity, its best to try and isolate anything machine specific into a few source files, like so
    Code:
    void myfoo ( void ) {
    #ifdef __linux__
      linuxfoo();
    #elif defined __win32__
      win32foo();
    #else
      #error No compatible foo
    #endif
    }
    Then all your other code calls myfoo() rather than cluttering up the code with an ever-expanding conditional complilation switch every time you want a foo().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ^that's what I was planning on doing with it... as in my first example...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get OS parameters
    By smboucou in forum C++ Programming
    Replies: 6
    Last Post: 11-26-2008, 12:15 PM
  2. Pics of Microsofts new OS, LongHorn XP
    By DarkViper in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 01-08-2003, 08:42 PM
  3. C++ Datatypes and OS Datatypes :: C++
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 12-07-2002, 02:06 PM
  4. What do YOU want in a OS?
    By commanderrulz in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 09-30-2001, 06:25 AM
  5. Find OS in C
    By Snuffy2 in forum C Programming
    Replies: 10
    Last Post: 09-09-2001, 04:40 PM