title: (C++) Win32 API sample code returning "undefined reference to `WinMain@16'" using g++
body: <p>I am using the G++ compiler with notepad++.</p>
  <p>I am following a tutorial by windows, the code that I am using is the example code from the windows tutorial (<a href="https://docs.microsoft.com/en-us/windows/win32/learnwin32/your-first-windows-program" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/windows/win32/learnwin32/your-first-windows-program</a>) I pasted this into my notepad and attempted to compile it, but I was greeted with an error message.</p>
  <p>Here is the code and the message:</p>
  <pre><code>#ifndef UNICODE
  #define UNICODE
  #endif 
  
  #include &lt;windows.h&gt;
  
  LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  
  int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
  {
      // Register the window class.
      const wchar_t CLASS_NAME[]  = L&quot;Sample Window Class&quot;;
      
      WNDCLASS wc = { };
  
      wc.lpfnWndProc   = WindowProc;
      wc.hInstance     = hInstance;
      wc.lpszClassName = CLASS_NAME;
  
      RegisterClass(&amp;wc);
  
      // Create the window.
  
      HWND hwnd = CreateWindowEx(
          0,                              // Optional window styles.
          CLASS_NAME,                     // Window class
          L&quot;Learn to Program Windows&quot;,    // Window text
          WS_OVERLAPPEDWINDOW,            // Window style
  
          // Size and position
          CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  
          NULL,       // Parent window    
          NULL,       // Menu
          hInstance,  // Instance handle
          NULL        // Additional application data
          );
  
      if (hwnd == NULL)
      {
          return 0;
      }
  
      ShowWindow(hwnd, nCmdShow);
  
      // Run the message loop.
  
      MSG msg = { };
      while (GetMessage(&amp;msg, NULL, 0, 0))
      {
          TranslateMessage(&amp;msg);
          DispatchMessage(&amp;msg);
      }
  
      return 0;
  }
  
  LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  {
      switch (uMsg)
      {
      case WM_DESTROY:
          PostQuitMessage(0);
          return 0;
  
      case WM_PAINT:
          {
              PAINTSTRUCT ps;
              HDC hdc = BeginPaint(hwnd, &amp;ps);
  
  
  
              FillRect(hdc, &amp;ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
  
              EndPaint(hwnd, &amp;ps);
          }
          return 0;
  
      }
      return DefWindowProc(hwnd, uMsg, wParam, lParam);
  }
  </code></pre>
  <p>This is the command I used to compile (powershell)</p>
  <pre><code>PS  g++ main.cpp
  </code></pre>
  <p>And this is the message I get when trying to compile (powershell)</p>
  <blockquote>
  <p>c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
  collect2.exe: error: ld returned 1 exit status</p>
  </blockquote>
  <p>if I try renaming <code>wWinMain</code> to <code>WinMain</code> it gives the error</p>
  <blockquote>
  <p>main.cpp:9:12: error: conflicting declaration of C function 'int WinMain(HINSTANCE, HINSTANCE, PWSTR, int)'  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)<br />
  In file included from c:\mingw\include\windows.h:44:0,                  from main.cpp:5: c:\mingw\include\winbase.h:1263:14: note: previous declaration 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)'  int APIENTRY WinMain (HINSTANCE, HINSTANCE, LPSTR, int);</p>
  </blockquote>
  <p>and if I try to use the command <code>g++ main.cpp -municode</code> then I get the message</p>
  <blockquote>
  <p>g++.exe: error: unrecognized command line option '-municode'</p>
  </blockquote>