Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to close the console on the Windows? #1190

Closed
Arkinux opened this issue Apr 25, 2022 · 5 comments
Closed

How to close the console on the Windows? #1190

Arkinux opened this issue Apr 25, 2022 · 5 comments

Comments

@Arkinux
Copy link

Arkinux commented Apr 25, 2022

When I double-click to launch the application, the black console appears:

微信图片_20220425175017

How to close the console on the Windows?

@tronical
Copy link
Member

You may need to select windows as the windows Subsystem instead of rust’s console default, using the windows_subsystem attribute.

So basically inserting this into your main.rs should do the trick:

#![windows_subsystem = "windows"]

You can also make this conditional so that you can still see debug output in debug builds:

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

@Arkinux
Copy link
Author

Arkinux commented Apr 25, 2022

You may need to select windows as the windows Subsystem instead of rust’s console default, using the windows_subsystem attribute.

So basically inserting this into your main.rs should do the trick:

#![windows_subsystem = "windows"]

You can also make this conditional so that you can still see debug output in debug builds:

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

Thank you. You solved my problem!

@medcelerate
Copy link

How would we do this in a C++ example?

@tronical
Copy link
Member

You need to pass WIN32 to add_executable:

https://cmake.org/cmake/help/latest/command/add_executable.html

@SolemnJoker
Copy link

How would we do this in a C++ example?
@medcelerate

You can add the WIN32 option in your CMakeLists.txt as follows:

add_executable(my_application WIN32 src/main.cpp)

Or open project's Properties-Link-System-SubSystem in your visual studio, change SUBSYSTEM:CONSOLE to SUBSYSTEM:WINDOWS

And change your main function to WinMain in your main.cpp:

#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // Write your application logic here. The console window will not be displayed.
    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants