Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile and run this Delphi code without installing an IDE?

Tags:

delphi

It's say to generate a winform:

var
  F : TForm;
  L : TLabel;
begin
  F := TForm.Create(Application);
  L := TLabel.Create(F);
  L.Parent := F; // Needed to have it show up on the form.
  L.Left := 50;
  L.Top := 50;
  L.Caption := 'This is an example';
  F.Show;
end;

In fact it's from my previous question.

If it's C programme, I can run it this way:

> gcc foo.c -o foo.exe
> foo.exe

How can I do it similarly in Delphi?

like image 342
user198729 Avatar asked Dec 10 '22 18:12

user198729


2 Answers

In order to compile Delphi code you would need a compiler. There are no free versions available of Delphi anymore so unless you can find an old one you would have to buy Delphi. Delphi comes with a commandline compiler like gcc and can compile programs without an IDE.

Delphi 2006 and before win32:

dcc32 YourProject.dpr

Delphi 2006 and before .Net:

dccil YourProject.dpr

Delphi 2007 and after:

msbuild YourProject.dproj

This will result in a compiled binary and in case of an EXE you can run it like you are used to.

There are free alternatives to Delphi like FreePascal and their free IDE Lazarus. I haven't checked for myself but I am pretty sure it comes with a commandline compiler as well.

like image 173
Lars Truijens Avatar answered Dec 24 '22 00:12

Lars Truijens


You should write this code in a DPR file. The general structure of a DPR file is something like this:

program {ProgramName};

uses {List of used units};

begin
  {Your code}
end.

So for the above code your DPR file will be like this:

program Project1;

uses
  Forms, StdCtrls;

var
  F : TForm;
  L : TLabel;
begin
  F := TForm.Create(nil);
  try
    L := TLabel.Create(F);
    L.Parent := F; // Needed to have it show up on the form.
    L.Left := 50;
    L.Top := 50;
    L.Caption := 'This is an example';
    F.ShowModal;
  finally
    F.Free;
  end;
end.

You can type this code in a text editor, and save it as Project1.dpr.

Now you can use Delphi's commandline compiler to compile it:

dcc32.exe Project1.dpr

like image 44
vcldeveloper Avatar answered Dec 24 '22 00:12

vcldeveloper