. Advertisement .
..3..
. Advertisement .
..4..
Suppose you notice an error called undefined reference to ‘WinMain@16’ in your code, and you don’t know what to do; don’t panic yet. This guide will show you how to fix it.
Why The Error undefined reference to ‘WinMain@16’ Appears?
undefined reference to ‘Winmain@16’ error happens when a linker can’t find the function WinMain or main.
Take the below Windows API program as an example:
#define NOMINMAX
#include <windows.h>
int main()
{
MessageBox( 0, "Blah blah...", "My Windows app!", MB_SETFOREGROUND );
}
How To Fix undefined reference to ‘WinMain@16’
Let’s now build it utilizing the GNU toolchain (also known as g++) without additional options. In this case, we use gnuc, which is a batch script file. Keep in mind that it simply provides options to standardize g++:
C:\test> gnuc x.cpp
C:\test> objdump -x a.exe | findstr /i "^subsystem"
Subsystem 00000003 (Windows CUI)
C:\test> _
This indicates that your linker created a subsystem executable by default. The value of the subsystem in the header file informs Windows of the program’s service requirements. In this instance, a console window is needed by the program with the console system.
Additionally, it makes the interpreter command wait for a program to finish.
Let’s construct it using a GUI subsystem, implying that the application doesn’t need the window console.
C:\test> gnuc x.cpp -mwindows
C:\test> objdump -x a.exe | findstr /i "^subsystem"
Subsystem 00000002 (Windows GUI)
C:\test> _
Keep in mind that even yet, the flag –mwindows is only semi-documented.
Constructing without this partially described flag, you will need to inform the linker more precisely about which value you want to use. Also, some import libraries of Windows API must be supplied.
C:\test> gnuc x.cpp -Wl,-subsystem,windows
C:\test> objdump -x a.exe | findstr /i "^subsystem"
Subsystem 00000002 (Windows GUI)
C:\test> _
Microsoft toolchain (also known as Visual C++) can also work when constructed as a subsystem executable.
C:\test> msvc x.cpp user32.lib
x.cpp
C:\test> dumpbin /headers x.exe | find /i "subsystem" | find /i "Windows"
3 subsystem (Windows CUI)
C:\test> _
Still, the GUI subsystem building process using Microsoft’s toolchain does not function by default.
C:\test> msvc x.cpp user32.lib /link /subsystem:windows
x.cpp
LIBCMT.lib(wincrt0.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartu
p
x.exe : fatal error LNK1120: 1 unresolved externals
C:\test> _
This is due to Microsoft’s linker being non-standard for GUI subsystems by default. While GUI is your subsystem, by default, an entry point of the runtime library is utilized by Microsoft’s linker. Once the code execution begins, the function winMainCRTStartup triggers the WinMain non-standard rather than the main standard in Microsoft.
Notice that you don’t need to fix this replacement.
Now, instruct the linker in Microsoft to utilize mainCRTStartup, which triggers the main standard.
C:\test> msvc x.cpp user32.lib /link /subsystem:windows /entry:mainCRTStartup
x.cpp
C:\test> dumpbin /headers x.exe | find /i "subsystem" | find /i "Windows"
2 subsystem (Windows GUI)
C:\test> _
Furthermore, it is so obscure that the majority of Windows developers, who primarily only use non-standard tools of Windows, are unaware of it and wrongly believe that the subsystem program of Windows GUI utilizes WinMain non-standard in place of the main standard.
As a side note, Microsoft will run into issues with this with C++0x, as the compiler will need to advertise whether it is hosted or free-standing. If it is hosted, it has to support the main standard.
Because of this, g++ can lament the absence of WinMain. Still, as you can tell, g++ can work quite well with the main standard, even for programs that are part of a GUI Windows subsystem.
In this case, you most likely don’t have a main and WinMain. Thus, after looking for a WinMain and main, which aren’t there, g++ indicates that the former is absent.
Now, let’s try with this empty source.
C:\test> type nul >y.cpp
C:\test> gnuc y.cpp -mwindows
c:/program files/mingw/bin/../lib/gcc/mingw32/4.4.1/../../../libmingw32.a(main.o):main.c:(.text+0xd2): undefined referen
ce to `WinMain@16'
collect2: ld returned 1 exit status
C:\test> _
The Bottom Line
Now you know why the error undefined reference to ‘WinMain@16’ occurs and how to fix it. Put the methods above into practice, and your problem will soon be solved.If you encounter other issues, such as “NameError: name ‘raw_input’ is not defined in Python”, check out this guide. We also provide solutions for many problems on our site, so visit it now.
Leave a comment