How to compile and run this Delphi code without installing an IDE?
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?
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.
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
Much easier, just
dcc32 "name of the project"
Even if the program exists out of 10 units, the compiler will autoresolve it, if within the current unit searchpath (see dcc32.cfg, or add dirs with -U and -I). Note that some of the "free" Explorer versions of Delphi don't come with a cmdline compiler.
For .NET, it's dccil.
Of course, if you compare with gcc, Free Pascal/Lazarus is a more logical choice (but not for .NET).
链接地址: http://www.djcxy.com/p/59654.html