How to compile and use an external class

Lars Kyllingstad public at kyllingen.NOSPAMnet
Tue Dec 2 01:48:42 PST 2008


Some Guy wrote:
> I am busy learning D and I want to be able to use classes now. I am
> used to programming in C# where it does everything for you and you
> don't have to think about this kind of thing so I now have no idea
> how to compile a program from the command line that uses a class in
> another source file.
> 
> I have a source file called MyProgram.d which contains the main
> program and I have a file called MyClass.d that contains a class
> called MyClass. I want to be able to create an instance of MyClass in
> the main program but I don't know what to type to compile it the
> command line. This is what I have tried so far which doesn't work:
> 
> dmd MyProgram.d MyClass.d
> 
> Thanks in advance for your help.

What error message are you getting? Did you remember to put module and 
import statements in your source files?

MyClass.d:

     module MyClass;

     class MyClass
     {
        ...
     }

MyProgram.d:

     module MyProgram;
     import MyClass;

     void main()
     {
         MyClass m = new MyClass;    // Example
         ...
     }

     ...

(The module statement in MyProgram.d isn't strictly necessary in this 
case, but personally I think it's good form to always declare the name 
of a module.)

-Lars


More information about the Digitalmars-d-learn mailing list