编程Windows编译器链接错误 - 未解析的外部

时间:2011-09-05 10:06:23

标签: windows visual-studio visual-studio-2005 linker

我是Windows编程新手。

我使用VS2005创建了一个win32控制台项目(没有预编译头)。代码如下。

// HelloWin.cpp : Defines the entry point for the console application.
//
#include <windows.h>
#include "stdafx.h"

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

int _tmain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("HelloWin");
    HWND        hwnd;
    MSG         msg;
    WNDCLASS    wndclass;

    wndclass.style          = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc    = WndProc;
    wndclass.cbClsExtra     = 0;
    wndclass.cbWndExtra     = 0;
    wndclass.hInstance      = hInstance;
    wndclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName   = NULL;
    wndclass.lpszClassName  = szAppName;

    if(!RegisterClass(&wndclass))
    {
        MessageBox(NULL, TEXT("This Program requires Windows NT!"), szAppName, MB_ICONERROR);
        return 0;

    }

    hwnd = CreateWindow(szAppName,                  // window class name
                        TEXT("The Hello Program"),  // window caption
                        WS_OVERLAPPEDWINDOW,        // window style
                        CW_USEDEFAULT,              // initial x position
                        CW_USEDEFAULT,              // initial y position
                        CW_USEDEFAULT,              // initial x size
                        CW_USEDEFAULT,              // initial y size
                        NULL,                       // parent window handle
                        NULL,                       // window menu handle
                        hInstance,                  // program instance handle
                        NULL);                      // creation parameters

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC         hdc;
    PAINTSTRUCT ps;
    RECT        rect;

    switch(message)
    {
    case WM_CREATE:
        PlaySound(TEXT("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC);
        return 0;

    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);

        GetClientRect(hwnd, &rect);

        DrawText(hdc, TEXT("Hello Windows XP"), -1, &rect, 
                 DT_SINGLELINE | DT_CENTER | DT_VCENTER);
        EndPaint(hwnd, &ps);
        return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }   
    return DefWindowProc(hwnd, message, wParam, lParam);
}

现在有两个链接错误,任何人都可以帮我解决这个错误。

是否由我的本地硬盘中没有hellowin.wav文件引起?如果是的话。我可以将一个simular WAV文件放在哪个目录中?

感谢。

  

1&gt;链接... 1&gt; HelloWin.obj:

     

错误LNK2019:未解析的外部   符号 imp__PlaySoundW @ 12在函数“long __stdcall中引用   WndProc(struct HWND *,unsigned int,unsigned int,long)“   (?@@的WndProc __ YGJPAUHWND IIJ @@ @ Z)   1 GT; d:\学习\ Windows \ ProgrammingWindows(5thEdition)\ HELLOWIN \调试\ HelloWin.exe   :

     

致命错误LNK1120:1个未解析的外部1&gt;构建日志已保存在

     

“file:// d:\ learning \ windows \ ProgrammingWindows(5thEdition)\ HelloWin \ HelloWin \ Debug \ BuildLog&gt; .htm”   1&gt; HelloWin - 2个错误,1个警告   ==========重建全部:0成功,1失败,0跳过==========

1 个答案:

答案 0 :(得分:1)

这里有几个问题。首先,您说您的应用程序是一个控制台应用程序。如果是,那么你使用错误的主。您的子系统可能是WINDOWS,在这种情况下,您的主要应该是:

int CALLBACK _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR szCmdLine, int iCmdShow)

您还说您没有使用预编译的标头,但是您有stdafx.h的包含。您可以通过更改主要内容以及更改包含内容来解决您的问题:

#include <windows.h>
#include <tchar.h>
#pragma comment(lib, "Winmm.lib")

这允许链接器查找我们告诉它的库文件。我还删除了你的预编译头文件并将其替换为tchar.h,因为到目前为止这是你唯一的其他依赖项。

编译时不存在hellowin.wav这一事实无关紧要。该程序仅在运行时查找它。

相关问题