Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Visual C++: Windows and Console Subsystems

📅 2010-Dec-02 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

 

Subsystem

The Subsystem option can be found in any Visual C++ project here: Project → Properties → Linker → System → Subsystem. The choice of Subsystem decides the execution environment for the executable that is compiled from the project. For a Console application, choose /SUBSYSTEM:CONSOLE. For a Windows (GUI) application, choose /SUBSYSTEM:WINDOWS. There are other Subsystems, but these two are the most common.

Entry Point Function

The entry point function of the executable is decided based on the Subsystem. A Console executable begins from a main() function, so this should be present somewhere in the project code. A Windows executable begins from a WinMain() function, so this should be defined. If a windowing library (like wxWidgets for example) is being used, that library code would typically have the WinMain call defined inside.

Preprocessor Definitions

In older Visual C++ environments, the preprocessor definitions _CONSOLE and _WINDOWS corresponded to the above 2 subsystems. They are still defined by Visual C++ when it creates a project. These definitions are not really needed now. But, there might be header files of external libraries that you use, which might look out for these definitions. So, it might be a good idea to not delete them if you face errors.

Switching Subsystems

To switch a Visual C++ project from Console to Windows or vice versa, both the Subsystem and the Preprocessor Definitions need to be changed.

Errors

A project has a mismatched Subsystem when its code is written for Console and its Subsystem is set to Windows or vice versa. This manifests itself in errors like:

MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

To fix this, make sure the Subsystem matches the code, else switch it.