VC:ConsoleWindows

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

Contents

The Problem

As you may know, WinMain() is the entry point for most apps built in Visual C++. This leads to portability issues right from the start of our code listing. In order to use the standard main() as the start point in Visual C++, we usually tell the compiler that we're building a console application. The problem with this approach is that there's a nasty DOS box/console window lurking around whenever our app is running.

The Solution

In order to remove the console window, we must tell the compiler that we don't need console output. This is achieved by adding the following linker command line option.

/SUBSYSTEM:WINDOWS

This tells Visual C++ that our program doesn't require a console window. However, the linker now assumes that it's looking for WinMain() as it's start point. If you compile your code you'll get an error something like this:

error LNK2019: unresolved external symbol _WinMain@16 referenced in function _WinMainCRTStartup

The fix for this is to add an additional switch:

/ENTRY:mainCRTStartup

This tells the compiler that main() is the start point for our app.

Adding the options

Visual C++ 6 .0
Select Settings from the Project Menu,
Select the Link tab on the Settings Dialog,
Enter "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup" in the Project Options list.

Visual C++.NET
Select <ProjectName> Properties from the Project menu,
Open the Linker folder in the Configuration Properties list,
Select the Command Line option under Linker,
Enter "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup" in the Additional Options field.

A Nice Side Effect

If we reverse the linker options to:

/SUBSYSTEM:CONSOLE
/ENTRY:WinMainCRTStartup

We can add a console window to a native Windows application. Very handy for debugging. To remove the console window simply delete the linker options or change them to:

/SUBSYSTEM:WINDOWS
/ENTRY:WinMainCRTStartup

Enjoy :)