1

I just can't seem to link a procedure from another unit to work in the main unit's form. I tried adding the procedure declaration below interface, as mentioned in this question How to run procedure from another unit? , but it didn't work. It keeps showing [DCC Error] Main.pas(27): E2003 Undeclared identifier: 'sayHi' Here are the codes for both units: Main.pas:

unit Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Unit2;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
sayHi();
end;

end.

and Unit2.pas

unit Unit2;

interface uses Dialogs;

procedure sayHi();

implementation

procedure sayHi();
begin
  ShowMessage('hi');
end;

end.

Here's the dpr file for the project:

program gl;

uses
  Vcl.Forms,
  Main in 'Main.pas' {Form1},
  Unit2 in 'Unit2.pas';

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Here is the main.dfm file:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 444
  ClientWidth = 621
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
end
Community
  • 1
  • 1
Andris Gauračs
  • 398
  • 6
  • 20
  • The code in the question is fine – David Heffernan Apr 06 '14 at 17:29
  • 1
    So then what else could be the problem? – Andris Gauračs Apr 06 '14 at 17:31
  • 2
    You aren't reproducing what is really going on – David Heffernan Apr 06 '14 at 17:33
  • Is there something you aren't telling us? One wonders at the tags you've used.. – Tony Hopkinson Apr 06 '14 at 17:33
  • I added the dpr and the dfm codes also. I'm sorry, I am really lost here - how can this error come up when compiling if the code is correct. Could you guys at least give me a direction in where to search for the problem? – Andris Gauračs Apr 06 '14 at 17:44
  • 1
    It may be that the compiler is finding another Unit2 - that doesn't declare sayHi() - somewhere on its search paths before the one that does. Try this: edit your Unit2 to include a ! character before the words "unit Unit2". Save and try to compile. Does the compiler still produce the same error as before, or does it complain about the "!" character? – MartynA Apr 06 '14 at 18:04
  • If you started from scratch, this would work – David Heffernan Apr 06 '14 at 19:00
  • 1
    I highly doubt the tag `stored-procedures` is related to your problem at all. The `linker` tag too. – Jerry Dodge Apr 06 '14 at 20:04
  • I'm having the same often, even in the latest 10.2 Tokyo; the weirdest thing is if I use Alt+Up in the "undeclared identifier" function it actually goes to that function in the linked unit, and if I do it in a new project it works fine. It makes no sense at all, Delphi is a bugfest and they keep on releasing new versions carrying over all the trash from years. – hikari May 04 '17 at 01:41

2 Answers2

9

I have seen this before, and it's always related to having a different version "Unit2" being found first.

You have more than one Unit2.dcu or pas on your machine.
The Unit2 that is being found without "SayHi" is being found first.

Please check your project and Delphi Global library paths.

Robert Love
  • 12,447
  • 2
  • 48
  • 80
0

This can also happen if you have forgot to define the procedure in the interface section at the top of the page.

SamAct
  • 529
  • 4
  • 23