Sign in to follow this  
Slaru

Windows console app

Recommended Posts

Hello, In windows, when your main function is a main() and not a WinMain() function, you get a console. I am using GLFW to manage my windows. So I don't need a WinMain function. Is there a way to make the console dissapear? ex. You double click the exe, you never see the console, and then the window GLFW creates appears. I want to make the console never appear. Is this possible? Thanks, Slaru

Share this post


Link to post
Share on other sites
Short answer: no
Long answer: if you really really want to, I'm sure it's possible (through various API calls and events), but there's no easy way that I've ever seen

Console apps are meant to be just that: consoles. The fact that you can create windows is really just a side-effect of the fact that it's still a win32 app.

Most developers end up creating a stub WinMain that wraps whatever framework they're using. SDL, for example, provides a wrapper that creates the graphics context then calls your main() function so you can start the game.

Share this post


Link to post
Share on other sites
If I recall correctly, simply putting your main function in WinMain (and setting a windows, rather than console, subsystem) is all you need to do. WinMain does not automatically create a window.

Share this post


Link to post
Share on other sites
The reason I wanted to was so I can totally not rely on Windows, for easy cross-platform porting (GLFW is cross-platform). If it is very complicated, then I will just use windows and WinMain. But if there is a way not too complicated, then that would be nice.

Share this post


Link to post
Share on other sites
You will need to write some windows specific code. You can either achieve this as Sneftel suggested (/SUBSYTEM:Windows) and winmain, or you can just call
FreeConsole
Just use
#ifdef _WIN32
#endif
to ensure your windows specific code only compiles under windows

Share this post


Link to post
Share on other sites
You could try something like:

int main(void);

#ifdef _WIN32 // If compiling for 32-bit Windows

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
return main(); // Note: If you have
// int main(int argc, char **argv)
// then you will have to parse lpCmdLine and fill in argc and argv yourself
}

#endif

int main(void)
{
...
}


This ensures that, if compiling under Windows, WinMain() will be in the entry point and it will call main(), and if compiling under a different OS, main() will simply be the entry point.

Share this post


Link to post
Share on other sites
Short answer: Yes.

I make GLUT apps all the time using main(). If you are using Visual Studio, add this line to your source:

#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )

You could do it in the project properties, but this line is easier.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this