Connecting Tech Pros Worldwide Forums | Help | Site Map

how to write os version independent code?

walter
Guest
 
Posts: n/a
#1: Nov 13 '05
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
Guest
 
Posts: n/a
#2: Nov 13 '05

re: how to write os version independent code?


walter:[color=blue]
> 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
>[/color]
Some of my PC ares multi-processors, some are not.
The kernel I'm using is 2.4.x.

rihad
Guest
 
Posts: n/a
#3: Nov 13 '05

re: how to write os version independent code?


On Fri, 07 Nov 2003 15:08:40 +0800, walter <gw.bbs@veryname.com> wrote:
[color=blue]
> 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?
>[/color]

"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...
walter
Guest
 
Posts: n/a
#4: Nov 13 '05

re: how to write os version independent code?


rihad:[color=blue]
> On Fri, 07 Nov 2003 15:08:40 +0800, walter <gw.bbs@veryname.com> wrote:
>
>[color=green]
>> 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?
>>[/color]
>
>
> "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...[/color]

The "everywhere" here is only for my Linux machines.
I don't like java, it's too slow, consumes too much resources.

James Hu
Guest
 
Posts: n/a
#5: Nov 13 '05

re: how to write os version independent code?


On 2003-11-07, walter <gw.bbs@veryname.com> wrote:[color=blue]
> walter:[color=green]
>> 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
>>[/color]
> Some of my PC ares multi-processors, some are not.
> The kernel I'm using is 2.4.x.[/color]

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
EventHelix.com
Guest
 
Posts: n/a
#6: Nov 13 '05

re: how to write os version independent code?


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/RealtimeMa...ndOrdering.htm

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Generate Sequence Diagrams and Use Case Diagrams in PDF
James Hu
Guest
 
Posts: n/a
#7: Nov 13 '05

re: how to write os version independent code?


On 2003-11-07, EventHelix.com <eventhelix@hotmail.com> wrote:[color=blue]
> 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/RealtimeMa...ndOrdering.htm[/color]

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
Keith Thompson
Guest
 
Posts: n/a
#8: Nov 13 '05

re: how to write os version independent code?


James Hu <jxh@despammed.com> writes:[color=blue]
> On 2003-11-07, walter <gw.bbs@veryname.com> wrote:[color=green]
> > walter:[color=darkred]
> >> 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
> >>[/color]
> > Some of my PC ares multi-processors, some are not.
> > The kernel I'm using is 2.4.x.[/color]
>
> 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.[/color]

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) kst@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Malcolm
Guest
 
Posts: n/a
#9: Nov 13 '05

re: how to write os version independent code?



"walter" <gw.bbs@veryname.com> wrote in message[color=blue]
> 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?
>[/color]
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.


Closed Thread