Newbie Linking Error

Discussion in 'C++' started by Jeff Schwab, Dec 29, 2003.

  1. Jeff Schwab

    Jeff Schwab Guest

    LuCk wrote:
    > I compile a program and there are no compiling errors....but when i go to
    > build the program or execute it, i get the following error:
    >
    > ----------------------------------------------------------
    > Linking...
    > LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    > Debug/Skeleton.exe : fatal error LNK1120: 1 unresolved externals
    > Error executing link.exe.
    > ----------------------------------------------------------
    >
    > I think it has something to do with including the skeleton.ico and the
    > skeleton_sm.ico files(icon files)
    > but i really dont know. The icon files are in the same directory as the
    > skeleton.cpp file.



    What would make you think this error had to do with icon files? The
    linker is telling you your main function has not been defined, not some
    data file.

    I guess you're using Windows. Can you compile from a command line, or
    do you have only a GUI available?
    Jeff Schwab, Dec 29, 2003
    #1
    1. Advertising

  2. Jeff Schwab

    LuCk Guest

    I compile a program and there are no compiling errors....but when i go to
    build the program or execute it, i get the following error:

    ----------------------------------------------------------
    Linking...
    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/Skeleton.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    ----------------------------------------------------------

    I think it has something to do with including the skeleton.ico and the
    skeleton_sm.ico files(icon files)
    but i really dont know. The icon files are in the same directory as the
    skeleton.cpp file.
    LuCk, Dec 29, 2003
    #2
    1. Advertising

  3. Jeff Schwab

    Jack Klein Guest

    On Mon, 29 Dec 2003 06:54:24 GMT, "LuCk" <> wrote
    in comp.lang.c++:

    > I compile a program and there are no compiling errors....but when i go to
    > build the program or execute it, i get the following error:
    >
    > ----------------------------------------------------------
    > Linking...
    > LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    > Debug/Skeleton.exe : fatal error LNK1120: 1 unresolved externals
    > Error executing link.exe.
    > ----------------------------------------------------------
    >
    > I think it has something to do with including the skeleton.ico and the
    > skeleton_sm.ico files(icon files)
    > but i really dont know. The icon files are in the same directory as the
    > skeleton.cpp file.


    All standard C++ programs begin execution at a function provided by
    the program named main() that returns an int. Your linker is
    complaining that you did not provide such a function, so it is unable
    to make a valid C++ program.

    If you are making some platform specific program that does not start
    with a function named main() that returns an int, it is not standard
    C++ and is off-topic here. You need to ask in a group that supports
    your compiler and platform.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Dec 30, 2003
    #3
  4. Jeff Schwab

    LuCk Guest

    Thankyou for your help so far.....i thought the error was in the linker
    because it says Linking...but i guess not. Also i figured nothing was wrong
    cause i copied it straight from Teach Yourself Game Programming in 24 Hours.

    Here is the code of the .cpp file (Skeleton.cpp)
    ----------------------------------------------------------


    #include "Skeleton.h"

    //-----------------------------------------------------------------
    // Global Function Declarations
    //-----------------------------------------------------------------
    LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM
    lParam);

    //-----------------------------------------------------------------
    // Global Functions
    //-----------------------------------------------------------------

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR szCmdLine, int iCmdShow)
    {
    static TCHAR szAppName[] = TEXT("Skeleton");
    WNDCLASSEX wndclass;
    HWND hWindow;
    MSG msg;

    // Create the window class for the main window
    wndclass.cbSize = sizeof(wndclass);
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(hInstance,
    MAKEINTRESOURCE(IDI_SKELETON));
    wndclass.hIconSm = LoadIcon(hInstance,
    MAKEINTRESOURCE(IDI_SKELETON_SM));
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    // Register the window class
    if (!RegisterClassEx(&wndclass))
    return 0;

    // Create the window
    hWindow = CreateWindow(szAppName, szAppName, WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance,
    NULL);

    // Show and update the window
    ShowWindow(hWindow, iCmdShow);
    UpdateWindow(hWindow);

    // Enter the main message loop
    while (GetMessage(&msg, NULL, 0, 0))
    {
    // Process the message
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return (int)msg.wParam;
    }

    LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM
    lParam)
    {
    HDC hDC;
    PAINTSTRUCT ps;
    RECT rect;

    switch (msg)
    {
    case WM_PAINT:
    // Draw some text centered in the client area of the main window
    hDC = BeginPaint(hWindow, &ps);
    GetClientRect(hWindow, &rect);
    DrawText(hDC, TEXT("This is a skeleton application!"), -1, &rect,
    DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    EndPaint(hWindow, &ps);
    return 0;

    case WM_DESTROY:
    // Exit the application
    PostQuitMessage(0);
    return 0;
    }
    return DefWindowProc(hWindow, msg, wParam, lParam);
    }


    ----------------------------------------------------------

    When i added:

    int main() {
    return 0;
    }

    before the `int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR szCmdLine, int iCmdShow)` function i got a Command prompt that just
    quit at the press of any key.
    The point of this project is to get a standard Windows window with `This is
    a skeleton application` text in the middle.


    Regards,
    Carl
    LuCk, Dec 30, 2003
    #4
  5. LuCk wrote:

    > Thankyou for your help so far.....i thought the error was in the linker
    > because it says Linking...but i guess not. Also i figured nothing was wrong


    The error is indeed during the linking, the linker tries to find your
    main function and cannot find one in any of your object files...

    > cause i copied it straight from Teach Yourself Game Programming in 24 Hours.
    >
    > Here is the code of the .cpp file (Skeleton.cpp)
    > ----------------------------------------------------------


    [.. snip, some off-topic win32 code...]

    > ----------------------------------------------------------
    >
    > When i added:
    >
    > int main() {
    > return 0;
    > }
    >
    > before the `int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    > PSTR szCmdLine, int iCmdShow)` function i got a Command prompt that just
    > quit at the press of any key.


    and why did you expect to get anything else from a program that just
    returns immediately in its main function?

    > The point of this project is to get a standard Windows window with `This is
    > a skeleton application` text in the middle.
    >
    >
    > Regards,
    > Carl


    To cut the story short, what that book tries to teach you has nothing to
    do with C++ it teaches you how to use some OS libraries (namely Win32
    API) to make a program that interacts with a given operating system
    (namely Windows) and as a bonus that OS has a wierd convention to have
    programs start from a function that is totally different than the
    regular main function. thus for all the above reasons it is totally
    off-topic here. You might want to check with a more relevant newsgroup
    or RTFM on how to tell your linker that you intend to do a Win32 program
    (hint).

    -- Nuclear / the Lab --
    John Tsiombikas (Nuclear / the Lab), Dec 30, 2003
    #5
  6. Jeff Schwab

    LuCk Guest

    Im not looking for trouble and im not trying to post in the wrong
    areas......i didnt know anything about what my problem was (as you can see
    from the title) and the book is teaching me how to game program for windows
    and in that particular section it shows how to create the screen in which
    the game will be played. That wasnt part of the game or game engine...just
    the screen. Thankyou for your hint....im hoping to find use for it :).....

    Regards,
    Carl

    `Sorry if posting off topic offended anyone here, i didnt intend it to be
    that way`
    LuCk, Dec 30, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RU
    Replies:
    2
    Views:
    7,954
  2. manish
    Replies:
    1
    Views:
    1,953
    Christopher Benson-Manica
    Apr 2, 2004
  3. Rob
    Replies:
    1
    Views:
    638
    Giuseppe
    Jul 22, 2003
  4. MagmaGal

    Newbie linking problem

    MagmaGal, Oct 19, 2004, in forum: C Programming
    Replies:
    7
    Views:
    344
    Tim Prince
    Nov 1, 2004
  5. Replies:
    6
    Views:
    301
    Walter Roberson
    Sep 16, 2006
Loading...

Share This Page