Thread: LNK2001 Error ?

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    Question LNK2001 Error ?

    I get the LNK2001 linking error when trying to build this code in MSVC++5

    I have looked at the help files and i think it has something to do with Extern variables or something, but i dont understand why? The code compiles with 0 errors and 0 warnings

    here is the code as i cant seem to post attachements yet
    ///////////////////////////////////////////////////////////////////////////////////
    MAIN.CPP

    #include <windows.h>
    #include "generic.h"

    HINSTANCE hInst; // current instance

    LPCTSTR lpszAppName = "Generic";
    LPCTSTR lpszTitle = "Generic";

    BOOL RegisterWin95(CONST WNDCLASS* lpwc);

    int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    {
    MSG msg; //message
    HWND hWnd; //windows handler
    WNDCLASS wc;

    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = 0;
    wc.hIcon = LoadIcon(hInstance,lpszAppName);
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName = lpszAppName;
    wc.lpszClassName = lpszAppName;

    if (!RegisterWin95(&wc))
    return false;
    hInst = hInstance;
    hWnd = CreateWindow(lpszAppName,lpszTitle,
    WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,
    CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
    if (!hWnd)
    return false;
    ShowWindow(hWnd,nCmdShow);
    UpdateWindow(hWnd);
    while(GetMessage(&msg,NULL,0,0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return(msg.wParam);
    }

    BOOL RegisterWin95(CONST WNDCLASS* lpwc)
    {
    WNDCLASSEX wcex;

    wcex.style = lpwc->style;
    wcex.lpfnWndProc = lpwc->lpfnWndProc;
    wcex.cbClsExtra = lpwc->cbClsExtra;
    wcex.hInstance = lpwc->hInstance;
    wcex.hIcon = lpwc->hIcon;
    wcex.hCursor = lpwc->hCursor;
    wcex.hbrBackground = lpwc->hbrBackground;
    wcex.lpszMenuName = lpwc->lpszMenuName;
    wcex.lpszClassName = lpwc->lpszClassName;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.hIconSm = LoadIcon(wcex.hInstance,"SMALL");

    return RegisterClassEx(&wcex);
    }

    LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
    {
    switch(uMsg)
    {
    case WM_COMMAND:
    switch(LOWORD(wParam))
    {

    case IDM_TEST :
    break;
    case IDM_EXIT :
    DestroyWindow(hWnd);
    break;
    }
    break;
    case WM_DESTROY :
    PostQuitMessage(0);
    break;
    default :
    return(DefWindowProc(hWnd,uMsg,wParam,lParam));
    }
    return(0L);
    }

    ///////////////////////////////////////////////////////////////////////////////////

    Here is "generic.h"

    #define IDM_EXIT 100
    #define IDM_TEST 200
    #define IDM_ABOUT 300

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

    ////////////////////////////////////////////////////////////////////////////////////

    Please please someone try this code out in MSVC++ 5 or later and see what my problems are? Or even point me in the direction of a solution?

    Thanks for your time.

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    When you get an linker error, it normally specifies what function can't be found. Without you posting this I'm not sure where the problem is, but it's probably your project settings. Make sure your linking it as a Win32 application and not a Win32 console application.
    zen

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Compiles and links okay with VC++ 6.0 Pro on NT4.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    12
    Ok, thx adrianxw

    Hmm it works in MSCV++6 but not 5, perhaps its my project settings..

    Ill check the settings out.. im pretty sure its nothing to do with a function or code as it compiles clean.

    thx all

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Off topic, I like the name.

    kamikazeecows

    Reminds me of something I read in the news a few years back.
    A Japanese trawler had to pull into Russian harbour after serious damage nearly caused it to sink.

    Appears that they were hit by a couple of cows. Try explaining that to customs.

    Later it was revealed that some rustlers had used a cargo plane to load up a herd of cows. During the flight the cows got spooked. To save the plane the rustlers opened up the rear door and let the cows out over the ocean.

    It was a reputable news paper so could be true.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    Smile Haha nice story

    Haha thanks, glad to someone appreciates besides me

    Me and my friend made up stupid names like this for hotmail addresses and this was my one, his was something like destructivepanda, or something like that, we had loads written down somewhere, haha ill look for them sometme and post em if u wish.

    As to the problem i posted above i still havent solved it yet and its plaguing me !!!

    Ah well ill work it out soon enough...

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I'll have another look at it tomorrow.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try changing

    APIENTRY

    to

    PASCAL

    I use APIENTRY for DLL's, PASCAL for exe.

    Does the complier tell you what function / var is causing the error?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #9
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I did this, try the same...

    Open VC++

    File->New->choose workspace Tab->give it a name, (I called it abc), and click OK.

    File->New-> choose project Tab if it is not already chosen, and select "Win32 Application" - NOT Win32 Console App or any of the others->give it a name, (again I called it abc), and check the "Add to current workspace" radio button, then click OK. On the next screen select "An empty project" for the application type. If it gives you another screen saying that no files will be created just say OK.

    File->New-> choose Files Tab, if it is not already chosen, and select "C/C++ Header File" -> give it the name generic.h and click OK.

    Cut and paste these lines into the new header file...

    #define IDM_EXIT 100
    #define IDM_TEST 200
    #define IDM_ABOUT 300

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


    File->New choose Files Tab, if it is not already chosen, and select "C/C++ Source File" -> give it a name, (abc) and click OK.

    Cut and paste these lines into the new source file...

    #include <windows.h>
    #include "generic.h"

    HINSTANCE hInst; // current instance

    LPCTSTR lpszAppName = "Generic";
    LPCTSTR lpszTitle = "Generic";

    BOOL RegisterWin95(CONST WNDCLASS* lpwc);

    int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    {
    MSG msg; //message
    HWND hWnd; //windows handler
    WNDCLASS wc;

    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = 0;
    wc.hIcon = LoadIcon(hInstance,lpszAppName);
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName = lpszAppName;
    wc.lpszClassName = lpszAppName;

    if (!RegisterWin95(&wc))
    return false;
    hInst = hInstance;
    hWnd = CreateWindow(lpszAppName,lpszTitle,
    WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,
    CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
    if (!hWnd)
    return false;
    ShowWindow(hWnd,nCmdShow);
    UpdateWindow(hWnd);
    while(GetMessage(&msg,NULL,0,0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return(msg.wParam);
    }

    BOOL RegisterWin95(CONST WNDCLASS* lpwc)
    {
    WNDCLASSEX wcex;

    wcex.style = lpwc->style;
    wcex.lpfnWndProc = lpwc->lpfnWndProc;
    wcex.cbClsExtra = lpwc->cbClsExtra;
    wcex.hInstance = lpwc->hInstance;
    wcex.hIcon = lpwc->hIcon;
    wcex.hCursor = lpwc->hCursor;
    wcex.hbrBackground = lpwc->hbrBackground;
    wcex.lpszMenuName = lpwc->lpszMenuName;
    wcex.lpszClassName = lpwc->lpszClassName;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.hIconSm = LoadIcon(wcex.hInstance,"SMALL");

    return RegisterClassEx(&wcex);
    }

    LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
    {
    switch(uMsg)
    {
    case WM_COMMAND:
    switch(LOWORD(wParam))
    {

    case IDM_TEST :
    break;
    case IDM_EXIT :
    DestroyWindow(hWnd);
    break;
    }
    break;
    case WM_DESTROY :
    PostQuitMessage(0);
    break;
    default :
    return(DefWindowProc(hWnd,uMsg,wParam,lParam));
    }
    return(0L);
    }

    Now press F7...

    When I do, I get

    Compiling...
    abc.cpp
    Linking...

    abc.exe - 0 error(s), 0 warning(s)

    Try this, and tell me what happens.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    Ah thx

    Thx so much ill try out these things later when i get home, thx !

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    12

    Talking IT WORKS

    Hey Zen, Andrewxw thanks so much for the information, it WORKS !

    It was the simple case of doing what you said andrexw

    I can now go on with my project and get some work done !

    Cheers.

    Now for the tough bit....

  12. #12
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> Now for the tough bit....

    What's that, admitting that you got my name wrong?

    LOL. Good luck!!!!!!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM