Detecting operating system... a step towards a portable code

This is a discussion on Detecting operating system... a step towards a portable code within the C Programming forums, part of the General Programming Boards category; Hi! I want to make a portable code - a code that could be used on pure MS-DOS, under Windows ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    6

    Question Detecting operating system... a step towards a portable code

    Hi!

    I want to make a portable code - a code that could be used on pure MS-DOS, under Windows and Linux OS.

    There are some routines that are different for these operating systems.
    My idea is, to write a procedure that figures out which operating system is running.

    Then, the program could run the appropriate routine that is compatible to the specific OS.

    Is this possible, and if it is, how to solve this problem?
    Any tips?

    Thank you!

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    So if I understand you correct, you want compiled code to be run on several OS'es? Then you would have to make a virtual machine to run your code on. You would have to create something comparable to the .NET runtime and the Java virtual machine. Note that such a virtual machine is not platform independent.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is this possible, and if it is, how to solve this problem?
    Yes, but it's considerably easier to use conditional compilation:
    Code:
    #if SYSTEM == __UNIX__
      /* Do UNIX stuff */
    #elif SYSTEM == __LINUX__
      /* Do Linux stuff */
    #elif SYSTEM == __WIN__
      /* Do Windows stuff */
    #elif SYSTEM == __DOS__
      #error Get a better OS!
    #endif
    -Prelude
    My best code is written with the delete key.

  4. #4
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    i can remember this question being asked only days ago... maybe this should go to the faq board?
    .sect signature

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    This is my first post on this board and I look very forward to staying as I have seen the quality posts on here *stops kissing*. What about for error checking purposes. Is there a predifined constant that is able you be used in a program that may only work on one OS?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there a predifined constant that is able you be used in a program that may only work on one OS?
    No, but once again you can use conditional compilation. For example, if you only wanted Windows users to compile your program:
    Code:
    #ifndef WIN32
      #error Must use Windows
    #endif
    This gives a compilation error if the operating system is not win32.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    So there is not a constant or function that returns the current version? Aiight, I'll make it then hehe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to detect the operating system at runtime?
    By sept in forum C Programming
    Replies: 26
    Last Post: 09-10-2008, 11:20 AM
  2. Operating System
    By Houssen in forum C Programming
    Replies: 18
    Last Post: 04-22-2008, 01:51 PM
  3. Replies: 8
    Last Post: 03-10-2008, 01:08 PM
  4. Create an operating system
    By histevenk in forum Tech Board
    Replies: 20
    Last Post: 10-17-2007, 05:30 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM

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