4

How to run a C++ code, without console ?

ShowWindow(GetConsoleWindow(), SW_HIDE); hide the window, but after it apear.

Before run the program, can I put a line witch hide completly console ?

Rafo 554
  • 99
  • 5
  • 1
    On MinGW, compiling with `-mwindows` disables the console. MSVC probably has something similar. – HolyBlackCat Jun 07 '20 at 21:40
  • Does this answer your question? [C++ How do I hide a console window on startup?](https://stackoverflow.com/questions/18260508/c-how-do-i-hide-a-console-window-on-startup) – Asteroids With Wings Jun 07 '20 at 21:41
  • 2
    This is platform-specific, as C++ has no concept of "console". Could you add your operating system to the question? It might also be good to include the method you use to run your program. (Presumably you are not running from a command line, so you launch from an IDE?) – JaMiT Jun 07 '20 at 22:19

1 Answers1

3

You can set this pragma inside the file where main method is located, on top of your header files includes:

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")

This can also be done with linker options:

/SUBSYSTEM:windows
/ENTRY:mainCRTStartup

As alternative, in VS, change the project subsystem to Windows (/SUBSYSTEM:WINDOWS) in Project Properties-Linker-System-Subsystem. If you do that, use the WinMain signature instead of main signature:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
    // Your code here.
    return 0;
}

Subsystem settings.

Fabio Crispino
  • 711
  • 1
  • 6
  • 22
  • Also, if you want to have a cross-platform solution as the question does not really specify the target OS, you can distinguish between Windows and non-Windows system using preprocessor definitions. For example one way to do it is to wrap WinMain and the windows required header using a combination of #ifdef _WIN32, #define, #endif and #ifndef. In Unix systems you can simply use main method and console won't appear. For a more detailed list of preprocessor definitions, you can check [here](https://iq.opengenus.org/detect-operating-system-in-c/). – Fabio Crispino Jun 07 '20 at 22:27