www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Version for windows/console compilation?

reply "Prudence" <Pursuit Happyness.All> writes:
Is there a flag for knowing when a project is compiling for 
windows(Uses WinMain) vs a console(normal main)?

version(Windows) is always valid for a console app, so it is 
useless to disambiguate between a console app and a windows app. 
(Say I have both a main and a winmain in my code, I need to 
select between them(it's a bit more complex than this but)).
Sep 10 2015
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 10 September 2015 at 18:06:43 UTC, Prudence wrote:
 Is there a flag for knowing when a project is compiling for 
 windows(Uses WinMain) vs a console(normal main)?
You'd have to choose the main yourself anyway, so document what process you use for that for people to use. BTW it is pretty rare that you should actually write a WinMain in D. The right thing to do in most cases is to write a normal main function. You can still get the windows gui subsystem with a linker flag.
Sep 10 2015
parent reply "Mike Parker" <aldacron gmail.com> writes:
On Thursday, 10 September 2015 at 18:10:43 UTC, Adam D. Ruppe 
wrote:
 BTW it is pretty rare that you should actually write a WinMain 
 in D. The right thing to do in most cases is to write a normal 
 main function. You can still get the windows gui subsystem with 
 a linker flag.
Specifically, add the following when using the Microsoft linker (compiling with -m64 or -m32mscoff): -L/SUBSYSTEM:windows,6.00 -L/ENTRY:mainCRTStartup And this when using OPTLINK: -L/SUBSYSTEM:windows,5.01 The version numbers are optional. I use 6.00 with the MS linker because it covers both 32-bit and 64-bit apps on Vista and later, and is the default for the VS 2015 linker. [1] https://msdn.microsoft.com/en-us/library/fcc1zstk.aspx I don't think I've written a WinMain in D in 10 years.
Sep 10 2015
parent reply "Prudence" <Pursuit Happyness.All> writes:
On Friday, 11 September 2015 at 01:36:31 UTC, Mike Parker wrote:
 On Thursday, 10 September 2015 at 18:10:43 UTC, Adam D. Ruppe 
 wrote:
 BTW it is pretty rare that you should actually write a WinMain 
 in D. The right thing to do in most cases is to write a normal 
 main function. You can still get the windows gui subsystem 
 with a linker flag.
Specifically, add the following when using the Microsoft linker (compiling with -m64 or -m32mscoff): -L/SUBSYSTEM:windows,6.00 -L/ENTRY:mainCRTStartup And this when using OPTLINK: -L/SUBSYSTEM:windows,5.01 The version numbers are optional. I use 6.00 with the MS linker because it covers both 32-bit and 64-bit apps on Vista and later, and is the default for the VS 2015 linker. [1] https://msdn.microsoft.com/en-us/library/fcc1zstk.aspx I don't think I've written a WinMain in D in 10 years.
I'm using Visual D and I assume it takes care of all this. It works so that's not a huge problem. I'm simply creating my own version flags in VD properties. Not the best way because I'll have to remember to set the flags every time I use the library or I'll get errors about stuff missing. I was hoping D had a flag to disambiguate console and windows apps(or some type to CT way to check).
Sep 10 2015
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 11 September 2015 at 04:30:44 UTC, Prudence wrote:
 I'm using Visual D and I assume it takes care of all this. It 
 works so that's not a huge problem.
If it is taking care of the linker switch, then you gain nothing but more complicated and fragile code by writing a WinMain!
 I was hoping D had a flag to disambiguate console and windows 
 apps(or some type to CT way to check).
There's not really a difference between them. A console app can create windows and a gui app can allocate a console. The best thing to check for might just be if there's already a console available when you want to use one.
Sep 11 2015
prev sibling parent "Mike Parker" <aldacron gmail.com> writes:
On Friday, 11 September 2015 at 04:30:44 UTC, Prudence wrote:
 I'm simply creating my own version flags in VD properties. Not 
 the best way because I'll have to remember to set the flags 
 every time I use the library or I'll get errors about stuff 
 missing. I was hoping D had a flag to disambiguate console and 
 windows apps(or some type to CT way to check).
The subsystem is set at link time, so there is no reliable way at compile time to determine the configuration. A simple and cheap runtime check can be made like this: import core.sys.windows.windows : GetConsoleCP; bool hasConsole = !!GetConsoleCP();
Sep 11 2015