www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - overloading main

reply NonNull <non-null use.startmail.com> writes:
I am linking to a C project with some C already automatically 
translated into D including the C main function `int main(int 
argc, char** argv){/* ... */}` which I wanted to call from a D 
main function in a new module. But then I found that the former C 
main in D will not compile! ldc2 complains that main must only 
have prototypes as if it is the entry point. So it seems that 
main cannot be overloaded with a prototype that is not a valid 
entry point in D. Is this restriction essential? And if not, why 
make it?
Oct 30 2022
next sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Sunday, 30 October 2022 at 16:09:54 UTC, NonNull wrote:
 I am linking to a C project with some C already automatically 
 translated into D including the C main function `int main(int 
 argc, char** argv){/* ... */}` which I wanted to call from a D 
 main function in a new module. But then I found that the former 
 C main in D will not compile! ldc2 complains that main must 
 only have prototypes as if it is the entry point. So it seems 
 that main cannot be overloaded with a prototype that is not a 
 valid entry point in D. Is this restriction essential? And if 
 not, why make it?
You should not have multiple mains. Rename it and call it
Oct 30 2022
parent reply NonNull <non-null use.startmail.com> writes:
On Sunday, 30 October 2022 at 16:31:45 UTC, Imperatorn wrote:
 You should not have multiple mains. Rename it and call it
Doesn't answer my questions. I wasn't asking for practical, moral or esthetic advice.
Oct 30 2022
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Sunday, 30 October 2022 at 17:29:25 UTC, NonNull wrote:
 On Sunday, 30 October 2022 at 16:31:45 UTC, Imperatorn wrote:
 You should not have multiple mains. Rename it and call it
Doesn't answer my questions. I wasn't asking for practical, moral or esthetic advice.
Try to phrase your question more clearly. I'm just stating a fact.
Oct 30 2022
parent NonNull <non-null use.startmail.com> writes:
On Sunday, 30 October 2022 at 17:41:07 UTC, Imperatorn wrote:
 On Sunday, 30 October 2022 at 17:29:25 UTC, NonNull wrote:
 On Sunday, 30 October 2022 at 16:31:45 UTC, Imperatorn wrote:
 You should not have multiple mains. Rename it and call it
Doesn't answer my questions. I wasn't asking for practical, moral or esthetic advice.
Try to phrase your question more clearly. I'm just stating a fact.
Doesn't look like a fact. Looks like a judgment. A different judgment might be to allow overloads and not have them be an entry point unless they have the signatures identified as such by D. My questions "Is this restriction essential? And if not, why make it?" are asking to justify the judgment you made. Which you did not do. So you have not answered them. Not unclear at all.
Oct 30 2022
prev sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Sunday, 30 October 2022 at 16:09:54 UTC, NonNull wrote:
 I am linking to a C project with some C already automatically 
 translated into D including the C main function `int main(int 
 argc, char** argv){/* ... */}` which I wanted to call from a D 
 main function in a new module.
did you put extern(C) on that thing? im p sure it'd allow it then but.... then it'd be the entry point instead of the D main so i don't think it'd let you get ahead.
 So it seems that main cannot be overloaded with a prototype 
 that is not a valid entry point in D. Is this restriction 
 essential? And if not, why make it?
it prolly just to keep newbs from making confusing mistakes and getting weird behavior at startup. If there's two mains, which one is the expected entry? Perhaps it could just be the one that matches the permitted signature, and if there isn't one it complains. But how does it know if there is one? Suppose there's module A: module a; int main(int argc, char** argv) {} And module B: module b; void main() {} They are compiled separately: dmd -c a.d dmd -c b.d dmd a.o b.o Which one is the main you want to use? What if you just did `dmd a.d`, should it error that the prototype is wrong, or just give the user the linker error "could not find D main" when they think they have it right there? (remember the linker doesn't know what D signatures are so it can't print a helpful message to correct the mistake).
Oct 30 2022
parent reply NonNull <non-null use.startmail.com> writes:
On Sunday, 30 October 2022 at 18:24:22 UTC, Adam D Ruppe wrote:
 it prolly just to keep newbs from making confusing mistakes and 
 getting weird behavior at startup. If there's two mains, which 
 one is the expected entry? Perhaps it could just be the one 
 that matches the permitted signature, and if there isn't one it 
 complains. But how does it know if there is one? Suppose 
 there's module A:

 module a;
 int main(int argc, char** argv) {}


 And module B:
 module b;
 void main() {}


 They are compiled separately:

 dmd -c a.d
 dmd -c b.d
 dmd a.o b.o

 Which one is the main you want to use? What if you just did 
 `dmd a.d`, should it error that the prototype is wrong, or just 
 give the user the linker error "could not find D main" when 
 they think they have it right there? (remember the linker 
 doesn't know what D signatures are so it can't print a helpful 
 message to correct the mistake).
Ah, makes sense to limit the possible low level error messages with separate compilation because of the linker not knowing D signatures. Thanks for the intuition.
Oct 30 2022
parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Sunday, 30 October 2022 at 23:43:03 UTC, NonNull wrote:
 On Sunday, 30 October 2022 at 18:24:22 UTC, Adam D Ruppe wrote:
 [...]
Ah, makes sense to limit the possible low level error messages with separate compilation because of the linker not knowing D signatures. Thanks for the intuition.
I thought this was too obvious to say, but apparently not.
Oct 31 2022