HPlogo HP C/HP-UX Programmer's Guide: HP 9000 Computers > Chapter 5 Programming for Portability

Using Name Spaces in HP C and ANSI C

» 

Technical documentation

Complete book in PDF

 » Table of Contents

The ANSI standard specifies exactly which names (for example, variable names, function names, type definition names) are reserved. The intention is to make it easier to port programs from one implementation to another without unexpected collisions in names. For example, since the ANSI C standard does not reserve the identifier open, an ANSI C program may define and use a function named open without colliding with the open(2) system call in different operating systems.

HP Header File and Library Implementation of Name Spaces

The HP header files and libraries have been designed to support several different name spaces. On HP-UX systems, four name spaces are available:

Figure 5-5 HP-UX Name Spaces

HP-UX Name Spaces

The HP library implementation has been designed with the assumption that many existing programs will use more routines than those allowed by the ANSI C standard.

If a program calls, but does not define, a routine that is not in the ANSI C name space (for example, open), then the library will resolve that reference. This allows a clean name space and backward compatibility.

The HP header file implementation uses preprocessor conditional compilation directives to select the name space. In non-ANSI mode, the default is the HP-UX name space. Compatibility mode means that virtually all programs that compiled and executed under previous releases of HP C on HP-UX continue to work as expected. The following table provides information on how to select a name space from a command line or from within a program using the defined libraries.

Table 5-2 Selecting a Name Space in ANSI Mode

When using the name space...

Use command line option...

or #define in source program

Platform

HP-UX

-D_HPUX_SOURCE

#define _HPUX_SOURCE

HP-UX Only

XOPEN

-D_XOPEN_SOURCE

#define _XOPEN_SOURCE

HP-UX Only

POSIX

-D_POSIX_SOURCE

#define _POSIX_SOURCE

HP-UX

ANSI C

default

default

HP-UX

 

In ANSI mode, the default is ANSI C name space. The macro names _POSIX_SOURCE, _XOPEN_SOURCE, and _HPUX_SOURCE may be used to select other name spaces. The name space may need to be relaxed to make existing programs compile in ANSI mode. This can be accomplished by defining the _HPUX_SOURCE macro definition.

For example, in HP-UX:

#include <sys/types.h>
#include <sys/socket.h>

results in the following compile-time error in ANSI mode because socket.h uses the symbol u_short and u_short is only defined in the HP-UX name space section of types.h:

"/usr/include/sys/socket.h", line 79: syntax error:
u_short sa_family;

This error can be fixed by adding -D_HPUX_SOURCE to the command line of the compile.

© Hewlett-Packard Development Company, L.P.