FAQ - Help and questions

How do I stop a black console window opening behind my Windows GUI app?

#windows#console#cfg#desktoppublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

A Windows executable is built for either the console or the GUI subsystem. Add #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] at the top of main.rs, so release builds open no console while debug builds keep one for println. The subsystem field of both builds was read from the files.

Checked with rustc 1.93.0, Windows 11; built in debug and release, run, and the Subsystem field of each executable's header was read (2 for the GUI subsystem, 3 for console).

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

Windows marks every program with a subsystem. A console program gets a console window (or shares the one it was started from). A GUI program gets none. Rust builds console programs by default, so a double-clicked GUI app also opens a black window.

One line at the top of the crate root changes that:

src/main.rsrust
// Release builds on Windows get the GUI subsystem (no console window); debug builds keep the console for println!.
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

fn main() {
    println!("hello"); // visible only when the console exists
}

cfg_attr(condition, attribute) applies the attribute only when the condition holds, here "not a debug build". On macOS and Linux the windows_subsystem attribute has no effect, so the line is harmless there.

What was checked: both builds were made, run, and the Subsystem field in the PE header of each executable was read. The debug build has value 3 (console) and printed hello; the release build has value 2 (GUI) and printed nothing.

Side effects of the GUI subsystem:

  • println! output goes nowhere, and panics print no message anywhere a user can see it. Log to a file in the app's data folder (see the settings thread), and show a message box or error banner for fatal errors.
  • A program started from a terminal does not print into that terminal. Windows offers a way to attach to the parent's console (AttachConsole in the Windows API); it needs a call into the Windows API.
  • Command-line tools that share the code should be a separate binary in the same package, built as a console program.

Common mistakes#

  • Applying windows_subsystem to a debug build and then wondering why println! and panic messages vanish. The cfg_attr line above avoids it.
  • Putting the attribute in a module. It belongs at the crate root (the top of main.rs) and needs the #! form.
  • Using it for a service or a tool that must print: they need the console subsystem.

Does it also set the icon or version information?#

No. Those are resources embedded in the executable; a build.rs with a resource crate does it. This thread covers only the console window.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet