single class in file

Derek Parnell derek at psych.ward
Sat May 5 14:17:16 PDT 2007


On Sat, 5 May 2007 22:11:03 +0200, Jan Hanselaer wrote:

> Hi
> 
> I'm trying to make a class in a module. A bit inspired by java where one 
> class is one object.
> Altough I tought this would work in D too.
> The code here works. But I just want the class Test in that file. And then I 
> want to use it in my main program (in another file).

Ok, here is one way you can do that ...

 // file :  test.d

  class Test
 {
       char[] name;
       int nr;
 
       this()
       {
          name = "test";
          nr = 1;
       }
 }
 // ------- EOF --------

 // file: app.d
 import test;
 void main(){}
 // ------- EOF --------


Then at the command line ...

 dmd app test

Or if you want to do it in steps ...

 dmd test -c
 dmd app test.obj

Or use one of the 'build' tools (eg. rebuild or bud) ...

 bud app

The D compiler needs to have a 'main' routine to be able to link the file
together. If you want to compile a file that does not have a main routine,
you need to use the "-c" -switch, which stands for 'compile only; no
linking'. And once you have all your files separately compiled, you need to
link the application, so you supply the source file that contains the
'main' routine plus all the object files it needs on the command line.


The 'build' tools help automate the compilation steps required.

-- 
Derek Parnell
Melbourne, Australia
"Justice for David Hicks!"
skype: derek.j.parnell


More information about the Digitalmars-d-learn mailing list