cpp文件不使用cl编译,但在Visual Studio中编译得很好

cpp File not compiling with cl, but compiles just fine inside Visual Studio

本文关键字:编译 Studio Visual 很好 但在 cl cpp 文件      更新时间:2023-10-16

我遇到了一个问题,无法使用CL(VS CMD)编译以下代码。它没有编译,而是给了我错误LN2019。在VS中编译相同的代码,编译时没有错误。

#include <windows.h>
LRESULT CALLBACK
MainWindowCallback( HWND   Window,
                    UINT   Message,
                    WPARAM WParam,
                    LPARAM LParam)
{
    LRESULT Result = 0;
    switch(Message)
    {
        case WM_SIZE:
        {
            OutputDebugStringA("WM_SIZEn");
        } break;
        case WM_DESTROY:
        {
            OutputDebugStringA("WM_DESTROYn");
        } break;
        case WM_CLOSE:
        {
            OutputDebugStringA("WM_CLOSEn");
        } break;
        case WM_ACTIVATEAPP:
        {
            OutputDebugStringA("WM_ACTIVATEAPPn");
        } break;
        default:
        {
            // OutputDebugSTringA("defaultn")
            Result = DefWindowProc(Window, Message, WParam, LParam);
        } break;
    }
    return(Result);
}
int CALLBACK
WinMain(HINSTANCE Instance,
        HINSTANCE PrevInstance,
        LPSTR     CommandLine,
        int       ShowCode)
{
    WNDCLASS WindowClass = {};
    WindowClass.style = CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
    WindowClass.lpfnWndProc = MainWindowCallback;
    WindowClass.hInstance = Instance;
    // WindowClass.hIcon;
    WindowClass.lpszClassName = "FooWindowClass";
    return(0);
}

我追踪到第36行的问题:

Result = DefWindowProc(Window, Message, WParam, LParam);

当我把这行注释出来时,文件编译得很好。用于编译的cl命令也是相当标准的:

cl -Zi Foo.cpp

我遗漏了一些cl参数吗?

您必须链接到user32.lib:

cl Foo.cpp user32.lib

错误Error LN2019没有"main"(您似乎已将其命名为WinMain)。

另请参阅:错误LNK2019:函数__mainCRTStartup 中引用的未解析外部符号_main