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

时间:2014-10-02 21:46:03

标签: 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)
{
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

这里发生了什么?!?!

1 个答案:

答案 0 :(得分:0)

解决问题的最简单方法可能是创建一个新的Visual Studio项目,您需要注意创建一个 GUI 项目。

这将添加最常见的库。

更通用的解决方案是负责工作,将每个库添加到链接器设置中。 Microsoft的文档称为 MSDN Library ,可在线获得,也可作为可下载的本地帮助,列出了每个函数所需的标头和库。您只需要在将来遇到此类错误时阅读文档。


关于

  

unresolved external symbol _main referenced

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