c - 错误 LNK1120 : 1 unresolved external - VS13 C

标签 c visual-studio winapi visual-c++ unresolved-external

<分区>

我试过编译这段代码:

#include <windows.h>
#include <commctrl.h>


#define ID_TABCTRL 1
#define ID_EDIT 2
#define BTN_ADD 3
#define BTN_DEL 4
#define BTN_CLR 5
#define MAX_TAB_LEN 15

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

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PWSTR pCmdLine, int nCmdShow) {

    MSG  msg;
    WNDCLASSW wc = { 0 };
    wc.lpszClassName = L"Tab control";
    wc.hInstance = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc = WndProc;
    wc.hCursor = LoadCursor(0, IDC_ARROW);

    RegisterClassW(&wc);
    CreateWindowW(wc.lpszClassName, L"Tab control",
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        100, 100, 380, 230, 0, 0, hInstance, 0);

    while (GetMessage(&msg, NULL, 0, 0)) {

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

WINCOMMCTRLAPI BOOL WINAPI InitCommonControlsEx(_In_ const INITCOMMONCONTROLSEX *picce);

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
    WPARAM wParam, LPARAM lParam) {

    TCITEMW tie;
    wchar_t text[4];
    LRESULT count, id;
    INITCOMMONCONTROLSEX icex;

    switch (msg) {

    case WM_CREATE:

        icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
        icex.dwICC = ICC_TAB_CLASSES;
        InitCommonControlsEx(&icex);

        hTab = CreateWindowW(WC_TABCONTROLW, NULL, WS_CHILD | WS_VISIBLE,
            0, 0, 200, 150, hwnd, (HMENU)ID_TABCTRL, NULL, NULL);

        hEdit = CreateWindowW(WC_EDITW, NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
            250, 20, 100, 25, hwnd, (HMENU)ID_EDIT, NULL, NULL);

        SendMessage(hEdit, EM_SETLIMITTEXT, MAX_TAB_LEN, 0);

        CreateWindowW(WC_BUTTONW, L"Add", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            250, 50, 100, 25, hwnd, (HMENU)BTN_ADD, NULL, NULL);

        CreateWindowW(WC_BUTTONW, L"Delete", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            250, 80, 100, 25, hwnd, (HMENU)BTN_DEL, NULL, NULL);

        CreateWindowW(WC_BUTTONW, L"Clear", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            250, 110, 100, 25, hwnd, (HMENU)BTN_CLR, NULL, NULL);
        break;

    case WM_COMMAND:

        switch (LOWORD(wParam)) {

        case BTN_ADD:

            GetWindowTextW(hEdit, text, 250);

            if (lstrlenW(text) != 0) {

                tie.mask = TCIF_TEXT;
                tie.pszText = text;
                count = SendMessageW(hTab, TCM_GETITEMCOUNT, 0, 0);
                SendMessageW(hTab, TCM_INSERTITEMW, count,
                    (LPARAM)(LPTCITEM)&tie);
            }
            break;

        case BTN_DEL:

            id = SendMessageW(hTab, TCM_GETCURSEL, 0, 0);

            if (id != -1) {

                SendMessageW(hTab, TCM_DELETEITEM, 0, id);
            }
            break;

        case BTN_CLR:

            SendMessageW(hTab, TCM_DELETEALLITEMS, 0, 0);
            break;
        }
        break;

    case WM_DESTROY:

        PostQuitMessage(0);
        break;
    }

    return(DefWindowProcW(hwnd, msg, wParam, lParam));
}

看起来像这个库:

#include <commctrl.h>

给我以下错误:

error LNK2019: unresolved external symbol        __imp__InitCommonControlsEx@4 referenced in function _WndProc@16   C:\Users\User\Desktop\Magshimim\Magshimim EX1\Magshimim EX1\01.obj  Magshimim EX1

现在代码是来自此处的示例代码:http://zetcode.com/gui/winapi/advancedcontrols/

所以我发现我的编译器有问题... 谁能帮我找到问题并改正?

编辑: 这个问题不是重复的,因为被引用的重复以非常笼统的方式谈论错误本身,作为一个初学者,我没有能力从这样的问题中建立一个正确的答案,因为这个代码被许多检查过的人使用这个例子我认为它不能提出一个具体的问题

最佳答案

添加 #pragma comment(lib, "comctl32.lib") 或调整链接器设置以链接到 comctl32.lib

您可以查看函数底部的表格 MSDN article找出您需要链接到哪个库。每个 Windows 应用程序都链接到 kernel32.dll,每个 GUI 应用程序都链接到 user32.dll。其他任何内容都需要明确指定 1 .


1有异常(exception),查IInspectable's comment below .

关于c - 错误 LNK1120 : 1 unresolved external - VS13 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38317651/

相关文章:

c - 插入排序的错误输出

c# - C 中的 allocVector() 和 System.AccessViolationException : Attempted to read or write protected memory?

c - UDP 套接字超时循环的内存错误

c# - C# 中的异步等待和任务

visual-studio - 防止 Visual Studio 加密文件

c++ - 如何将焦点重新集中在透明(启用点击)窗口上

c - 打印数组 C 时使用字符数组而不是字符名称

visual-studio - VS2012 Web Essentials 中的 TypeScript 错误通知

c++ - Windows 8 上的 DLL LoadCount

delphi - 确定字符是否可打印