www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Calling functions from other files/modules

reply Namal <sotis22 mail.ru> writes:
Hello,

finally I want to learn how to do it right and I tried to 
understand it from here

https://en.wikibooks.org/wiki/D_(The_Programming_Language)/d2/Modules

But I have a few questions. Do I have always to include std.stdio 
in every file like in the example or is it enough just to import 
a module which has this already included?

Why do I need to define my main as the main module? I can't 
imagine that I can import it somewhere else.

Is there something like #pragma once that needs to be done?
Jan 06 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 6 January 2016 at 22:06:32 UTC, Namal wrote:
 Do I have always to include std.stdio in every file like in the 
 example or is it enough just to import a module which has this 
 already included?
In every file that you use it, yes. Module imports are private to the module (well, unless you specifically mark them as public) which is different than C includes.
 Why do I need to define my main as the main module? I can't 
 imagine that I can import it somewhere else.
You can import it as long as you define it!
 Is there something like #pragma once that needs to be done?
no need
Jan 06 2016
parent reply Namal <sotis22 mail.ru> writes:
On Wednesday, 6 January 2016 at 22:15:43 UTC, Adam D. Ruppe wrote:

 You can import it as long as you define it!
I just tried to import one module with a main into another, but I get this: Error: only one main allowed
Jan 06 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 6 January 2016 at 23:00:43 UTC, Namal wrote:
 I just tried to import one module with a main into another, but 
 I get this:
You can't have two mains, but you can import a module with main from another module without one.
Jan 06 2016
parent reply Namal <sotis22 mail.ru> writes:
On Wednesday, 6 January 2016 at 23:06:38 UTC, Adam D. Ruppe wrote:
 On Wednesday, 6 January 2016 at 23:00:43 UTC, Namal wrote:
 I just tried to import one module with a main into another, 
 but I get this:
You can't have two mains, but you can import a module with main from another module without one.
How can I produce a program file from that module which has no main but uses some functions from main module?
Jan 06 2016
parent tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
On Wednesday, 6 January 2016 at 23:12:27 UTC, Namal wrote:
 On Wednesday, 6 January 2016 at 23:06:38 UTC, Adam D. Ruppe 
 wrote:
 On Wednesday, 6 January 2016 at 23:00:43 UTC, Namal wrote:
 I just tried to import one module with a main into another, 
 but I get this:
You can't have two mains, but you can import a module with main from another module without one.
How can I produce a program file from that module which has no main but uses some functions from main module?
I think the below example clarifies everything. entry.d ======= module project.entry; import std.stdio; import project.other; void bark(){ writeln("Woof"); } void main(){ project.other.callbark(); } other.d ======= module project.other; import project.entry; void callbark(){ project.entry.bark(); }
Jan 06 2016