RAD Studio (Common)
ContentsIndex
PreviousUpNext
Weak packaging
Type  
Switch  
Syntax  
{$WEAKPACKAGEUNIT ON} or {$WEAKPACKAGEUNIT OFF}  
Default  
{$WEAKPACKAGEUNIT OFF}  
Scope  
Local  

Remarks 

The $WEAKPACKAGEUNIT directive affects the way a .dcu file is stored in a Delphi package's .dcp and .bpl files on the Win32 platform, or, analogously, how a .dcuil file is stored in the package's .dcpil and .dll files on the .NET platform. If {$WEAKPACKAGEUNIT ON} appears in a unit file, the compiler omits the unit from bpls or dlls 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 a package called PACK contains only one unit, UNIT1. Suppose UNIT1 does not use any further units, but it makes calls to RARE.DLL. If the {$WEAKPACKAGEUNIT ON} directive is inserted in UNIT1.pas before compiling, UNIT1 will not be included in PACK.BPL (or PACK.DLL on .NET); copies of RARE.DLL will not have to be distributed with PACK. However, UNIT1 will still be included in PACK.dcp (or PACK.dcpil on .NET). If UNIT1 is referenced by another package or application that uses PACK, it will be copied from PACK.dcp (or PACK.dcpil on .NET) and compiled directly into the project. 

Now suppose a second unit, UNIT2, is added to PACK. Suppose that UNIT2 uses UNIT1. This time, even if PACK is compiled with {$WEAKPACKAGEUNIT ON} in UNIT1.pas, the compiler will include UNIT1 in PACK.BPL (or PACK.DLL on .NET). But other packages or applications that reference UNIT1 will use the (non-packaged) copy taken from PACK.dcp (or PACK.dcpil on .NET).

Note: Note: Unit files containing the {$WEAKPACKAGEUNIT ON} directive must not have global variables, initialization sections, or finalization sections.
The $WEAKPACKAGEUNIT directive is an advanced feature intended for developers who distribute their packages to other programmers. It can help to avoid distribution of infrequently used DLLs, and to eliminate conflicts among packages that may depend on the same external library. 

For example, Delphi's 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 VCL60 (which encapsulates many commonly-used Delphi components). When you compile a project that uses PenWin and the VCL60 package, PenWin is copied from VCL60.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, VCL60 itself would be statically linked to PENWIN.DLL, and so could not be loaded on any computer which didn't have PENWIN.DLL installed. Second, if someone tried to create a package that contained PenWin, a compiler error would result because the PenWin unit would be contained in both VCL60 and the new package. Thus, without weak packaging, PenWin could not be included in standard distributions of VCL60.

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!