@@ -1702,3 +1702,46 @@ Well now our cursor works just fine!
17021702
17031703The day is saved!
17041704
1705+ ## windows_subsystem
1706+
1707+ A last note before we go.
1708+ The Windows operating system has two "subsystems" that a program can target: "console" and "windows".
1709+ The only difference to us Rust folks is if the program gets a console allocated for it automatically by the system.
1710+ * A console subsystem program gets a console allocated for it.
1711+ If you run it from an existing console it'll use that one,
1712+ but if you run it via other means it'll open a new console of its own while it's running.
1713+ * A windows subsystem program does * not* get a console allocated for it.
1714+
1715+ If you try to use standard output or standard error, such as with ` println! ` and ` eprintln! ` ,
1716+ and there's no currently allocated console,
1717+ then the output goes nowhere.
1718+
1719+ By default, a Rust program is a console subsystem program.
1720+ This is fine, because it means that we can print debug messages.
1721+ However, it also means that if we give our program to a friend and they run it,
1722+ maybe by double clicking the executable in windows explorer,
1723+ then they'll see the window that we want open up * and also* they'll see a mysterious console open in the background.
1724+ If you really really mean for end users to see that console appear then that's fine,
1725+ but most people don't expect an extra console to appear when the run a Windows program.
1726+ It feels a little janky for this debug thing to show up along side the "real" program's window.
1727+
1728+ This is easy enough to fix.
1729+ If we use the [ windows_subsystem] ( https://doc.rust-lang.org/stable/reference/runtime.html#the-windows_subsystem-attribute )
1730+ attribute on our program we can pick the subsystem we want the program to use.
1731+
1732+ ``` rust
1733+ // at the top of the file
1734+ #![windows_subsystem = " windows" ]
1735+ ```
1736+
1737+ However, if we use that attribute directly then * even debugging builds* don't get a console.
1738+
1739+ Instead, I like to use [ cfg_attr] ( https://doc.rust-lang.org/stable/reference/conditional-compilation.html?highlight=cfg_attr#the-cfg_attr-attribute )
1740+ to set the ` windows_subsystem ` value any time we're * not* using debug assertions.
1741+ That's not exactly the same as "whenever it's a release build", but close enough.
1742+
1743+ ``` rust
1744+ #![cfg_attr(not(debug_assertions), windows_subsystem = " windows" )]
1745+ ```
1746+
1747+ And now we can send a release build of the program to our friends and have it act like they expect a normal Win32 program to act.
0 commit comments