Strong link types (Object Pascal)

From Appmethod Topics
Jump to: navigation, search

Go Up to Object Pascal Compiler Directives (List) Index

Type

Switch

Syntax

{$STRONGLINKTYPES ON} or {$STRONGLINKTYPES OFF}

Default

{$STRONGLINKTYPES OFF}

Scope

Global



Remarks

This directive affects how the final EXE or DLL is linked. To work reliably, the directive should be in the main program or library source (.dpr). It only affects programs (EXEs) and libraries (DLLs), not packages (BPLs).

{$STRONGLINKTYPES ON} links in with strong fixups (rather than weak fixups) all types in the EXE or DLL's root type table. The root type table is the index that lets the RTTI unit map qualified names to types. The smart linker eliminates symbols (including the RTTI associated with types) that only have weak fixups referencing them. If one or more strong fixups references the symbol, then it is included in the final binary, and the weak references do not get set to @SysInit.PtrToNil (internally).

For example:

uses Rtti;
var
   c: TRttiContext;
begin
   Writeln(Length(c.GetTypes));
end.

(Output: 344)

This program prints out the number of public types available, that is, linked into the executable. Public types are those that have a QualifiedName, can be looked up by that name with System.Rtti.TRttiContext.FindType, and for which TRttiType.IsPublicType returns True. The number of types available normally depends on how the code refers to those types, because the smart linker tries to eliminate RTTI associated with types that are not used directly by the source of the program.

But the linker cannot detect indirect references to types, for example by using System.Rtti.TRttiContext.FindType to look up a type by name.

In advanced scenarios, types may never be directly referred to, and instead only looked up by name -- as in deserialization. In these situations, you might want access to all types without having to manually refer to them in the source, forcing them to be linked in. {$STRONGLINKTYPES ON} has this effect:

uses Rtti;
{$STRONGLINKTYPES ON}
var
   c: TRttiContext;
   t: TRttiType;
begin
   Writeln(Length(c.GetTypes));
end.

(Output: 2203)

Because this directive defeats the smart linker to a large degree, it can cause inflation of the executable size, as more RTTI is included in the executable, including code of methods referred to by that RTTI, and code referred to by those methods, and so forth.