Import function from module

Mike Parker aldacron71 at yahoo.com
Sat Oct 14 14:57:42 PDT 2006


LW wrote:
> How do I use a function from another module in my main file without
> generating a library-file or using the second file as a parameter for
> dmd.exe?
> 

You can't. If DMD doesn't compile and link the second file, then how are 
you going to use the function? You have to understand that D is a 
statically compiled language. Each module is compiled into an object 
file and then linked into the executable. A library is just an archive 
of object files (precompiled modules) that you don't have to recompile 
each time. So you have to either pass the second module on the command 
line or precompile it as an object or library file.

FYI, you can create a makefile and use the make utility (which ships 
with DMC, a prerequisite for DMD so you have it already) to build your 
project. You can add each module to the make file, define rules, invoke 
make, and it will build the project. But makefiles can be complex and 
bothersome. Fortunately, there is a tool that will pass all imported 
modules to DMD automatically, called Build (which you can find at 
http://www.dsource.org/projects/build). Pass only your main module to 
build and it will make sure that all imported modules are compiled and 
linked: build mymodule.d. Very simple. No complex makefiles or long 
command lines.



More information about the Digitalmars-d-learn mailing list