www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Code reuse?

reply Mike <Mike_member pathlink.com> writes:
Could someone tell me how i can write a class in D, compile it and then re-use
the compiled code in other D projects. 

I am familiar with accomplishing this in Java by using the .class files that are
created when any class is compiled. How is this most easily done in the D
language?

thanks in advance
Nov 08 2004
next sibling parent clayasaurus <clayasaurus gmail.com> writes:
Mike wrote:
 Could someone tell me how i can write a class in D, compile it and then re-use
 the compiled code in other D projects. 
 
 I am familiar with accomplishing this in Java by using the .class files that
are
 created when any class is compiled. How is this most easily done in the D
 language?
 
 thanks in advance
 
 

I've never used java so I'm not quite sure I understand. Class files can be compiled natively. D is not like java that compiled code will work on every platform, though you probably know that. 'dmd -c class.d' will produce 'class.o' which can be linked to your project using 'gcc -o project class.o' on linux and 'dmc -o project class.o' on windows. You can also try out makefiles or makefile alternatives like dmake (http://www.prowiki.org/wiki4d/wiki.cgi?Dmake) which will only compile the code you changed.
Nov 08 2004
prev sibling next sibling parent Mike Parker <aldacron71 yahoo.com> writes:
Mike wrote:
 Could someone tell me how i can write a class in D, compile it and then re-use
 the compiled code in other D projects. 
 
 I am familiar with accomplishing this in Java by using the .class files that
are
 created when any class is compiled. How is this most easily done in the D
 language?

D is not like Java in this regard. Each D source file is compiled into object files. These can be linked directly to your application when you compile it, or can be archived into a library file which can also be linked to when you compile. It is the in C and C++. Furthermore, the source modules need to be on the import path. You can specifiy the import path on the command line or via dmd's configuration file. This also means you can forgo the above steps and just compile the source modules into the application rather than linking to precompiled objects. It may help to get out of thinking of the 'one class per file' and 'everything is an object' Java mentality. A D source module need not contain any classes at all, and may very well contain several. And also remember that D is statically compiled, so there is no runtime dynamic class loading going on.
Nov 09 2004
prev sibling parent Ilya Minkov <minkov cs.tum.edu> writes:
Apart from what others wrote, i suggest you take a look at the module 
std.loader which is included in Phobos. As many other newer Phobos 
modules, it is not documented in the manual but only in its source, so 
if you have trouble figurung stuff out, just ask.

-eye


Mike schrieb:

 Could someone tell me how i can write a class in D, compile it and then re-use
 the compiled code in other D projects. 
 
 I am familiar with accomplishing this in Java by using the .class files that
are
 created when any class is compiled. How is this most easily done in the D
 language?
 
 thanks in advance
 
 

Nov 09 2004