Find OS in C

This is a discussion on Find OS in C within the C Programming forums, part of the General Programming Boards category; Is there a function to get the OS that a C program is running in? I need to write a ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    Find OS in C

    Is there a function to get the OS that a C program is running in? I need to write a program that will run in Linux, Minix, & Win32. But it needs to do OS specificsystem calls. How can I determine what OS I am in from withing the program without prompting the user.

    Thanx
    Snuffy2

  2. #2
    .
    Join Date
    Aug 2001
    Posts
    598
    There is no way to create a single exe file that will run on all systems if that is what you want to do. You would need to compile seprete seperate exe files for each OS. So it would be much easer to port it then any thing else. (you might be able to find a cross-plaftfom libbary that will allow you just to take your code from compiler to compiler and compile for the diffrent operating systems_
    To Err Is To Be Human. To Game Is Divine!"

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    3
    I understand I'm gonna need to recompile each time, but my assignment is to use 1 C file for all 3 OSs. So it needs to be able to determine the OS and use the specific calls for that os.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    An idea is to let the user tell you

    \?program win32

    or

    \?program linux

    etc.

    and go from there

  5. #5
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Use defines such as

    #ifdefine __WIN32__
    and so on

  6. #6
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212

    Exclamation

    I would be interested in knowing more on how to do this could you please elaborate a bit more.

  7. #7
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You don't need to know what os it is at runtime
    only when you compile. Most operating systems
    and compilers define variables such as __WIN32__
    that you can use.

  8. #8
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Not variables persay but you get what I mean right?

  9. #9
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You could do something similar to this.

    Code:
    #ifdef __UNIX__
    #include <unistd.h>
    #endif
    
    #ifdef __UNIX__
    #define HAS_SLEEP
    #endif
    
    int main(void)
    {
         #ifdef HAS_SLEEP
         sleep(3);
         #endif
    
         return 0;
    }

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    3
    The __WIN32__ variable looks like what I am looking for, is there an yplace where I can find a list of what the actual var is defined for each OS???

  11. #11
    .
    Join Date
    Aug 2001
    Posts
    598
    __APPLE__ is apple

    __MACH__ is for Mach
    To Err Is To Be Human. To Game Is Divine!"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 10:13 AM
  2. Can't find DirectX!!!
    By Queatrix in forum C++ Programming
    Replies: 2
    Last Post: 07-24-2006, 08:50 PM
  3. Replies: 3
    Last Post: 06-09-2006, 10:53 AM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM

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