14

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?

Johnathan
  • 787
  • 4
  • 10
  • 21

9 Answers9

21

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

elmindreda
  • 612
  • 4
  • 17
  • 1
    Works like a charm combined with @Emre's answer. Thanks. – Sergey.quixoticaxis.Ivanov Jul 10 '17 at 15:49
  • 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) –  Jan 20 '19 at 07:41
12

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.

Emre
  • 1,239
  • 9
  • 7
  • 5
    I tried this, but in VS2015 it gives a linker error: unresolved external symbol WinMain referenced in function "int __cdecl invoke_main(void)" – Nathan Reed Aug 01 '16 at 06:33
  • To get remove linker error: unresolved external symbol WinMain... you have to add 'mainCRTStartup' entry point: Linker->All Options->Entry Point (VS2019 Community) – Dmitry Babich Aug 30 '23 at 21:27
12

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

I think on GCC it's called -mwindows

MHD
  • 121
  • 2
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")
Chao
  • 121
  • 1
  • 2
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

Sean Edwards
  • 2,062
  • 1
  • 18
  • 19
  • 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! – Johnathan May 13 '11 at 17:34
  • 2
    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. – Sean Edwards May 13 '11 at 17:35
  • Are you saying that in Linux and Mac, they do not have a console window by default? – Johnathan May 13 '11 at 17:44
  • 1
    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(). – Sean Edwards May 13 '11 at 17:53
  • Thanks for letting me know. I will do what Sean Edwards wrote in the comment and add the #ifdef WIN32. This should do it. – Johnathan May 13 '11 at 18:02
  • It's worth noting that with `FreeConsole()`, the console window still pops up for an instant before disappearing. – Nathan Reed Aug 01 '16 at 06:31
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
){
    // ...
}
tibur
  • 11,531
  • 2
  • 37
  • 39
  • 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. – Johnathan May 13 '11 at 17:25
  • 1
    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`. – tibur May 13 '11 at 17:26
  • 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? – Johnathan May 13 '11 at 17:37
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.

onteria_
  • 68,181
  • 7
  • 71
  • 64
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

JadziaMD
  • 2,690
  • 5
  • 31
  • 42
  • That did solve the error warning, but not it crashed and brought me to atonexit.c – Johnathan May 13 '11 at 17:29
  • 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. – Sean Edwards May 13 '11 at 17:39
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!