error LNK1120: 1 unresolved external - VS13 C

时间:2016-07-11 23:15:54

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

I have tried to compile this code:

#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));
}

and it seems like this libary:

#include <commctrl.h>

giving me the following error:

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

now the code is an example code from here: http://zetcode.com/gui/winapi/advancedcontrols/

so i figured out that something wrong with my compiler... can any one help me find the problem and correct it?

edit: the question is not a duplicate because the refereed duplicate talks in a very general way about the error itself and as a beginner i dont have the skills to build a proper answer from such a question and because that code is been used by many peoples who check the example i think its impotent to make a specific question

1 个答案:

答案 0 :(得分:3)

Either add #pragma comment(lib, "comctl32.lib") or adjust the linker settings to link against comctl32.lib.

You can check the table at the bottom of a function's MSDN article to find out, which library you are required to link against. Every Windows application is linked against kernel32.dll and every GUI application against user32.dll. Anything else needs to be specified explicitly1.


1 There are exceptions, check IInspectable's comment below.