Portability Issues

Stanley Friesen sarima at tdatirv.UUCP
Fri Apr 26 08:14:05 AEST 1991


In article <2573 at plx.UUCP> ming at plxsun.uucp (Lak-Ming Lam) writes:
>I would like to develop program which can be portable to different system
>(e.g. PC/MS-DOS, UNIX, Mac OS, etc.).  I would like to have anyone of you
>help me on the following issues:
>
>o	what need to be aware of (e.g. word size, byte order, etc.)
Well, there are tremendous differences in these areas across machines.
However, depending on what you want to do, this may not be very significant.
For most types of applications it is possible to write very simple code
that does not depend on word size or byte ordering.  Generally, if you
avoid dealing with binary files and type punning you are fairly safe.

The main area to watch for otherwise is using bit masks. Never assume a
particular number of bits in a word when masking a particular bit off.
You can avoid this by using the '~' operator to generate the inverse mask.
For example:
#define BAD_BIT 4
...
flags &= ~BAD_BIT;	/* Turn off BAD_BIT	*/

Your *real* problem in the list of systems you gave is in the OS interface.
They are totally and utterly different.  You can (hopefully) rely on the
existance of something approximating the standard I/O library on all of the
systems mentioned.  But this is rarely adequate for any but the simplest
applications.  Thus if you need something other than basic character &
line oriented I/O and simple memory allocation, you may have problems.

>o	how to use one set of source code to support different systems
>	(using #if MSDOS etc. or some other ways)

The solution is two-fold.   First, try to isolate as much of the OS
interface as possible into as few low-level routines as possible.
Second, use #if and #define to customize the OS interface portion
as needed.

A 'standard' header file that contains standard OS specific definitions
is quite helpful.  You can use #define macros here to make the system
calls have a standard appearance across all machines (at least to some
degree - where they are sufficiently compatible).

A great deal depends on just what OS facilities you need to use, and
which C compilers you are using on each machine.
-- 
---------------
uunet!tdatirv!sarima				(Stanley Friesen)



More information about the Comp.lang.c mailing list