www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - single class in file

reply "Jan Hanselaer" <jan.hanselaer gmail.com> writes:
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).
I don't need a main in this module, but without it I can't compile the file. 
Whu is this? And what's the right way to do it?
I can't just put all my code in one file. Well I could ... but I don't want 
to.

file :  test.d

class Test
{
      char[] name;
      int nr;

      this()
      {
         name = "test";
         nr = 1;
      }
}

void main(){} 
May 05 2007
next sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
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).
 I don't need a main in this module, but without it I can't compile the file. 
 Whu is this? And what's the right way to do it?
 I can't just put all my code in one file. Well I could ... but I don't want 
 to.
 
 file :  test.d
 
 class Test
 {
       char[] name;
       int nr;
 
       this()
       {
          name = "test";
          nr = 1;
       }
 }
 
 void main(){} 
Use 'dmd -c' if you want to compile only with no linking. Use bud or rebuild if you want to make your life easy. --bb
May 05 2007
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
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
May 05 2007
parent "Jan Hanselaer" <jan.hanselaer gmail.com> writes:
"Derek Parnell" <derek psych.ward> schreef in bericht 
news:1vzymj9lmr9rr.1m84406w2ct0f$.dlg 40tude.net...
 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
That's the information I needed. Thanks a lot!
May 05 2007