C++ 恼人的VS2013链接器错误(Win32API)…为什么?

C++ 恼人的VS2013链接器错误(Win32API)…为什么?,c++,winapi,visual-studio-2013,C++,Winapi,Visual Studio 2013,我已经启动了一个新的VS2013 Win32API项目,并添加了一些简单的绘图菜单项和代码: LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; HPEN hPen; HPEN hOldPen; HBRUSH hBrush; HBRUSH hOldBrush; switch (message) {

我已经启动了一个新的VS2013 Win32API项目,并添加了一些简单的绘图菜单项和代码:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
HPEN hPen;
HPEN hOldPen;
HBRUSH hBrush;
HBRUSH hOldBrush;

switch (message)
{
case WM_COMMAND:
    wmId    = LOWORD(wParam);
    wmEvent = HIWORD(wParam);
    // Parse the menu selections:
    switch (wmId)
    {
    case IDM_ABOUT:
        DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
        break;
    case IDM_EXIT:
        DestroyWindow(hWnd);
        break;
    // Need to randomize x,y coordinates
    case ID_DRAW_CIRCLE:
        /* draw a blue-bordered magenta-crosshatched circle */
        hdc = GetDC(hWnd);                 /* get a DC for painting */
        hPen = CreatePen(PS_SOLID, 3, RGB(0, 0, 255));  /* blue pen */
        hBrush = CreateHatchBrush(HS_DIAGCROSS, RGB(255, 0, 255));
        hOldPen = (HPEN)SelectObject(hdc, hPen);      /* select into DC & */
        hOldBrush = (HBRUSH)SelectObject(hdc, hBrush); /* save old object */
        Ellipse(hdc, 100, 30, 180, 110);       /* draw circle */
        SelectObject(hdc, hOldBrush);          /* displace brush */
        DeleteObject(hBrush);                  /* delete brush */
        SelectObject(hdc, hOldPen);            /* same for pen */
        DeleteObject(hPen);
        ReleaseDC(hWnd, hdc);   /* release the DC to end painting */
        break; 
    case ID_DRAW_RECTANGLE:
        /* draw a red-bordered, cyan-filled rectangle */
        hdc = GetDC(hWnd);                /* get a DC for painting */
        hPen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0));   /* red pen */
        hBrush = CreateSolidBrush(RGB(0, 255, 255));  /* cyan brush */
        hOldPen = (HPEN)SelectObject(hdc, hPen);      /* select into DC & */
        hOldBrush = (HBRUSH)SelectObject(hdc, hBrush); /* save old object */
        Rectangle(hdc, 15, 15, 80, 60);        /* draw rectangle */
        SelectObject(hdc, hOldBrush);          /* displace new brush */
        DeleteObject(hBrush);                  /* delete it from DC */
        SelectObject(hdc, hOldPen);            /* same for pen */
        DeleteObject(hPen);
        ReleaseDC(hWnd, hdc);                   /* get rid of DC */
        break; 
    case ID_DRAW_CLEARSCREEN:
        hdc = GetDC(hWnd); //NULL gets whole screen
        hBrush = CreateSolidBrush(RGB(255, 0, 0)); //create brush
        SelectObject(hdc, hBrush); //select brush into DC
        Rectangle(hdc, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)); //draw rectangle over whole screen
        break; 
    case ID_DRAW_QUIT:
        DestroyWindow(hWnd);
        break; 
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    break;
case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    // TODO: Add any drawing code here...
    EndPaint(hWnd, &ps);
    break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
没什么疯狂的,但是当我编译这段代码时,我得到了大约30个未解决的符号错误:

Error   1   error LNK2019: unresolved external symbol __imp__LoadStringW@16 referenced in function _wWinMain@16 C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   2   error LNK2019: unresolved external symbol __imp__CreateHatchBrush@8 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   3   error LNK2019: unresolved external symbol __imp__CreatePen@12 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)   C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj    Win32Draw
Error   4   error LNK2019: unresolved external symbol __imp__CreateSolidBrush@4 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   5   error LNK2019: unresolved external symbol __imp__DeleteObject@4 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   6   error LNK2019: unresolved external symbol __imp__Ellipse@20 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   7   error LNK2019: unresolved external symbol __imp__Rectangle@20 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)   C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   8   error LNK2019: unresolved external symbol __imp__SelectObject@8 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   9   error LNK2019: unresolved external symbol __imp__GetMessageW@16 referenced in function _wWinMain@16 C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   10  error LNK2019: unresolved external symbol __imp__TranslateMessage@4 referenced in function _wWinMain@16 C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   11  error LNK2019: unresolved external symbol __imp__DispatchMessageW@4 referenced in function _wWinMain@16 C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   12  error LNK2019: unresolved external symbol __imp__DefWindowProcW@16 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)  C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   13  error LNK2019: unresolved external symbol __imp__PostQuitMessage@4 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)  C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   14  error LNK2019: unresolved external symbol __imp__RegisterClassExW@4 referenced in function "unsigned short __cdecl MyRegisterClass(struct HINSTANCE__ *)" (?MyRegisterClass@@YAGPAUHINSTANCE__@@@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   15  error LNK2019: unresolved external symbol __imp__CreateWindowExW@48 referenced in function "int __cdecl InitInstance(struct HINSTANCE__ *,int)" (?InitInstance@@YAHPAUHINSTANCE__@@H@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   16  error LNK2019: unresolved external symbol __imp__DestroyWindow@4 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)    C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   17  error LNK2019: unresolved external symbol __imp__ShowWindow@8 referenced in function "int __cdecl InitInstance(struct HINSTANCE__ *,int)" (?InitInstance@@YAHPAUHINSTANCE__@@H@Z)   C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   18  error LNK2019: unresolved external symbol __imp__DialogBoxParamW@20 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   19  error LNK2019: unresolved external symbol __imp__EndDialog@8 referenced in function "int __stdcall About(struct HWND__ *,unsigned int,unsigned int,long)" (?About@@YGHPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   20  error LNK2019: unresolved external symbol __imp__LoadAcceleratorsW@8 referenced in function _wWinMain@16    C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   21  error LNK2019: unresolved external symbol __imp__TranslateAcceleratorW@12 referenced in function _wWinMain@16   C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   22  error LNK2019: unresolved external symbol __imp__GetSystemMetrics@4 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   23  error LNK2019: unresolved external symbol __imp__UpdateWindow@4 referenced in function "int __cdecl InitInstance(struct HINSTANCE__ *,int)" (?InitInstance@@YAHPAUHINSTANCE__@@H@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   24  error LNK2019: unresolved external symbol __imp__GetDC@4 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)    C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   25  error LNK2019: unresolved external symbol __imp__ReleaseDC@8 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)    C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   26  error LNK2019: unresolved external symbol __imp__BeginPaint@8 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z)   C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   27  error LNK2019: unresolved external symbol __imp__EndPaint@8 referenced in function "long __stdcall WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@@YGJPAUHWND__@@IIJ@Z) C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   28  error LNK2019: unresolved external symbol __imp__LoadCursorW@8 referenced in function "unsigned short __cdecl MyRegisterClass(struct HINSTANCE__ *)" (?MyRegisterClass@@YAGPAUHINSTANCE__@@@Z)  C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   29  error LNK2019: unresolved external symbol __imp__LoadIconW@8 referenced in function "unsigned short __cdecl MyRegisterClass(struct HINSTANCE__ *)" (?MyRegisterClass@@YAGPAUHINSTANCE__@@@Z)    C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\Win32Draw.obj   Win32Draw
Error   30  error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup   C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Win32Draw\MSVCRTD.lib(crtexe.obj) Win32Draw
Error   31  error LNK1120: 30 unresolved externals  C:\Users\Jonathan\Documents\Visual Studio 2013\Projects\Win32Draw\Debug\Win32Draw.exe   Win32Draw

这是怎么回事

解决问题的最简单方法可能是创建一个新的Visual Studio项目,在该项目中,您可以创建一个GUI项目

这将添加最常见的库

一个更通用的解决方案是负责管理事务,将您需要的每个库添加到链接器设置中。Microsoft的文档名为MSDN Library,可在线或下载本地帮助,其中列出了每个函数所需的标题和库。您只需在将来遇到此类错误时阅读文档即可

关于

“未解析的外部符号\u主引用”


…请注意,没有必要或理由使用Microsoft的非标准启动函数WinMain或tWinMain。使用标准的C++主函数。对于GUI子系统,使用Microsoft的工具,您必须指定入口点mainCRTStartup,因为默认情况下,Microsoft的工具是非标准的供应商锁定等等。

您需要包括user32.lib、gdi32.lib和kernel32.lib,并与之链接。也许还有一两个,但这些涵盖了我在你的清单上看到的大部分内容。哦,您还需要一个名为WinMain的函数。一句话:你需要一本关于Windows编程的入门书来告诉你如何开始。检查每个函数的文档。查找需要链接的库。将其添加到库中。最可能的原因是您创建了一个控制台程序项目。这是由缺少的main表示的,您可能已经定义了Microsoft Monstrosy WinMain,而不是:定义一个标准main,并在更改为GUI子系统时将入口点设置为mainCRTStartup。这一切都是反向的。使用Visual Studio附带的CRT时,不能更改“启动”函数的名称或签名。它已硬编码到mainCRTStartup中。如果您不想使用CRT,您可以随意命名您的入口点,包括main。微软的工具并不能阻止这一点。另外,当你包括你不再关心便携性。详细信息:.@IInspectable在这里的声明在技术上没有意义。例如,它被硬编码到mainCRTStartup中,这虽然微不足道,但也毫无意义;有四个这样的CRT入口点函数,每个函数调用不同的硬接线启动函数,例如wmainCRTStartup调用wmain。他似乎在反对什么东西,但没有指称;他的链接似乎支持某些论点,但事实并非如此;所以这都是空话,有点欺骗。当使用Visual Studio附带的CRT时,您不应该仅仅因为能够使用main而将GUI应用程序的入口点更改为mainCRTStartup。这样做会产生一个行为不好的应用程序。它将不遵守初始窗口显示模式,无法正确初始化错误报告,并且无法设置正确的应用程序类型。应用程序类型由CRT内部使用;不匹配有着未知的含义。