digitalmars.D.learn - Import function from module
- LW (3/3) Oct 14 2006 How do I use a function from another module in my main file without
- Mike Parker (18/22) Oct 14 2006 You can't. If DMD doesn't compile and link the second file, then how are...
- John Demme (10/13) Oct 14 2006 dmd -c file1.d
- Hasan Aljudy (10/14) Oct 14 2006 Technically speaking, that's not possible.
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?
Oct 14 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.
Oct 14 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?dmd -c file1.d dmd -c file2.d gcc file1.o file2.o -o myapp If gcc is not your linker, the last line will be different. You have to compile each file, however- there's no getting around that. -- ~John Demme me teqdruid.com http://www.teqdruid.com/
Oct 14 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?Technically speaking, that's not possible. But in reality, you can use the "dbuild" tool, which will hide these details from you; it reads import statements and figures out module dependencies. Just pass it the module (file) that contains the main function, and use the -clean switch to delete .obj files after the build. http://www.dsource.org/projects/build (p.s. it's actually called "build", not "dbuild"; at least not officially yet)
Oct 14 2006
My problem were only these long command-line arguments, or - if I used make - additional files. And I'm using Ultra-Edit with external tools, so it's better if the source files contain information about additional source files. Thanks, I've already read of this tool, but first I denied it. Well now I will try it.
Oct 15 2006
Well, I believe it shouldn't be possible to use those function in other modules without linking them, but with the command-line arguments "-inline -O" it works, but I have to use them BOTH. I think this is actually an error, because it seems to depend on the function-size and therefore isn't predictable
Oct 21 2006