I have making an attempt at writing my first program in Visual Studio, however am being troubled by an error. It says: -
Error 3 error LNK2019: unresolved external symbol _wWinMain@16 referenced in function ___tmainCRTStartup
E:\Documents\Programming\Software Development\Microsoft Development\Microsoft Development\MSVCRTD.lib(wcrtexew.obj)
Microsoft Development
On researching I found similar errors, but none have helped me solve the problem. I have changed the entry point to
wWinMainCRTStartup
the character set to Unicode
the subsystem to console. The project is a win32 application. The code is as follows: -
#include <windows.h>
#include <stdio.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, "Hello World!", "Note", 1/*MB_OK*/);
printf("nCmdShow = %d\n", nCmdShow);
return 0;
}
How do I fix this issue?
P.S. I am using Visual Studio Ultimate 2013
For a Unicode build your code needs to be more like this:
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, L"Hello World!", L"Note", 1/*MB_OK*/);
printf(L"nCmdShow = %d\n", nCmdShow);
return 0;
}
At least by default this will be set to use the Windows subsystem (because the entry point is named a variant of WinMain). You can force that to the console subsystem (-subsystem:console
flag to the linker) or get it to happen by default by changing the entry point to a variant of main
instead:
int wmain(int argc, wchar_t **argv) { // ...
Obviously you won't be able to print nCmdShow
using this though (not that it really means anything in a console program). For that matter, since you're not using the command line arguments anyway, you can simplify this somewhat to:
int wmain(){ // ....
Actually, nCmdShow
is basically obsolete even for windowed programs. The first time a windowed program calls ShowWindow
, it normally passes nCmdShow
as the parameter. Windows, in turn, ignores the value passed in the first call to ShowWindow
, and instead uses the value from the process' STARTUPINFO
structure. Only in subsequent calls to ShowWindow
is the parameter used (and for these subsequent calls, you're not supposed to pass nCmdShow
either--you're supposed to pass one of the defined constants such as SW_SHOWNORMAL
).
Reference: MSDN entry for ShowWindow