how to write os version independent code?

W

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
 
W

walter

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.
 
R

rihad

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...
 
W

walter

rihad:
"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.
 
J

James Hu

walter:
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
 
J

James Hu

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
 
K

Keith Thompson

James Hu said:
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).
 
M

Malcolm

walter said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top