tell which os is used?

This is a discussion on tell which os is used? within the C++ Programming forums, part of the General Programming Boards category; is there any c or c++ functions that will tell what OS is being used?...

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    tell which os is used?

    is there any c or c++ functions that will tell what OS is being used?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no standard way to do that in a program.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    No because that information is OS dependent.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    The only standard function you can use is system(). You'd pass it a command line string such as "uname" on UNIX to display the OS, but of course if you don't know what OS you're on you probably won't know what command to pass to system().
    Does it really matter though? If you try to run a UNIX program on Windows or visa versa, they certainly won't run.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    True enough, you can't cross-run things anyways.

    What you can do is to set a #define OS linux or #define OS windows (with -D in the compile flags for example).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by cpjust View Post
    Does it really matter though? If you try to run a UNIX program on Windows or visa versa, they certainly won't run.
    I guess not. I was only wondering because there are display differences between a Linux terminal and the Windows command prompt, and thought it would be nice if I could compensate for those differences (thus necessitating the OS info). Oh well, if it was that important, I suppose I could ask the user...
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  7. #7
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by matsp View Post
    What you can do is to set a #define OS linux or #define OS windows (with -D in the compile flags for example).

    --
    Mats
    or that!
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Or you could try to scan for OS-specific files and use OS-specific API!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by Elysia View Post
    Or you could try to scan for OS-specific files and use OS-specific API!
    or even that!

    so it can be done! sorta...
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, the thing is, that if you want to USE any OS-specific features in your code, you need to know which OS you are building the application for. You can write a piece of code that tries a number of different options, but it would make very little sense, because it would have to be compiled for the OS it runs on, and at the point of building, you (as in the programmer and/or the makefile, project definition file or whatever) will know what OS this is for. So there's no point in making things more complicated by trying to do it at runtime.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting if OS is Windows???
    By Ktulu in forum Windows Programming
    Replies: 2
    Last Post: 11-19-2006, 01:49 AM
  2. a simple OS
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-06-2004, 10:47 PM
  3. Linux OS to Windows OS code
    By sw9830 in forum C Programming
    Replies: 2
    Last Post: 02-28-2003, 02:11 PM
  4. Programming an OS
    By minesweeper in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-09-2002, 06:51 PM
  5. How do they compile code for an OS ?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 49
    Last Post: 03-27-2002, 11:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21