5

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 ?

3
  • 1
    On MinGW, compiling with -mwindows disables the console. MSVC probably has something similar. Commented Jun 7, 2020 at 21:40
  • Does this answer your question? C++ How do I hide a console window on startup? Commented Jun 7, 2020 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?) Commented Jun 7, 2020 at 22:19

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.