C++ Programming Guide

Pragma Reference

#pragma align

#pragma align integer(variable[,variable]...)

Use align to make the listed variables memory-aligned to integer bytes, overriding the default. The following limitations apply:

#pragma init

#pragma init(identifier [ , identifier ] ...)

Use init to mark identifier as an initialization function. Such functions are expected to be of type void, to accept no arguments, and to be called while constructing the memory image of the program at the start of execution. Initializers in a shared object are executed during the operation that brings the shared object into memory, either at program start up or during some dynamic loading operation, such as dlopen(). The only ordering of calls to initialization functions is the order in which they are processed by the link editors, both static and dynamic.

Within a source file, the functions specified in #pragma init are executed after the static constructors in that file. You must declare the identifiers before using them in the pragma.

#pragma fini

#pragma fini (identifier [, identifier]...)

Use fini to mark identifier as a finalization function. Such functions are expected to be of type void, to accept no arguments, and to be called either when a program terminates under program control or when the containing shared object is removed from memory. As with initialization functions, finalization functions are executed in the order processed by the link editor.

In a source file, the functions specified in #pragma fini are executed after the static destructors in that file. You must declare the identifiers before using them in the pragma.

#pragma ident

#pragma ident string

Use ident to place string in the .comment section of the executable.

#pragma pack(n)

#pragma pack([n])

Use pack to affect the packing of structure members.

If present, n must be 0 or a power of 2. A value of other than 0 instructs the compiler to use the smaller of n-byte alignment and the platform's natural alignment for the data type. For example, the following directive causes the members of all structures defined after the directive (and before subsequent pack directives) to be aligned no more strictly than on 2-byte boundaries, even if the normal alignment would be on 4- or 8-byte boundaries.


#pragma pack(2)

When n is 0 or omitted, the member alignment reverts to the natural alignment values.

If the value of n is the same as or greater than the strictest alignment on the platform, the directive has the effect of natural alignment.

Table 3-1 Strictest Alignment by Platform

Platform 

Strictest Alignment 

x86 

SPARC generic, v7, v8, v8a, v8plus, v8plusa 

SPARC v9, v9a 

16 

A pack directive applies to all structure definitions which follow it, until the next pack directive. If the same structure is defined in different translation units with different packing, your program may fail in unpredictable ways. In particular, you should not use a pack directive prior to including a header defining the interface of a precompiled library. The recommended usage is to place the pack directive in your program code, immediately before the structure to be packed, and to place #pragma pack() immediately after the structure.

#pragma unknown_control_flow

#pragma unknown_control_flow (name, [,name] ...)

Use unknown_control_flow to specify a list of routines that violate the usual control flow properties of procedure calls. For example, the statement following a call to setjmp() can be reached from an arbitrary call to any other routine. The statement is reached by a call to longjmp().

Because such routines render standard flowgraph analysis invalid, routines that call them cannot be safely optimized; hence, they are compiled with the optimizer disabled.

#pragma weak

#pragma weak function-name1 [= function-name2]

Use weak to define a weak global symbol. This pragma is used mainly in source files for building libraries. The linker does not warn you if it cannot resolve a weak symbol.

The following directive defines bar to be a weak symbol. No error messages are generated if the linker cannot find a definition for a function named bar.


#pragma weak bar

The following directive instructs the linker to resolves any references to bar to bar if it is defined anywhere in the program, and to foo otherwise.


#pragma weak bar = foo

You must declare a function before you use it in a weak pragma. For example:


extern void bar(int);
extern void _bar(int);
#pragma weak _bar=bar

The effects of using #pragma weak are:

See the Solaris Linker and Libraries Guide for more information.


Note -

The names in the pragma must be the names a seen by the linker, which means the "mangled" name if the function has C++ linkage.