Weak packaging

From RAD Studio
Jump to: navigation, search

Go Up to Delphi Compiler Directives (List) Index










The $WEAKPACKAGEUNIT directive affects the way a .dcu file is stored in a Delphi package's .dcp and .bpl files on the Win32 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; copies of RARE.DLL will not have to be distributed with PACK. However, UNIT1 will still be included in PACK.dcp. If UNIT1 is referenced by another package or application that uses PACK, it will be copied from PACK.dcp 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. But other packages or applications that reference UNIT1 will use the (non-packaged) copy taken from PACK.dcp.

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.

See Also