HomeDelphiDelphi Error – E2221 $WEAKPACKAGEUNIT ‘%s’ cannot have initialization or finalization code

Delphi Error – E2221 $WEAKPACKAGEUNIT ‘%s’ cannot have initialization or finalization code

Delphi Compiler Error

E2221 $WEAKPACKAGEUNIT ‘%s’ cannot have initialization or finalization code

Reason for the Error & Solution

A unit which has been flagged with the $weakpackageunit directive cannot contain initialization or finalization code, nor can it contain global data. The reason for this is that multiple copies of the same weakly packaged units can appear in an application, and then referring to the data for that unit becomes and ambiguous proposition. This ambiguity is furthered when dynamically loaded packages are used in your applications.

(*$WEAKPACKAGEUNIT*)
unit yamadama;
interface
implementation
  var
    Title : String;

initialization
  Title := 'Tiny Calc';
finalization
end.

In the above example, there are two problems: Title is a global variable, and Title is initialized in the initialization section of the unit.

There are only two alternatives: either remove the $weakpackageunit directive from the unit, or remove all global data, initialization and finalization code.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...