3 Pragma Directives and Attributes

A pragma directive is an instruction to the compiler. You use a #pragma directive to control the actions of the compiler in a particular portion of a translation unit without affecting the translation unit as a whole.

Put pragmas in your C++ source code where you want them to take effect. Unless otherwise specified, a pragma is in effect from the point where it is included until the end of the translation unit or until another pragma changes its status.

This chapter discusses the following pragma directives:

“Initialization and Termination Pragmas” (page 96)

“Copyright Notice and Identification Pragmas” (page 97)

“Data Alignment Pragmas” (page 98)

“Optimization Pragmas” (page 103)

“Diagnostic Pragmas” (page 105)

“Other Pragmas” (page 105)

“OpenMP Clauses” (page 114)

“Attributes” (page 116)

Initialization and Termination Pragmas

This section describes the INIT and FINI pragmas. These pragmas allow the user to set up functions which are called when a load module (a shared library or executable) is loaded (initializer) or unloaded (terminator).

For example, when a program begins execution, its initializers get called before any other user code gets called. This allows some set up work to take place. In addition, when the user’s program ends, the terminators can do some clean up. When a shared library is loaded or unloaded, its initializers and terminators are also executed at the appropriate time.

INIT

#pragma INIT “string

Use #pragma INIT to specify an initialization function. The function takes no arguments and returns nothing. The function specified by the INIT pragma is called before the program starts or when a shared library is loaded. For example,

#pragma INIT "my_init"

void my_init() { ... do some initializations ... }

FINI

#pragma FINI “string

Use #pragma FINI to specify a termination function. The function specified by the FINI pragma is called after the program terminates by either calling the libc exit function, returning from the main or _start functions, or when the shared library, which contains the FINI is unloaded from memory. Like the function called by the INIT pragma, the termination function takes no arguments and returns nothing. For example,

#pragma FINI "my_fini"

void my_fini() { ... do some clean up ... }

96 Pragma Directives and Attributes

Page 96
Image 96
HP C/aC++ for PA-RISC Software manual Pragma Directives and Attributes, Initialization and Termination Pragmas