RAD Studio
ContentsIndex
PreviousUpNext
#pragma package

Syntax

#pragma package(smart_init)
#pragma package(smart_init, weak)

Description: smart_init argument 

The #pragma package(smart_init) assures that packaged units are initialized in the order determined by their dependencies. (Included by default in package source file.) Typically, you would use the #pragma package for .cpp files that are built as packages. 

This pragma affects the order of initialization of that compilation unit. For units, initialization occurs in the following order:

  • 1. By their "uses" dependencies, that is, if unitA depends on unitB, unitB must be initialized before unitA.
  • 2. The link order.
  • 3. Priority order within the unit.
For regular object files (those not built as units), initialization happens first according to priority order and then link order. Changing the link order of the object files changes the order in which the global object constructors get called. 

The following examples show how the initialization differs between units and regular object files. 

Take as an example three unit files, A, B and C that are "smart initialized" with #pragma package(smart_init) and have priority values (defined by the priority parameter of the #pragma startup) set of 10, 20 and 30. The functions are named according to their priority value and the parent object file, so the names are a10, a20, a30, b10, and so on. 

Since all three are units, and if A uses B and C and the link order is A, B then C, the order of initialization is: 

B10 B20 B30 C10 C20 C30 A10 A20 A30 

If the above were object files, not units, the order would be: 

A10 B10 C10 A20 B20 C20 A30 B30 C30 

The .cpp files that use #pragma package(smart_init) also require that any #pragma link references to other object files from a .cpp file that declares #pragma package(smart_init), must be resolved by a unit. #pragma link references to non object files can still be resolved by libraries, etc. 

Description: weak packages 

The #pragma package(smart_init, weak) directive affects the way an object file is stored in a package’s .bpi and .bpl files. If #pragma package(smart_init, weak) appears in a unit file, the compiler omits the unit from BPLs when possible, and creates a non-packaged local copy of the unit when it is required by another application or package. A unit compiled with this directive is said to be "weakly packaged". 

#pragma package(smart_init, weak) is used to eliminate conflicts among packages that may depend on the same external library. 

Unit files containing the #pragma package(smart_init, weak) directive must not have global variables. 

For more information about using weak packages, see Weak packaging.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!