LNK2019未解析的外部符号user32.lib

时间:2012-07-30 15:32:55

标签: visual-c++

我不知道为什么会收到此错误:

test_project.obj:错误LNK2019:未解析的外部符号“int __cdecl run(void)”(?run @@ YAHXZ)在函数_WinMain @ 16中引用

代码如下:

            #include "stdafx.h"
            #include "test_project.h"
            #include <Windows.h>

            HWND ghMainWnd = 0;

            bool InitWindowsApp (HINSTANCE instanceHandle, int show);

            int run();

            LRESULT CALLBACK
            WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

            int WINAPI
            WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nShowCmd) {
                if (!InitWindowsApp (hInstance, nShowCmd) )
                    return 0;
                return run();
            }

            bool InitWindowsApp (HINSTANCE instanceHandle, int show) {
                WNDCLASS wc;
                wc.style        = CS_HREDRAW | CS_VREDRAW;
                wc.lpfnWndProc  = WndProc;
                wc.cbClsExtra   = 0;
                wc.cbWndExtra   = 0;
                wc.hInstance    = instanceHandle;
                wc.hIcon        = LoadIcon( 0, IDI_APPLICATION );
                wc.hCursor      = LoadCursor( 0 , IDC_ARROW );
                wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
                wc.lpszMenuName = 0;
                wc.lpszClassName = L"BasicWndClass";

                if (!RegisterClass(&wc) ) {
                    MessageBox(0, L"RegisterClass FAILED", 0, 0);
                    return false;
                }

                ghMainWnd = CreateWindow (
                    L"BasicWndClass",
                    L"Win32Basic",
                    WS_OVERLAPPEDWINDOW,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    CW_USEDEFAULT,
                    0,
                    0,
                    instanceHandle,
                    0);

                if (ghMainWnd == 0) {
                    MessageBox ( 0, L"CreateWindow FAILED", 0, 0);
                    return false;
                }
                ShowWindow (ghMainWnd, show);
                UpdateWindow (ghMainWnd);

                return true;
            }

            int Run() {
                MSG msg = {0};

                BOOL bRet = 1;
                while ((bRet = GetMessage(&msg, 0, 0, 0)) != 0) {
                    if (bRet == -1)
                    {
                        MessageBox(0, L"GetMessage FAILED", L"Error", MB_OK);
                        break;
                    }
                    else {
                        TranslateMessage(&msg);
                        DispatchMessage(&msg);
                    }
                }
                return (int)msg.wParam;
            }

            LRESULT CALLBACK
            WndProc ( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
                switch(msg) {
                case WM_LBUTTONDOWN:
                    MessageBox(0, L"Hello, World", L"Hello", MB_OK);
                    return 0;
                case WM_KEYDOWN:
                    if (wParam == VK_ESCAPE)
                        DestroyWindow(ghMainWnd);
                    return 0;
                case WM_DESTROY:
                    PostQuitMessage(0);
                    return 0;
                }

                return DefWindowProc (hWnd, msg, wParam, lParam);
            }

在属性页面 - &gt; c / c ++ - &gt; general-&gt;其他包含目录我已经放置了包含lib的目录:C:\ Program Files \ Microsoft SDKs \ Windows \ v7.1 \ Lib

在属性页面中 - >链接器 - >输入 - >其他依赖项我已经将完整路径放到了lib C:\ Program Files \ Microsoft SDKs \ Windows \ v7.1 \ Lib \ user32.lib

在属性页面 - &gt;链接器 - &gt; system-&gt;子系统中我放了Windows(/ SUBSYSTEM:WINDOWS)

我不知道下一步该尝试什么。

1 个答案:

答案 0 :(得分:1)

C ++是case-sensitive。您必须决定是否为您的函数run()Run()命名。

int run();

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   PSTR pCmdLine, int nShowCmd) {
    if (!InitWindowsApp (hInstance, nShowCmd) )
        return 0;
    return run();  // <-- There.
}

对战:

int Run() {
    // ...
}