overloading main

Adam D Ruppe destructionator at gmail.com
Sun Oct 30 18:24:22 UTC 2022


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).


More information about the Digitalmars-d-learn mailing list