title: How to hide console window from Windows Gtk C programs compiled with cl
body: <p>Due to unstable and unreliable mingw / MSYS2 behaviours on Windows 11 (x86_64), I have decided to use the Windows-native <code>cl</code> compiler bundled with Visual Studio to compile Gtk 3 programs written in C.  I am using Gtk 3 built with the <a href="https://github.com/wingtk/gvsbuild" rel="nofollow noreferrer">gvsbuild</a> script.  My Gtk 3 test program compiles successfully with the <code>cl</code> compiler, using a compiler <a href="https://web.archive.org/web/20220419135908/https://docs.microsoft.com/en-us/cpp/build/reference/at-specify-a-compiler-response-file?view=msvc-170" rel="nofollow noreferrer">command-file</a> / response-file for the compile and linker options, but when I run the compiled Gtk 3 program, I get a console window behind it.</p>
  <p><a href="https://www.pismotek.com/media/screenshot-gtk-console.png" rel="nofollow noreferrer"><img src="https://www.pismotek.com/media/screenshot-gtk-console.png" alt="Image of Gtk test program with console behind it" /></a></p>
  <p>I have searched tirelessly on StackOverflow as well as the rest of the internet and have not found a solution that addresses my <code>cl</code>-specific situation.</p>
  <p>I have tried using <code>/SUBSYSTEM:WINDOWS</code> which causes a compiler error because the compiler can't find WinMain hidden by Gtk.  I have tried <code>/D&quot;subsystem,windows&quot;</code>, <code>/Dmwindows</code> as well as <code>/Dsubsystem=gui</code> with and without the <code>-</code> in front, but none solve the issue.</p>
  <p>I have also tried this bit of code, which causes a huge amount of errors because the compiler can't find Windows-specific code hidden by Gtk:</p>
  <pre><code>#ifdef _WIN32
  AllocConsole();
  ShowWindow(GetConsoleWindow(), SW_HIDE);
  #endif
  </code></pre>
  <p>Here is the test program:</p>
  <pre><code>#include &lt;gtk/gtk.h&gt;
  
  static void activate (GtkApplication * app, gpointer user_data)
  {
    GtkWidget * window;
  
    window = gtk_application_window_new (app);
    gtk_window_set_title (GTK_WINDOW (window), &quot;Hello internet from Gtk Windows!&quot;);
    gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
    gtk_widget_show_all (window);
  }
  
  int main (int argc, char ** argv)
  {
    GtkApplication * app;
    int status;
  
    app = gtk_application_new (&quot;org.gtk.example&quot;, G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, &quot;activate&quot;, G_CALLBACK (activate), NULL);
    status = g_application_run (G_APPLICATION (app), argc, argv);
    g_object_unref (app);
  
    return status;
  }
  </code></pre>
  <p>I've elected not to post the compiler command-file contents as it's quite lengthy.</p>
  <p>For those who may answer &quot;Just use mingw, MSYS2, cygwin, etc&quot; I'd rather stay with a <code>cl</code>-specific solution please.</p>
  <p>Is there a <code>cl</code>-specific option for suppressing the console window when compiling and running C Gtk 3 programs on Windows?</p>
  <hr />
  <p><strong>A big thanks to Tony Lee</strong> for the recommendation!  This code is working properly after specifying <code>/SUBSYSTEM:WINDOWS</code> in the linker options:</p>
  <pre><code>#include &lt;gtk/gtk.h&gt;
  
  static void activate (GtkApplication * app, gpointer user_data)
  {
    GtkWidget * window;
  
    window = gtk_application_window_new (app);
    gtk_window_set_title (GTK_WINDOW (window), &quot;Hello internet from Gtk Windows!&quot;);
    gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
    gtk_widget_show_all (window);
  }
  
  #ifdef _WIN32
  int WinMain (void * hInstance, void * hPrevInstance, char ** argv, int nCmdShow)
  #else
  int main (int argc, char ** argv)
  #endif
  {
    GtkApplication * app;
    int status;
  
    app = gtk_application_new (&quot;org.gtk.example&quot;, G_APPLICATION_FLAGS_NONE);
    g_signal_connect (app, &quot;activate&quot;, G_CALLBACK (activate), NULL);
    status = g_application_run (G_APPLICATION (app), 0, argv);
    g_object_unref (app);
  
    return status;
  }
  </code></pre>
  <p>It should be noted that I had to put a zero for the <code>argc</code> value in <code>g_application_run()</code>, so that's another (minor) issue I'll have to figure out later, but if your Gtk program doesn't need to process command-line arguments, it's fine.</p>