tags:

views:

391

answers:

3

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 to do the similar in delphi?

+1  A: 

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)

Marco van de Voort
What do you mean by "name of the project"?I just have a single file which contains the code I pasted.
That would not compile. The above code assumes you have created a project and added a unit to it with a function in it executing this code. I assume you are new to Delphi? Might I suggest using the IDE first? Have a look at http://www.delphibasics.co.uk/Article.asp?Name=FirstPgm
Lars Truijens
Yes,I'm new to delphi.But I don't want to refer to IDE.I want to know the general steps to write,compile and run a delphi programme.
These answers will help you with the compile part. You might want to concentrate now on the write part. Start by learning to write Delphi/ObjectPascal.
Lars Truijens
An IDE will help you learn a new language faster by providing help when designing forms, help, hints, color highlighting, code completion, etc
Lars Truijens
(I don't agree with that. People don't ask themselves the "why" enough in an IDE, and just patch and hope it works. IDE's qualities for normal development are clear, but most modern IDEs IMHO are not really are suitable for education.)
Marco van de Voort
+5  A: 

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.

Lars Truijens
If you have dcc32.exe only without libraries (system, VCL, 3rd-party components used in application etc.) it won't compile even a simple project with TForm. To compile from command line without Delphi installation you need to specify at least path to this libraries.
ThinkJet
A: 

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

vcldeveloper
I'm afraid this will not work either, but it is a good start. An application needs to be created and Application.Run needs to be called or the application will not pump messages and will quit immediately. See the code in a normal VCL application's .dpr file.
Lars Truijens
Yes, but to prevent that, I used ShowModal instead of Show. If we were supposed to use Show method, then we had to add Application.Initialize and Application.Run in the DPR file just like a normal VCL Form Application.
vcldeveloper