HP C/HP-UX Online Help
Migrating C Programs to HP-UX
Migrating an Application
Byte Order
Data Alignment
Unsupported Keywords
Predefined Macro Names
White Space
Hexadecimal Escape Sequence
Invalid Structure References
Leading Underscore
Library Functions
Floating-Point Format
Bit Fields
Data Storage and Alignment
Typedefs
This section discusses issues to consider when migrating C language programs
from VAX systems, HP 9000 Series 300/400, and HP 9000 Series 500 computers
to HP workstations and servers. The first section lists some steps
you need to take to migrate an application program to an HP 9000 workstation
or server. Subsequent sections in This section highlight major differences
between various C compilers and suggest how to modify source files to ease
migration.
Because C is a highly portable language, if you follow the recommendations
given in Programming for Portability , your program
should migrate easily. However, if you use system-dependent programming
practices, a program that executes successfully on one computer may not
execute properly when transferred to a HP 9000 workstation or server. For
example, if you use system-specific I/O routines outside of the standard
C library, you will have difficulty with portability.
Migrating an Application
Following are the general steps to migrate a C program from an HP-UX or
UNIX system.
- Test your program on the current system so you have a copy of the results.
- Use the tar command (see the HP-UX Reference Manual)
with the cv options to transfer the source files you want to migrate
to tape.
- Use the tar command with the r option to transfer
any associated data files to tape.
- Install the source files and any related data files on the HP 9000 workstation
or server using the tar command with the x option.
- Check your makefiles for any implementation-specific options. Change
programs depending on implementation-specific command options. On HP-UX
systems, these options are generally preceded by -W or +,
and may include options to be passed to ld or cpp. You
can optionally include the -g option to permit symbolic debugging.
- Review Programming for Portability and Practices
to Avoid and check over the source code for system-dependent programming.
(If the source files are extensive, you may want to skip this step and
catch errors when you run lint or compile.)
- Search for instances of #include files and make sure that the
files or routines included appear in the correct directory or library on
the HP 9000 workstation or server.
- Run lint, a C program checker that verifies source code and
prints warning messages about problems with the source code style, efficiency,
portability, and consistency.
- Compile the program on the HP 9000 workstation or server using the cc
command. (Refer to the HP C/HP-UX Reference Manual for details about
the cc command and options, and explanations of error, warning,
and panic messages.) Change the source code to resolve any messages you
receive.
- Recompile the program until you receive no messages.
- Link the program. The linker reports any symbols that cannot be found.
- Run the program on the HP 9000 workstation or server. Compare the results
with those received on the original computer.
Byte Order
The VAX computer has a different byte order from HP 9000 computers. Binary
data files created on a VAX computer may need to be swapped before they
can be interpreted on an HP 9000 workstation or server. Use the descriptions
of storage and alignment on both systems to write a programming tool to
reorder the data. The C library function
swab (see the
HP-UX
Reference Manual) can be used to swap bytes, if that is sufficient
for the particular application. Otherwise, you need to write a customized
tool. ASCII code and data files should migrate to the HP 9000 workstation
or server without change.
Data Alignment
The HP 9000 workstations and servers are more strict than other machines
with respect to data alignment. Misaligned data addresses cause bus errors
when attempting to dereference them. Use the +w1 option when compiling
to report occurrences of "Casting from loose to strict alignment." Fix
occurrences that result from using the address of a more loosely aligned
item (such as char) to access a more strictly aligned item (such
as int).
Unsupported Keywords
Some implementations of C permit use of the keywords asm,
fortran,
and
entry. These are not supported on the HP 9000 workstations
and servers. You must rewrite any code that uses these keywords.
Predefined Macro Names
In non-ANSI mode, there are several HP C specific macro names defined.
These names may conflict with identifiers used in the source code.
The HP 9000 workstation and server preprocessors predefine the macro
names PWB,
hpux, and unix. The HP 9000 workstations
and servers predefine the macro name hp9000s800; the HP 9000 Series
500 predefines hp9000s500; and the HP 9000 Series 300/400 predefine
the macro name
hp9000s300. The VAX predefines the macro name vax.
If any of these macro names is used as an identifier in the source code,
use the #undef preprocessor directive to "undefine" the macro
or rename the identifier(s).
In ANSI mode, none of the above macro names are defined and you should
not have difficulty with these HP C specific macro names.
White Space
HP 9000 Series 300/400, 500, and workstation and server preprocessors do
not include trailing white space in the replacement text of a macro. The
VAX preprocessor includes the trailing white space. If your program depends
on the inclusion of the white space, you can place white space around the
macro invocation.
Hexadecimal Escape Sequence
The HP 9000 workstations and servers compiler allows character constants
containing hexadecimal escape sequences. For example,
'A' can
be expressed with the hexadecimal escape sequence '\x41'. The
HP 9000 Series 200, 300, and 500 do not allow hexadecimal escape sequences.
Check your source files for any occurrences for \x, and verify
that a hexadecimal escape sequence is intended.
Invalid Structure References
The HP 9000 workstations and server compiler does not allow structure members
to be referenced through a pointer to a different type of object. The VAX
pcc and HP 9000 Series 200 and 500 compilers allow this. Change any invalid
structure references to cast the pointer to the appropriate type before
referencing the member. For example, given the following:
struct x {
int y;
}z;
char *c;
c -> y=5;
c -> y=5; is invalid. Instead, use the following code:
c = (char *) &z;
((struct x *) c)->y = 5;
Leading Underscore
External names on the HP 9000 workstations and servers do not contain a
leading underscore. You need to change any programs that rely on external
names containing leading underscores. Note that all languages on the HP
9000 workstations and servers follow the same convention. Therefore, only
assembly language code and names that were aliased in other languages are
affected by this. Because there is no leading underscore, external names
contain one additional significant character. Identifiers that differ only
in the 255th character will denote different items on the HP 9000 workstations
and servers.
Library Functions
The set of library routines available on HP-UX systems may differ from
those available on BSD 4.2 systems. If you encounter an unresolved function
after linking, refer to the HP-UX Reference Manual to see if there
is an HP-UX function that does what you want it to do. If not, you will
have to write one of your own.
Floating-Point Format
The VAX floating-point representation is different from that on HP 9000
computers. You will have to change any programs dependent on the characteristics
of VAX floating point. In particular, this difference could expose errors
in the code that happen to work acceptably on the VAX. These errors include
mismatched function return types (float on one side, double
on the other), and passing the address of a double instead of
a float to scanf. The VAX representation of a float
differs in the number of bits in the exponent, as well as the mantissa.
Therefore, mismatched types can cause a vastly different answer on HP 9000
computers.
Bit Fields
The HP 9000 workstations and servers C compiler treats bit-fields without
the unsigned type modifier as signed. The VAX, HP 9000 Series
300/400, and 500 compilers treat them as unsigned.
Data Storage and Alignment
The alignment requirements of some data types are different on the HP 9000
workstations and servers. Check any externally imposed data structure layouts
for differences. These may include byte and bit-field order, if you are
migrating from a VAX, or different internal padding for structure member
alignment. On the HP 9000 workstations and servers, doubles must
be aligned on a 64-bit boundary, whereas other machines require alignment
on a 32-bit boundary. Refer to Data Storage and Alignment for complete storage and alignment
information.
Typedefs
The HP C compiler does not allow combining type specifiers with typedef
names.
For example:
typedef long t;
unsigned t var;
Compilers derived from pcc accept this code, but HP C does not. Change
the typedef to include the type specifier:
typedef unsigned long t;
t var;
or use a define:
#define t long
unsigned t var;