| Thread overview | |||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 19, 2009 No -c no main() | ||||
|---|---|---|---|---|
| ||||
Would it be a big deal to make the compiler recognize that there is no -c, but no instance of main() has been found, and say
"Link skipped - no main()"
instead of what you get if you do dmd mian.d on
import std.stdio;
void mian()
{
writefln("Hello Wolrd");
}
| ||||
March 19, 2009 Re: No -c no main() | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steve Teale |
Steve Teale wrote:
> Would it be a big deal to make the compiler recognize that there is no -c, but no instance of main() has been found, and say
>
> "Link skipped - no main()"
>
> instead of what you get if you do dmd mian.d on
>
> import std.stdio;
>
> void mian()
> {
> writefln("Hello Wolrd");
> }
main might be linked in from another library/object on the command line.
dmd stuff.d unittest_main_stub.obj
You'd have to have the compiler crack open all .lib and .obj files and look for a main.
-- Daniel
| |||
March 19, 2009 Re: No -c no main() | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep | Daniel Keep Wrote:
>
>
> Steve Teale wrote:
> > Would it be a big deal to make the compiler recognize that there is no -c, but no instance of main() has been found, and say
> >
> > "Link skipped - no main()"
> >
> > instead of what you get if you do dmd mian.d on
> >
> > import std.stdio;
> >
> > void mian()
> > {
> > writefln("Hello Wolrd");
> > }
>
> main might be linked in from another library/object on the command line.
>
> dmd stuff.d unittest_main_stub.obj
>
> You'd have to have the compiler crack open all .lib and .obj files and look for a main.
>
> -- Daniel
True, but maybe if there were only .d files?
| |||
March 19, 2009 Re: No -c no main() | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | It's a good idea. Please add to bugzilla as an enhancement request! | |||
March 19, 2009 Re: No -c no main() | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On 19/03/2009 12:19, Walter Bright wrote:
> It's a good idea. Please add to bugzilla as an enhancement request!
another thing that can be done is provide a compiler flag to specify the module that contains main() so the compiler will use that specific main and ignore all others. this is similar to how you specify the class with main() method in Java. Jar files have a manifest with this info and in Eclipse you specify the class in the Run dialog. I don't remember how it works with the command line (actually, I don't remember ever using the command line with Java)
| |||
March 19, 2009 Re: No -c no main() | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Yigal Chripun | Yigal Chripun wrote:
> On 19/03/2009 12:19, Walter Bright wrote:
>> It's a good idea. Please add to bugzilla as an enhancement request!
>
> another thing that can be done is provide a compiler flag to specify the module that contains main() so the compiler will use that specific main and ignore all others. this is similar to how you specify the class with main() method in Java. Jar files have a manifest with this info and in Eclipse you specify the class in the Run dialog. I don't remember how it works with the command line (actually, I don't remember ever using the command line with Java)
It's not something that happens at compile time. All existing main methods are compiled into their respective class files. From the command line, you execute the JRE (java or javaw in Sun's case) and pass it the name of the class you want to execute. If the class has a main method, it is called. If not, you get an error.
| |||
March 20, 2009 Re: No -c no main() | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | Mike Parker Wrote:
> Yigal Chripun wrote:
> > On 19/03/2009 12:19, Walter Bright wrote:
> >> It's a good idea. Please add to bugzilla as an enhancement request!
> >
> > another thing that can be done is provide a compiler flag to specify the module that contains main() so the compiler will use that specific main and ignore all others. this is similar to how you specify the class with main() method in Java. Jar files have a manifest with this info and in Eclipse you specify the class in the Run dialog. I don't remember how it works with the command line (actually, I don't remember ever using the command line with Java)
>
> It's not something that happens at compile time. All existing main methods are compiled into their respective class files. From the command line, you execute the JRE (java or javaw in Sun's case) and pass it the name of the class you want to execute. If the class has a main method, it is called. If not, you get an error.
Java's model is complicated. it has the concept of class-loaders.
What I'm suggesting is this:
given three modules a,b and c all defining the main() function, you can do the following:
dmd a.d b.d c.d --main=b.d
currently the compiler will complain about multiple definitions of main(), but with the above flag, it could skip over the main() functions in a and c.
the next logical step would be to allow:
dmd -c a.d
dmd -c b.d
dmd -c c.d
dmd a.obj b.obj c.obj --main=b.obj
in the above all three obj files have a main() method. this time it must be the *linker* that needs to understand the flag and only link the main() function into the executable. this requires changing the linker which is written in asm so less likely to happen.
I was not suggesting D to use a VM or the class-loader concept as in Java. even if we want that concept in D it needs to have a different implementation than in Java since there are problems with the way Java does this. (that's besides the differences due to the JVM)
| |||
March 20, 2009 Re: No -c no main() | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Yigal Chripun | Yigal Chripun wrote:
> Mike Parker Wrote:
>
>> Yigal Chripun wrote:
>>> On 19/03/2009 12:19, Walter Bright wrote:
>>>> It's a good idea. Please add to bugzilla as an enhancement request!
>>> another thing that can be done is provide a compiler flag to specify the module that contains main() so the compiler will use that specific main and ignore all others. this is similar to how you specify the class with main() method in Java. Jar files have a manifest with this info and in Eclipse you specify the class in the Run dialog. I don't remember how it works with the command line (actually, I don't remember ever using the command line with Java)
>> It's not something that happens at compile time. All existing main methods are compiled into their respective class files. From the command line, you execute the JRE (java or javaw in Sun's case) and pass it the name of the class you want to execute. If the class has a main method, it is called. If not, you get an error.
>
> Java's model is complicated. it has the concept of class-loaders. What I'm suggesting is this: given three modules a,b and c all defining the main() function, you can do the following:
> dmd a.d b.d c.d --main=b.d currently the compiler will complain about multiple definitions of main(), but with the above flag, it could skip over the main() functions in a and c.
>
> the next logical step would be to allow:
> dmd -c a.d
> dmd -c b.d dmd -c c.d
> dmd a.obj b.obj c.obj --main=b.obj
>
> in the above all three obj files have a main() method. this time it must be the *linker* that needs to understand the flag and only link the main() function into the executable. this requires changing the linker which is written in asm so less likely to happen.
>
> I was not suggesting D to use a VM or the class-loader concept as in Java. even if we want that concept in D it needs to have a different implementation than in Java since there are problems with the way Java does this. (that's besides the differences due to the JVM)
I understand what you were suggesting. My point is that in Java, which entry point to use is decided at run time, not compile time. From that perspective, it's a useful feature. For a statically compiled language like D, I don't see any obvious benefit. I mean, what is the benefit of this over delegating to a specific pseudo-main method based on a commandline arg or config file?
| |||
March 20, 2009 Re: No -c no main() | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | Mike Parker escribió:
> Yigal Chripun wrote:
>> Mike Parker Wrote:
>>
>>> Yigal Chripun wrote:
>>>> On 19/03/2009 12:19, Walter Bright wrote:
>>>>> It's a good idea. Please add to bugzilla as an enhancement request!
>>>> another thing that can be done is provide a compiler flag to specify the module that contains main() so the compiler will use that specific main and ignore all others. this is similar to how you specify the class with main() method in Java. Jar files have a manifest with this info and in Eclipse you specify the class in the Run dialog. I don't remember how it works with the command line (actually, I don't remember ever using the command line with Java)
>>> It's not something that happens at compile time. All existing main methods are compiled into their respective class files. From the command line, you execute the JRE (java or javaw in Sun's case) and pass it the name of the class you want to execute. If the class has a main method, it is called. If not, you get an error.
>>
>> Java's model is complicated. it has the concept of class-loaders. What I'm suggesting is this: given three modules a,b and c all defining the main() function, you can do the following:
>> dmd a.d b.d c.d --main=b.d currently the compiler will complain about multiple definitions of main(), but with the above flag, it could skip over the main() functions in a and c.
>> the next logical step would be to allow:
>> dmd -c a.d
>> dmd -c b.d dmd -c c.d
>> dmd a.obj b.obj c.obj --main=b.obj
>>
>> in the above all three obj files have a main() method. this time it must be the *linker* that needs to understand the flag and only link the main() function into the executable. this requires changing the linker which is written in asm so less likely to happen.
>> I was not suggesting D to use a VM or the class-loader concept as in Java. even if we want that concept in D it needs to have a different implementation than in Java since there are problems with the way Java does this. (that's besides the differences due to the JVM)
>
> I understand what you were suggesting. My point is that in Java, which entry point to use is decided at run time, not compile time. From that perspective, it's a useful feature. For a statically compiled language like D, I don't see any obvious benefit. I mean, what is the benefit of this over delegating to a specific pseudo-main method based on a commandline arg or config file?
It is useful. Suppose you are writing a new module and want to test something quickly (not with unittests, maybe make a little program to see how a specific feature you just wrote works). So you write a main right there in the module, compile it and link it saying that the main is in your new module. And you can then run it.
If you couldn't do that you'd have to search for your only main in your project, change it by adding the necessary imports, compile it and run it. Way more steps.
Also, that way each module can have a main method that shows some of it's funcionality with a little program.
| |||
March 20, 2009 Re: No -c no main() | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | Ary Borenszweig:
> It is useful. Suppose you are writing a new module and want to test something quickly (not with unittests, maybe make a little program to see how a specific feature you just wrote works). So you write a main right there in the module, compile it and link it saying that the main is in your new module. And you can then run it.
It has many usages, we have already discussed about this in the past.
In D you can group related functions and classes into one module, so sometimes a module can be run alone (with the help of some modules it imports). So you can add demonstration code into its main, or benchmark/testing code, or module testing code, and so on. You can even create a program where certain modules can be sub-programs, that you can run alone to do specific sub-purposes, instead of using them as imported modules.
Such usages are very common in Python too, there's even a idiomatic syntax to do such things. In the past other people and me have suggested some ways to do this.
One of them was to add a __MAIN__ compile time constant defined as false for all the modules that the compiler is asked to not compile as the (main) module that contains the main(), so you can use a:
static if (__MAIN__) { void main(string[] args) {...} }
to wrap the main function. I don't remember if this very simple idea was already shot down or improved by better ideas.
Bye,
bearophile
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply