HPlogo HP C/HP-UX Reference Manual: Version A.06.05 > Chapter 9 Compiling and Running HP C Programs

Compiling HP C Programs

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

When you compile a program, it passes through one or more of the following steps depending upon which command line options you use:

  • Preprocessor: This phase examines all lines beginning with a # and performs the corresponding actions and macro replacements.

  • Compilation Process: This phase takes the output of the preprocessor and generates object code.

  • Optimization: This optional phase optimizes the generated object code.

  • Linking: In this phase, the linker is invoked to produce an executable program. External references in shared and archived libraries are resolved as required. The startup routines are copied in, and the C library in /usr/lib/hpux##/lib.so (## is 32 or 64) is referenced. (By default, shared libraries take precedence over archived libraries if both versions are available. However, if you use the LPATH environment variable, you should make sure that all shared libraries come before any archive library directories. See the HP-UX Linker and Libraries Online User Guide for information on LPATH or on creating and linking with shared libraries.) Object modules are combined into an executable program file.

The cc(1) Command

Use the cc(1) command to compile HP C programs. It has the following format:

cc [options] files

where:

options

is one or more compiler options and their arguments, if any. Options can be grouped together under one minus sign.

files

is one or more file names, separated by blanks. Each file is either a source or an object file.

Specifying Files to the cc Command

Files with names ending in .c are assumed to be HP C source files. Each HP C source file is compiled, producing an object file with the same name as the source file except that the .c extension is changed to a .o extension. However, if you compile and link a single source file into an HP C program in one step, the .o file is automatically deleted.

Files with names ending in .i are assumed to be preprocessor output files. Files ending in .i are processed the same as .c files, except that the preprocessor is not run on the .i file before the file is compiled.

Files with names ending in .s are assumed to be assembly source files; the compiler invokes the assembler to produce .o files from these.

Files with .o extensions are assumed to be relocatable object files that are included in the linking. All other files are passed directly to the linker by the compiler.

Specifying Options to the cc Command

Each compiler option has the following format:

     -optionname [optionarg]

where:

optionname

is the name of a standard compiler option.

optionarg

is the argument to optionname.

The optional argument -- delimits the end of options. Any following arguments are treated as operands (typically input filenames) even if they begin with the minus (-) character.

An Example of Using a Compiler Option

By default, the cc command names the executable file a.out. For example, given the following command line:

cc demo.c

the executable file is named a.out.

You can use the -o option to override the default name of the executable file produced by cc. For example, suppose my_source.c contains C source code and you want to create an executable file name my_executable. Then you would use the following command line:

cc -o my_executable my_source.c

Concatenating Options

You can concatenate some options to the cc command under a single prefix. The longest substring that matches an option is used. Only the last option can take an argument. You can concatenate option arguments with their options if the resulting string does not match a longer option.

For example, suppose you want to compile my_file.c using the -v, -g, and -DPROG=sub compiler options. There are several ways to do this:

cc my_file.c -v -g -DPROG=sub
cc my_file.c -vg -D PROG=sub

cc my_file.c -vgDPROG=sub
cc -vgDPROG=sub my_file.c

HP C Compiler Options

Table 9-1 “HP C Compiler Options at a Glance ” summarizes the command line options supported by HP Integrity servers. Some of these options are HP aC++ options. Refer to HP aC++/HP C Programmer’s Guide for detailed information on compiler options.

Table 9-1 HP C Compiler Options at a Glance

Option

Description

-.suffix

Directs output from the -E option into a file with a corresponding .suffix instead of a .c file.

-Aa

Enables strict ANSI C compliance.

-AA

Enables use of 2.0 Standard C++ library.

-Aarm

Enables Tru64 UNIX C++ ARM dialect.

-AC89

Enables ANSI C89 compliance.
-AC99Enables ANSI C99 compliance.

-Ae

Enables ANSI C89 compliance, HP value-added features (as described for +e option), and _HPUX_SOURCE name space macro. It is equivalent to -AC89 +e.

-Ag++

Enables GNU C++ dialiect compatibility.

-Agcc

Enables GNU C dialect compatibility.

-AP

Turns off the -AA mode and uses the older C++ runtime libraries.

-b

Creates a shared library rather than an executable file.

-Bdefault

Assigns default export class to global symbols.

-Bhidden

Assigns hidden export class to symbols.

-BexternPerforms the same operation as +Oextern=sym1,sym2,sym3... except that symbols are loaded from an existing file, instead of specified on the command line.

-Bprotected

Assigns protected export class to symbols.

-Bprotected_data

Assigns protected export class to data symbols.

-Bprotected_def

Assigns protected export class to locally defined symbols.

-Bsymbolic

Assigns protected export class to all symbols.

-c

Compiles only, does not link.

-C

Prevents the preprocessor from stripping comments.

-Dname

Defines the preprocessor variable name with a value of "1".

-Dname=def

Defines the preprocessor variable name with a value of def.

-dynamicEnables linking of PIC objects.

-E

Performs preprocessing only with output to stdout.

-e epsym

Sets the default entry point address for output file to the same as the symbol epsym.

-exec

Indicates that object files created will be used to create an executable file.

-ext

Specifying -ext enables HP aC++ extension to the C++ standard.

-fast

Selects a combination of optimization options for optimum execution speed and reasonable build times.

-[no]fpwidetypes

Enables [disables] extended and quad floating point data types.

-g

Inserts information for the symbolic debugger in the object file.

-g0

Causes the compiler to generate complete debug information for the debugger.

-g1

Causes the debugger to generate minimal information for the debugger.

-G

Inserts information required by the gprof profiler in the object file.

-ipo

Enables interprocedural optimizations across files.

-Idir

Inserts dir in the include file search path.

-lname

Causes the linker to search one of the default libraries to resolve unresolved external references.

-lx

Links with the /lib/libx.a and /usr/lib/libx.a libraries.

-Ldir

Links the libraries in dir before the libraries in the default search path.

-minsharedIndicates that the result of the current compilation is going into an executable file that will make minimal use of shared libraries. This option is equivalent to -exec -Bprotected.

-mt

Enables multi-threading capability with the need to set flags.

-n

Generates shareable code.

-N

Generates unshareable code.

-o outfile

Places object modules in outfile file.

-O

Optimizes at level 2.

-p

Inserts information required by the prof profiler in the object file.

-P

Performs preprocessing only with output to the corresponding .i file.

-q

Marks the executable as demand loadable.

-Q

Marks the executable as not being demand loadable.

-r

Retains relocation information in the output file for subsequent relinking.

-s

Strips the symbol table from the executable file.

-S

Generates an assembly language source file.

-t x,name

Substitutes or inserts subprocess x with name.

-Uname

Undefines name in the preprocessor.

-v

Enables verbose mode.

-V

Causes subprocesses to print version information to stderr.

-w

Suppresses warning messages.

-Wx, arg1 [,arg2,..,argn]

Passes the arguments arg1 through argn to the subprocess x.

-Wc,-ansi_for_scope [on|off]

Enables or disables the standard scoping rules for init declarations in for statements.

-Wc,-koenig_lookup,[on|off]

Enables or disables standard argument-dependent lookup rules.

-Y

Enables Native Language Support (NLS).

-z

Disallows runtime dereferencing of null pointers.

-Z

Allows dereferencing of null pointers at runtime.

+cond_rodata

Allows more data to be placed in a read-only section.

+d

Prevents expansion of inline functions.

+DD32Generates ILP32 code and is the default.
+DD64Generates LP64 code.

+DSmodel

Performs instruction scheduling for a specific implementation of Itanium®-based architecture.

+DOosnameSets the target operating system for the compiler.

+dryrun

Generates subprocesses information for a given command line without running the subprocesses.

+e

Enables the following HP value added features while compiling in ANSI C mode: sized enum, long long, long pointers, compiler supplied defaults for missing arguments to intrinsic calls, and $ in identifier HP C extensions.

+ES[no]lit

Places [does not place] string literals and const-qualified variables that do not require load-time or runtime initialization in the read-only data section.

+FPflags

Controls floating-point traps.

+help

Launches a web browser displaying an html version of the HP C/HP-UX Online Help.

+ild

Specifies incremental linking.

+ildrelink

Performs an initial incremental link, regardless of the output load module.

+inline_level num

Controls how C++ inlining hints influence HP aC++.

+inst_compiletime

Causes the compiler to use the compile time (CTTI) instantiation.

+inst_directed

Indicates to the compiler that no templates are to be instantiated (except explicit instantiations).

+inst_implicit_include

Specifies that the compiler use a process similar to the cfront source rule for locating template definition files.

+inst_include_suffixes

Specifies the file name extensions that the compiler uses to locate definition files.

+legacy_cpp

Enables the use of the HP-UX 11.20 ANSI C preprocessor.

+legacy_v5

Enables the use of the A.05.* compiler.

+M

Provides ANSI migration warnings that explain the differences between code compiled with -Ac and -Aa.

+M1Provides platform migration warnings for features that may not be supported in future releases.
+M2Provides migration warnings for transitioning code from the ILP32 to the LP64 data model.

+m[d]

Directs a list of quote enclosed header files to stdout.

+M[d]

Directs a list of both quote enclosed header files and angle bracket enclosed header files to stdout.

+noeh

Disables exception handling.

+nostl

Eliminates references to standard header files and libraries to allow developers full control over header files and libraries in compilation and linking of their applications.

+[no]nrv

Enables [disables] named value return (NRV) optimization.

+o

Prints hexadecimal code offsets in the source code listing.

+O[no]cross_region_addressing

Enables [disables] the use of cross-region addressing.

+O[no]cxlimitedrange

Enables [disables] the use of the usual formulas for complex arithmetic.

+O[no]datalayout

Enables [disables] profile-driven layout of global and static data items to improve cache memory utilization.

+O[no]dataprefetch

When +Odataprefetch is enabled, the optimizer inserts instructions within innermost loops to explicitly prefetch data from memory into the data cache.

+O[no]failsafe

Enables [disables] failsafe optimization.

+O[no]fenvacess

Provides a means to inform the compiler when a program might access the floating point environment to test flags or run under non-default modes.

+O[no]fltacc

Disables [enables] floating-point optimizations that can result in numerical differences.

+O[no]info

Displays information about the optimization process.

+O[no]initcheck

Initializes [does not initialize] local and non-static variables.

+O[no]inline

Allows [does not allow] inlining of funtions by the optimizer.

+O[no]libmerrno

Enables [disables] support for errno in libm funtions.

+O[no]limit

Enables [disables] optimizations that significantly affect compile time or memory consumption.

+O[no]loop_transform

Transforms [does not transform] eligible loops for improved cache and performance.

+O[no]loop_unroll

Enables [disables] loop unrolling.

+O[no]loop_unroll_jam

Enables [disables] loop unrolling and jamming.

+O[no]openmp

Honors [silently ignores] OpenMP directives.

+O[no]parminit

Enables [disables] automatic initialization to non-NaT function parameters at call sites.

+O[no]preserved_fpregs

Specifies whether the compiler is allowed [not allowed] to make use of the preserved subset of the floating point register file.

+O[no]procelim

Enables [disables] the elimination of dead procedure code and unreferenced data.

+O[no]promote_indirect_calls

Uses profile data from profile-based optimization and other information to determine the most likely target of indirect calls and promotes them to direct calls.

+O[no]ptrs_to_globals

Tells the optimizer whether global variables are accessed [not accessed] through pointers.

+O[no]recovery

Generates [does not generate] recovery code for control speculation.

+O[no]report

Displays [does not display] optimization reports.

+O[no]signedopinters

Treats [does not treat] pointers in Boolean comparisons as signed quantities.

+O[no]sumreduction

Enables [disables] sum reduction optimization.

+O[no]size

While most optimizations reduce code size, the +Osize option suppresses those few optimizations that significantly increase code size. The +Onosize option enables code-expanding optimizations.

+O[no]store_ordering

Preserves [does not preserve] the original program order for stores to memory that is visible to multiple threads.

+O[no]volatile

Treats all global variables as [not] volatile.

+O[no]whole_program_mode

Enables [disables] assertion for files compiled with this option.

+O[0|1|2|3|4]

Specifies the level of optimization.

+Ofast

Selects a combination of optimization options for optimum execution speed and reasonable build times.

+Ofaster

Equivalent to +Ofast with an increased optimization level.

+Ofrequently_called

Calls a list of functions that are frequently called. This option overrides any information in a profile database.

+Oinlinebudget

Controls the compile time budget for the inliner.

+Ointeger_overflow

Ensures that runtime integer arithmetic expressions that arise in certain contexts do not overflow.

+Olevel

Lowers optimization to a specified level for one or more functions.

+OlitPlaces data items that do not require load-time or runtime initialization in a read-only data section.

+Oprefetch_latency

Applies to loops for which the compiler generates data prefetch instructions.

+Orarely_called

Calls a list of functions that are rarely called. This option overrides any information in a profile database.

+Oshortdata

Places objects in the short data area; and references to such data assume it resides in the short data area

+Otype_safety

Controls type-based aliasing assumptions.

+Ounroll_factor

Applies the unroll factor to all loops in the current translation unit.

+Oprofile=collect

Prepares the object code for profile-based optimization data collection.

+Oprofile=use

Performs profile-based optimization.

+[no]objdebug

When used with -g, +objdebug leaves debug information in the object files instead of copying it to the executable file. The object files must be accessible to the HP WDB debugger when debugging.

+p

Disallows all anachronistic constructs.

+profilebucketsize

Enables support for prof and gprof when building an executable, but not a shared library.

+sbMake bitfields signed by default in both 32-bit and 64-bit modes.

+[no]srcpos

Controls generation of source position information for HP Caliper.

+time

Generates timing information for compiler subprocesses.

+tls

Specifies whether references to thread local data items are to be performed according to the mode.

+tru64

Causes return types of unprototyped functions to be treated as long, instead of int, matching Tru64 behavior.

+ub

Specifies unqualified char, short, int, long, and long long bit fields as unsigned.

+ucMakes unqualified char data types unsigned.

+w

Warns about all questionable constructs and gives pedantic warnings.

+W arg1[,arg2,...argn]

Suppresses the specified warnings.

+We n1[,n2,...nN]

Changes the specified warnings to errors.

+wn

Specifies the level of warning messages. Warns about all questionable constructs and gives pedantic warnings.

+Ww n1[,n2,...nN]

Enables the specified warnings, assuming all other warnings are suppressed with -w or +w3.

+z

Generates shared library object code (same as +Z in 64-bit mode).

+Z

Generates shared library object code with a large data linkage table (long-form PIC).

 

Examples of Compiler Commands

  • cc -Aa prog.c

    requests a strict ANSI C compilation of prog.c.

  • cc -tp,/users/devel/cpp prog.c

    uses /users/devel/cpp as the pathname for the preprocessing phase.

  • cc -tpca,/users/devel/x prog.s

    uses /users/devel/x/cpp for cpp, /users/devel/x/ctcom for ctcom, and /users/devel/x/as for as; the assembly file prog.s is processed by the specified assembler.

  • cc -Aa prog.c procedure.o -o prog

    compiles and links the file prog.c, creating the executable program file prog. The compiler produces prog.o. The linker ld(1) links prog.o and procedure.o with all of the HP C startup routines and the library routines from the HP C library /usr/lib/hpux32/libc.so.

  • cc prog.c -co /users/my/prog.o

    compiles the source file prog.c and places the object file prog.o in /users/my/prog.o.

  • cc -Wp,-H150000 p1.c p2.c p3.c -o p +legacy_cpp

    compiles the source files in the option -H150000 to the preprocessor cpp to increase the define table size from the default.

  • cc -Wl,-vt *.c -o vmh

    compiles all files in the working directory ending with .c, passes the -vt option to the linker, and causes the resulting program file to be named vmh.

  • cc -vg prog.c

    compiles prog.c, adds debug information, and displays the steps in the compilation process.

  • cc -S prog.c

    compiles the file prog.c into an assembly output file called prog.s.

  • cc -OAa prog.c

    compiles prog.c in ANSI mode and requests level 2 optimization.

  • cc +O1 prog.c

    compiles prog.c and requests level 1 optimization.

  • cc +w1 prog.c -c

    compiles prog.c with low-level warnings emitted and suppresses linking.

  • cc -D BUFFER_SIZE=1024 prog.c

    passes the option -D BUFFER_SIZE=1024 to the preprocessor, setting the value of the macro for the compilation of prog.c.

  • cc prog.c -lm

    compiles prog.c requests the linker to link the library /usr/lib/hpux32/libm.so with the object file prog.o to create the executable a.out.

  • cc -L/users/devel/lib prog.c -lme

    compiles prog.c and causes the linker to search the directory /users/devel/lib for the library libme.a, before searching in /usr/lib/hpux32.

  • cc +DD32 +Z -c prog.c

    compiles prog.c for use in a large shared library in 32-bit mode.

  • cc +DD64 +Z -c prog.c

    compiles prog.c for use in a large shared library or executable in 64-bit mode.

  • cc +DD32 +z -c prog.c

    compiles prog.c in 32-bit mode for use in a shared library with less than 16K of symbols.

  • cc +DD64 prog.c

    compiles prog.c in 64-bit mode.

    Refer to the discussion on optimization options in the C online help or the HP C/HP-UX Programmer’s Guide for more information.

Environment Variables

This section describes the following environment variables you can use to control the C compiler:

CCOPTS Environment Variable

You can pass arguments to the compiler using the CCOPTS environment variable or include them on the command line. The CCOPTS environment variable provides a convenient way for establishing default values for cc command line options. It also provides a way for overriding cc command line options.

The syntax for the CCOPTS environment variable in C shell notation is:

export CCOPTS="options | options" (ksh/sh notation)

setenv CCOPTS "options | options" (csh notation)

The compiler places the arguments that appear before the vertical bar in front of the command line arguments to cc. It then places the second group of arguments after any command line arguments to cc.

Options that appear after the vertical bar in the CCOPTS variable override and take precedence over options supplied on the cc command line.

If the vertical bar is omitted, the compiler gets the value of CCOPTS and places its contents before any arguments on the command line.

For example, the following in C shell notation

setenv CCOPTS -v
cc -g prog.c

is equivalent to

cc -v -g prog.c

For example, the following in C shell notation

setenv CCOPTS "-v | +O1"
cc +O2 prog.c

is equivalent to

cc -v +O2 prog.c +O1

In the above example, level 1 optimization is performed, since the +O1 argument appearing after the vertical bar in CCOPTS takes precedence over the cc command line arguments.

CCROOTDIR Environment Variable

The CCROOTDIR environment variable causes aCC to invoke all subprocesses from an alternate aCC directory, rather than from their default directory. The default aCC root directory is /opt/aCC.

Syntax:

export CCROOTDIR=directory   ksh/sh notation
setenv CCROOTDIR directory   csh notation

directory is an aCC root directory where you want the HP aC++ driver to search for subprocesses.

Example:

export CCROOTDIR=/mnt/CXX2.1

In this example, HP aC++ searches the directories under /mnt/CXX2.1 (/mnt/CXX2.1/bin and /mnt/CXX2.1/lbin) for subprocesses rather than their respective default directories.

TMPDIR Environment Variable

Another environment variable, TMPDIR, allows you to change the location of temporary files that the compiler creates and uses. The directory specified in TMPDIR replaces /var/tmp as the default directory for temporary files. The syntax for TMPDIR in C shell notation is:

export TMPDIR=directory (ksh/sh notation)

setenv TMPDIR directory (csh notation)

directory is the name of an HP-UX directory where you want HP C to put temporary files during compilation.

aCC_MAXERR Environment Variable

This release of HP C compiler provides support to specify the maximum number of errors emitted before the compilation aborts. In the earlier versions, the compiler stopped, if it recognized more than 99 errors while compiling. With this release, the maximum number of errors that the compiler emits can be tuned as required by setting the aCC_MAXERR environment variable.

The syntax for aCC_MAXERR in C shell notation is:

export aCC_MAXERR=errors

where errors are the maximum number of errors set.

SDK/XDK

SDK/XDK, helps you in selecting the components, headerfiles, and libraries installed in alternate locations. To enable this support in your compiler, you need to set either one or both of the following environment variables:

  • SDKROOT

  • TARGETROOT

Setting SDKROOT Environment Variable

The SDKROOT environment variable is used as a prefix for all references to tool set components. This environment variables is set by the user while using a non-native development kit or toolset installed at an alternative location. Some of the toolset components are compiler drivers, compiler applications, preprocessor, linker, and object file tools. If the HP C compiler has its tool set installed in /opt/xdk-ia/directory then the command:

export SDKROOT=/opt/xdk-ia

will prefix all references to the HP C compiler tool set components with /opt/xdk-ia. The following details the default tool set components location as specified in the above command and its earlier location before the execution of the command:

Table 9-2 Native and Alternate Toolset Location

Native LocationAlternate Toolset Location
/opt/ansic/bin/cc/opt/xdk-ia/opt/ansic/bin/cc
/opt/ansic/lbin/ecom (for A.06.*)/opt/xdk-ia/opt/ansic/lbin/ecom (for A.06.*)
/opt/ansic/lbin/ctcom (for A.05.*)/opt/xdk-ia/opt/ansic/lbin/ctcom (for A.05.*)
/opt/langtools/lbin/cpp.ansi/opt/xdk-ia/opt/langtools/lbin/cpp.ansi
/opt/langtools/lbin/u2comp/opt/xdk-ia/opt/langtools/lbin/u2comp
/opt/langtools/lbin/be/opt/xdk-ia/opt/langtools/lbin/be

 

Invoking the toolset components will actually result in the invocation of tool set components from the alternate location as specified above.

Setting TARGETROOT Environment Variable

The TARGETROOT environment variable is used as a prefix for all references to target set components. This environment variable is set by the user when using a non-native development kit. Some of the target set components are header files, archive libraries, and shared libraries. If the HP C compiler has its large tool set installed in /opt/xdk-ia/ directory, the command:

export TARGETROOT=opt/xdk-ia

will prefix all references to the target tool set components with /opt/xdk-ia. The following details the default location of the tool set components as specified in the above command and its earlier location before the execution of the command:

Table 9-3 Native and Alternate Toolset Location

Native locationAlternate Toolset Location
/usr/include/opt/xdk-ia/usr/include
/usr/lib/opt/xdk-ia/usr/lib

 

NOTE: Options like -l or -L on the command line will override $TARGETROOT prefixing.
© 2003-2004 Hewlett-Packard Development Company, L.P.