Using the Weak Packaging Directive

From RAD Studio
Jump to: navigation, search

Go Up to Compiling Packages


In Delphi:

The $WEAKPACKAGEUNIT directive affects the way a .dcu file is stored in a package's .dcp and .bpl files. (For information about files generated by the compiler, see Packages and Standard DLLs.) If {$WEAKPACKAGEUNIT ON} 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.

For example, suppose you've created a package called pack1 that contains only one unit, unit1. Suppose unit1 does not use any additional units, but it makes calls to rare.dll. If you put the {$WEAKPACKAGEUNIT ON} directive in unit1.pas (Delphi) or unit1.cpp (C++) when you compile your package, unit1 will not be included in pack1.bpl; you will not have to distribute copies of rare.dll with pack1. However, unit1 will still be included in pack1.dcp. If unit1 is referenced by another package or application that uses pack1, it will be copied from pack1.dcp and compiled directly into the project.

Now suppose you add a second unit, unit2, to pack1. Suppose that unit2 uses unit1. This time, even if you compile pack1 with {$WEAKPACKAGEUNIT ON} in unit1.pas, the compiler will include unit1 in pack1.bpl. But other packages or applications that reference unit1 will use the (non-packaged) copy taken from pack1.dcp.

Note: Unit files containing the {$WEAKPACKAGEUNIT ON} directive must not have global variables, initialization sections, or finalization sections.

The {$WEAKPACKAGEUNIT ON} directive is an advanced feature intended for developers who distribute their packages to other programmers. It can help you to avoid distribution of infrequently used DLLs, and to eliminate conflicts among packages that may depend on the same external library.

For example, the PenWin unit references PenWin.dll. Most projects don't use PenWin, and most computers don't have PenWin.dll installed on them. For this reason, the PenWin unit is weakly packaged in vcl. When you compile a project that uses PenWin and the vcl package, PenWin is copied from vcl70.dcp and bound directly into your project; the resulting executable is statically linked to PenWin.dll.

If PenWin were not weakly packaged, two problems would arise. First, vcl itself would be statically linked to PenWin.dll, and so you could not load it on any computer which didn't have PenWin.dll installed. Second, if you tried to create a package that contained PenWin, a compiler error would result because the PenWin unit would be contained in both vcl and your package. Thus, without weak packaging, PenWin could not be included in standard distributions of vcl.

In C++Builder:

The #pragma package(smart_init, weak) directive affects the way an .obj file is stored in a package's .bpi and .bpl files. (For information about files generated by the compiler and linker, see Packages and Standard DLLs.) If #pragma package(smart_init, weak) appears in a unit file, the linker 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.

For example, suppose you've created a package called PACK that contains only one unit, UNIT1. Suppose UNIT1 does not use any additional units, but it makes calls to RARE.dll. If you put #pragma package(smart_init, weak) in UNIT1.cpp when you build your package, UNIT1 will not be included in PACK.bpl; you will not have to distribute copies of RARE.dll with PACK. However, UNIT1 will still be included in PACK.bpi. If UNIT1 is referenced by another package or application that uses PACK, it will be copied from PACK.bpi and linked directly into the project.

Now suppose you add a second unit, UNIT2, to PACK. Suppose that UNIT2 uses UNIT1. This time, even if you compile PACK with #pragma package(smart_init, weak) in UNIT1.cpp, the linker will include UNIT1 in PACK.bpl. But other packages or applications that reference UNIT1 will use the (non-packaged) copy taken from PACK.bpi.

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

#pragma package(smart_init, weak) is an advanced feature intended for developers who distribute their bpls to other C++Builder programmers. It can help you to avoid distribution of infrequently used DLLs, and to eliminate conflicts among packages that may depend on the same external library.

For example, the PenWin unit references PenWin.dll. Most projects do not use PenWin, and most computers do not have PenWin.dll installed on them. For this reason, the PenWin unit is weakly packaged in vcl. When you link a project that uses PenWin and the vcl package, PenWin is copied from vcl60.bpi and bound directly into your project; the resulting executable is statically linked to PenWin.dll.

If PenWin were not weakly packaged, two problems would arise. First, vcl itself would be statically linked to PenWin.dll, and so you could not load it on any computer that did not have PenWin.dll installed. Second, if you tried to create a package that contained PenWin, a build error would result because the PenWin unit would be contained in both vcl and your package. Thus, without weak packaging, PenWin could not be included in standard distributions of vcl.

See Also