how to write os version independent code?

Discussion in 'C Programming' started by walter, Nov 7, 2003.

  1. walter

    walter Guest

    I have many computers running Linux. For some reasons,
    the versions are not the same. Now, I want to write a C program
    to run on them. "Compile once and run everywhere". Is there
    anything I have to take care of?

    Thanks
     
    walter, Nov 7, 2003
    #1
    1. Advertising

  2. walter

    walter Guest

    walter:
    > I have many computers running Linux. For some reasons,
    > the versions are not the same. Now, I want to write a C program
    > to run on them. "Compile once and run everywhere". Is there
    > anything I have to take care of?
    >
    > Thanks
    >

    Some of my PC ares multi-processors, some are not.
    The kernel I'm using is 2.4.x.
     
    walter, Nov 7, 2003
    #2
    1. Advertising

  3. walter

    rihad Guest

    On Fri, 07 Nov 2003 15:08:40 +0800, walter <> wrote:

    > I have many computers running Linux. For some reasons,
    >the versions are not the same. Now, I want to write a C program
    >to run on them. "Compile once and run everywhere". Is there
    >anything I have to take care of?
    >


    "Compile once and run everywhere"? That's more like Java, I'm afraid. In C it's
    rather "Write once and compiler everywhere". In an ideal world, that is...
     
    rihad, Nov 7, 2003
    #3
  4. walter

    walter Guest

    rihad:
    > On Fri, 07 Nov 2003 15:08:40 +0800, walter <> wrote:
    >
    >
    >> I have many computers running Linux. For some reasons,
    >>the versions are not the same. Now, I want to write a C program
    >>to run on them. "Compile once and run everywhere". Is there
    >>anything I have to take care of?
    >>

    >
    >
    > "Compile once and run everywhere"? That's more like Java, I'm afraid. In C it's
    > rather "Write once and compiler everywhere". In an ideal world, that is...


    The "everywhere" here is only for my Linux machines.
    I don't like java, it's too slow, consumes too much resources.
     
    walter, Nov 7, 2003
    #4
  5. walter

    James Hu Guest

    On 2003-11-07, walter <> wrote:
    > walter:
    >> I have many computers running Linux. For some reasons,
    >> the versions are not the same. Now, I want to write a C program
    >> to run on them. "Compile once and run everywhere". Is there
    >> anything I have to take care of?
    >>
    >> Thanks
    >>

    > Some of my PC ares multi-processors, some are not.
    > The kernel I'm using is 2.4.x.


    You can write strictly conforming code. This gives you the best chance
    that your programs will do exactly what you intended no matter where
    you port your code to, so long the target platform supports the version
    of Standard C that you are coding to.

    If you require features that are outside of C, your next best bet
    is to write code that conforms to POSIX. But discussion POSIX
    conformance is beyond the scope of this newsgroup. You best bet
    would be to talk to the folks at: comp.unix.programmer

    If you require features that are outside of POSIX, then you will
    need to dig into the interfaces that are specific to Linux. Your
    best bet is to find an appropriate newsgroup under: comp.os.linux*

    -- James
     
    James Hu, Nov 7, 2003
    #5
  6. EventHelix.com, Nov 7, 2003
    #6
  7. walter

    James Hu Guest

    On 2003-11-07, EventHelix.com <> wrote:
    > If you are just running with the same Kernal, it will actually be hard
    > to write code that doesn't work the same on different machines.
    >
    > If you go across multiple CPU architectures you need to be careful
    > about byte alignment and ordering issues.
    >
    > Checkout the following article:
    > http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm


    Byte alignment and byte ordering only become an issue if one writes code
    that is dependent upon such things. And if one does so purposefully,
    (such as to store data as binary and have it read by a program running
    on a different machine), such code relies upon implementation defined
    behavior (and can invoke undefined behavior in the program reading
    the binary data). Strictly conforming code would not depend upon
    implementation defined behavior.

    The closest to a Standard C solution to the above scenario would be to
    store the data as text. However, it is still implementation defined as
    to whether the stored text can be read properly by some other program
    running on some other machine. (It depends on EOL discipline, character
    set, and perhaps other issues, each issue being off-topic here.)

    Since the OP states all his machines are Linux, does Linux on IBM
    mainframes use EBCDIC?

    -- James
     
    James Hu, Nov 7, 2003
    #7
  8. James Hu <> writes:
    > On 2003-11-07, walter <> wrote:
    > > walter:
    > >> I have many computers running Linux. For some reasons,
    > >> the versions are not the same. Now, I want to write a C program
    > >> to run on them. "Compile once and run everywhere". Is there
    > >> anything I have to take care of?
    > >>
    > >> Thanks
    > >>

    > > Some of my PC ares multi-processors, some are not.
    > > The kernel I'm using is 2.4.x.

    >
    > You can write strictly conforming code. This gives you the best chance
    > that your programs will do exactly what you intended no matter where
    > you port your code to, so long the target platform supports the version
    > of Standard C that you are coding to.


    Strict conformance gives you portability of your source code, but the
    OP specifically wants portability of the executable. This is
    typically difficult or impossible across different processor
    architectures (e.g., x86 vs. Alpha), but it's probably feasible if you
    limit yourself to x86 systems. It's also well beyond the scope of
    this newsgroup.

    Try one of the Linux groups (I'm not sure whicn one is best).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
     
    Keith Thompson, Nov 8, 2003
    #8
  9. walter

    Malcolm Guest

    "walter" <> wrote in message
    > I have many computers running Linux. For some reasons,
    > the versions are not the same. Now, I want to write a C program
    > to run on them. "Compile once and run everywhere". Is there
    > anything I have to take care of?
    >

    It's a Linux issue. Some OSes, like MS DOS / Windows, have good backwards
    compatibility, others don't.
    If your program is pure ANSI C, it will be easy to recompile. However most
    real programs need some sort of GUI. Linux should provide
    back-compatibility, but again its a Linux issue. Even if a program is
    back-compatible, it may be practically unusable - for instance an old
    character-mapped program may run, but in many environments it would be
    unacceptable to users accustomed to a modern desktop.
     
    Malcolm, Nov 9, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Brady Drummonds

    Writing Compile-time Independent Code

    Scott Brady Drummonds, Jul 23, 2003, in forum: C++
    Replies:
    1
    Views:
    404
    Nick Keighley
    Jul 24, 2003
  2. V Green
    Replies:
    0
    Views:
    921
    V Green
    Feb 5, 2008
  3. PA Bear [MS MVP]
    Replies:
    0
    Views:
    1,033
    PA Bear [MS MVP]
    Feb 5, 2008
  4. MowGreen [MVP]
    Replies:
    5
    Views:
    2,080
    PA Bear [MS MVP]
    Feb 9, 2008
  5. Lui Core
    Replies:
    0
    Views:
    93
    Lui Core
    Aug 13, 2009
Loading...

Share This Page