Removing console window for Glut/FreeGlut/GLFW?
Asked Answered
D

9

15

Under Visual C++, I have played around with Glut/FreeGlut/GLFW. It seems that everyone of these projects adds a CMD window by default. I tried removing it going under:

Properties->C/C++->Preprocessor->Preprocessor Definitions

From here, I remove the _CONSOLE and replace it with _WINDOWS

Then I went under:

Properties->Linker->System->SubSystem

And I set the option to Windows (/SUBSYSTEM:WINDOWS)

Then when I try compiling under GLFW, I get the following building errors:

  • Error 1 error LNK2001: unresolved external symbol _WinMain@16 MSVCRT.lib

  • Error 2 fatal error LNK1120: 1 unresolved externals glfwWindow.exe

Is it possible to remove the console window?

Dunigan answered 13/5, 2011 at 17:10 Comment(0)
G
5

Non-console Windows applications use the WinMain() entry point convention. Your Glut examples probably use the standard C main() convention.

If you want a quick fix just for the demo app, the WinAPI function FreeConsole() might help.

MSDN: http://msdn.microsoft.com/en-us/library/ms683150(v=vs.85).aspx

Greengage answered 13/5, 2011 at 17:19 Comment(6)
This worked, and I will keep note of it for sure, but it would be nice to have an x-platform way of doing this. Thanks again!Dunigan
Well, you don't really have the same problem on other platforms, so I'd suggest just wrapping FreeConsole() in an #ifdef WIN32 / #endif block.Greengage
Are you saying that in Linux and Mac, they do not have a console window by default?Dunigan
I don't know about Mac. On Linux, if you launch an application from, for example, the Ubuntu desktop, a console window will not pop up to show you standard output. Program output will just disappear into nowhere. If you run it in a terminal, then it will dump standard output there like normal. There's no Linux equivalent for FreeConsole().Greengage
Thanks for letting me know. I will do what Sean Edwards wrote in the comment and add the #ifdef WIN32. This should do it.Dunigan
It's worth noting that with FreeConsole(), the console window still pops up for an instant before disappearing.Michel
W
23

Under the linker options, set your entry point to mainCRTStartup . This function does the necessary setup of the MS libc and then calls main.

Woolly answered 4/6, 2011 at 20:9 Comment(2)
Works like a charm combined with @Emre's answer. Thanks.Urdar
Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)Parnassian
M
14

My project just has a main, (no WinMain) and to disable console, I just set Linker->System->SubSystem to "Windows (/SUBSYSTEM:WINDOWS)" instead of "Console (/SUBSYSTEM:CONSOLE)" and the console goes away.

You don't need to mess with the Preprocessor Definitions to remove the console window.

I know my answer is a few years late, but I hope it helps.

Moot answered 18/6, 2014 at 12:13 Comment(2)
I tried this, but in VS2015 it gives a linker error: unresolved external symbol WinMain referenced in function "int __cdecl invoke_main(void)"Michel
To get remove linker error: unresolved external symbol WinMain... you have to add 'mainCRTStartup' entry point: Linker->All Options->Entry Point (VS2019 Community)Hydroquinone
A
12

Most linkers support options that automatically remove the console startup code.

I think on GCC it's called -mwindows

Aflutter answered 7/7, 2011 at 18:18 Comment(1)
+1 since i had WinMain, but the console kept opening.Trutko
D
7

To get rid of the console using cmake, the link flags can be set as follows:

set_target_properties(exe_name PROPERTIES 
    LINK_FLAGS "/ENTRY:mainCRTStartup /SUBSYSTEM:WINDOWS")
Desmond answered 17/10, 2017 at 19:6 Comment(2)
this is great wrapping into a IF (WIN32) to make an application cross platform.Basseterre
Under Visual Studio, it works in Linker->All OptionsHydroquinone
G
5

Non-console Windows applications use the WinMain() entry point convention. Your Glut examples probably use the standard C main() convention.

If you want a quick fix just for the demo app, the WinAPI function FreeConsole() might help.

MSDN: http://msdn.microsoft.com/en-us/library/ms683150(v=vs.85).aspx

Greengage answered 13/5, 2011 at 17:19 Comment(6)
This worked, and I will keep note of it for sure, but it would be nice to have an x-platform way of doing this. Thanks again!Dunigan
Well, you don't really have the same problem on other platforms, so I'd suggest just wrapping FreeConsole() in an #ifdef WIN32 / #endif block.Greengage
Are you saying that in Linux and Mac, they do not have a console window by default?Dunigan
I don't know about Mac. On Linux, if you launch an application from, for example, the Ubuntu desktop, a console window will not pop up to show you standard output. Program output will just disappear into nowhere. If you run it in a terminal, then it will dump standard output there like normal. There's no Linux equivalent for FreeConsole().Greengage
Thanks for letting me know. I will do what Sean Edwards wrote in the comment and add the #ifdef WIN32. This should do it.Dunigan
It's worth noting that with FreeConsole(), the console window still pops up for an instant before disappearing.Michel
V
1

You need to write a WinMain entry point and copy your existing code (from main):

int CALLBACK WinMain(
  __in  HINSTANCE hInstance,
  __in  HINSTANCE hPrevInstance,
  __in  LPSTR lpCmdLine,
  __in  int nCmdShow
){
    // ...
}
Viscoid answered 13/5, 2011 at 17:17 Comment(3)
Ok, I see what you mean, but I would need a cross platform method of doing this. I'll probably have to go with what onteria_ said. I'll give that a shot. Thanks for the great answer though.Dunigan
I use to have a WinMain to main converter. This way, your application provides both entry point. You just need to convert the command line from lpCmdLine to argc, argv.Viscoid
I'm not quite sure what you mean by that. Is it something I have to supply to a function, or a flag for Visual Studio?Dunigan
A
0

If you create a new project as a console application, it will always run as such. You have to create a new GUI project if you want to run it in an actual window, otherwise the correct headers and libraries will not be included.

Also the WinMain function that's required will be included for you in the resulting template files.

Avail answered 13/5, 2011 at 17:16 Comment(0)
G
0

When I've gotten an error like that I was able to fix it by entering that following text in the linker, section Advance, option Entry Point the following:

main

Godown answered 13/5, 2011 at 17:18 Comment(2)
That did solve the error warning, but not it crashed and brought me to atonexit.cDunigan
I'm surprised it worked at all. AFAIK that setting just refers to the name of the entrypoint method, but doesn't change the signature at all. You're basically calling ()(HINSTANCE, HINSTANCE, LPSTR, int) as ()(char**, int), which is almost certainly going to play hell with your call stack when that method returns.Greengage
H
0

Here is the detailed instruction how to solve it under Visual Studio without rewriting your code:

  1. Open project Properties window
  2. Leave the Debug configuration as is, console is great for debugging
  3. Under Release configuration go to Linker->All Options
  4. Set Entry Point to mainCRTStartup
  5. Set SubSystem to Windows: /SUBSYSTEM:WINDOWS
  6. Hit Apply to save setting

Now when you compile your project under Debug configuration, the console window appears, while under Release configuration it never opens. You can use the entry point function in any common forms:

int main() {}

int main(int argc, char** argv) {}

I personally prefer the second. Thanks to Chao and elmindreda, I upvoted you guys!

Hydroquinone answered 30/8, 2023 at 22:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.