Accessing Delphi method pointers from another unit in another package when compiling with 'dcc32 -JL'

146 Views Asked by At

This is a bit complicated scenario - and even more complicated in practice, but I managed to minimize it for this question.

I have defined two Delphi packages: BasePackage.dpk and AnotherPackage.dpk

package BasePackage;

requires
  rtl;

contains
  Base in 'Base.pas';

end.

and

package AnotherPackage;

requires
  BasePackage;

contains
  Another in 'Another.pas';

end.

and units

unit Base;

interface

type
  TMethod = procedure;

  TEncodeableType = record
    Method: TMethod;
  end;
  PEncodeableType = ^TEncodeableType;

procedure Method_Impl;

const
  TestMethod_Type: TEncodeableType = (
    Method: Method_Impl;
  );

implementation

procedure Method_Impl;
begin
end;

end.

and

unit Another;

interface

uses
  Base;

implementation

const
  ResponseType: PEncodeableType = @TestMethod_Type;

end.

Everything compiles fine basically, and I can even generate the basic C++Builder units with 'dcc32 -JPHNEK --BCB' but when I try to generate the library files for C++Builder using 'dcc32 -JPHNEK --BCB -JL' I get

>dcc32  -JPHNEK --BCB -JL BasePackage.dpk
Embarcadero Delphi for Win32 compiler version 32.0
Copyright (c) 1983,2017 Embarcadero Technologies, Inc.
BasePackage.dpk(10)
11 lines, 0.09 seconds, 3688 bytes code, 176 bytes data.
>dcc32  -JPHNEK --BCB -JL AnotherPackage.dpk
Embarcadero Delphi for Win32 compiler version 32.0
Copyright (c) 1983,2017 Embarcadero Technologies, Inc.
AnotherPackage.dpk(10)
Error: E2201 Need imported data reference ($G) to access 'TestMethod_Type' from unit 'Another'

I have tried to play with different compile options (and of course introduce {$G+} or {$IMPORTEDDATA ON} to the units and packages (although it's the default anyway), but haven't seen them make any difference.

The packages are compiled without any or with these options in .cfg:

-$A+
-$B-
-$C-
-$D-
-$E-
-$F-
-$G+
-$H+
-$I-
-$J-
-$K-
-$L+
-$M-
-$N+
-$O+
-$P+
-$Q-
-$R-
-$S-
-$T+
-$U-
-$V-
-$W-
-$X+
-$Y-
-$Z1
-cg
-AWinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE;
-H+
-W+
-M
-$M16384,1048576
-K$00300000
-Z

and either without any or with these options in .dpk:

{$ALIGN 8}
{$ASSERTIONS ON}
{$BOOLEVAL OFF}
{$DEBUGINFO ON}
{$EXTENDEDSYNTAX ON}
{$IMPORTEDDATA ON}
{$IOCHECKS ON}
{$LOCALSYMBOLS ON}
{$LONGSTRINGS ON}
{$OPENSTRINGS ON}
{$OPTIMIZATION ON}
{$OVERFLOWCHECKS ON}
{$RANGECHECKS ON}
{$REFERENCEINFO OFF}
{$SAFEDIVIDE OFF}
{$STACKFRAMES OFF}
{$TYPEDADDRESS OFF}
{$VARSTRINGCHECKS ON}
{$WRITEABLECONST ON}
{$MINENUMSIZE 1}
{$IMAGEBASE $300000}
{$DESIGNONLY}
{$IMPLICITBUILD OFF} 

Any idea if I could make this work (I really need the functionalities in different packages)?

I have been compiling with Delphi 10.2 Tokyo and XE4 with the same result.

1

There are 1 best solutions below

0
On

I believe this is a compiler issue and therefore it might be impossible to solve for my case exactly like this. But, I can declare the mapping with variables, instead of constants (in practice I have several of these inside structures).

So, this version of Another.pas compiles and works:

unit Another;

interface

uses
  Base;

implementation

var
  ResponseType: PEncodeableType;

initialization
  ResponseType := @TestMethod_Type;

end.