0

I'm using the msvc compiler to create windows. A link error is occurring, but the problem is difficult to solve because I am not familiar with vscode.

#include <windows.h>

#pragma comment(linker, "/entry:wWinMainCRTStartup /subsystem:console")

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PWSTR pCmdLine, int nCmdShow) {

    MSG msg;
    HWND hwnd;
    WNDCLASSW wc;

    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.lpszClassName = L"Window";
    wc.hInstance     = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpszMenuName  = NULL;
    wc.lpfnWndProc   = WndProc;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);

    RegisterClassW(&wc);
    hwnd = CreateWindowW(wc.lpszClassName, L"Window",
                WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                100, 100, 350, 250, NULL, NULL, hInstance, NULL);

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while (GetMessage(&msg, NULL, 0, 0)) {

        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
    WPARAM wParam, LPARAM lParam) {

    switch(msg) {

      case WM_DESTROY:

          PostQuitMessage(0);
          break;
    }

    return DefWindowProcW(hwnd, msg, wParam, lParam);
}


I have confirmed that a similar error occurs when compiling without saving when using g++, but in my case it is msvc. In the case of msvc, it is being used through Developer power shell for 2019.

1 Answers1

0

You link with the console sybsystem option but you are missing the main function. Console apps start with a main.

Either create a main function or link with the 'Windows' subsystem to start with a WinMain.

Perhaps the /entry:wWinMainCRTStartup is not needed at all.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
  • So, how should I convert the subsystem to windows? – backpack Mar 13 '23 at 15:46
  • In the Visual Studio environment, this code also works fine in console mode. I'm guessing it's probably because I changed the entry point to winmain via the pragma commet. – backpack Mar 13 '23 at 15:48
  • @backpack Setting the entry point to *your* function is bypassing the CRT's startup code (that, incidentally, does **a lot**). Best not to change that, ever. As far as the `#pragma`-directive goes, none of the options supplied are [supported](https://learn.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp#linker). You would need to explicitly pass the `/SUBSYSTEM:WINDOWS` option to the linker. The manual for your build environment should explain how to set that up. Alternatively, use Visual Studio, and get a debugger that's actually useful as a bonus. – IInspectable Mar 14 '23 at 15:23