Compile error 35: undefined reference to `_CreatePen@12'

This is a discussion on Compile error 35: undefined reference to `_CreatePen@12' within the C++ Programming forums, part of the General Programming Boards category; Sorry that this is messy, but im under a quickly approaching deadline. I would consolidate the code but I am ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    12

    Compile error 35: undefined reference to `_CreatePen@12'

    Sorry that this is messy, but im under a quickly approaching deadline. I would consolidate the code but I am new to windows programming and I'm not sure whats wrong.

    I just would like to know why I cant succesfully compile this code which was provided by our professor. I appreciate any help in getting it to compile and run.

    Thank you.

    Code:
    #include <windows.h>
    
    const char g_szClassName[] = "myWindowClass";
    
    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        
        HDC hDC;
        PAINTSTRUCT Ps;
        HPEN hPen1;
        //HBRUSH hBrush;
        //HFONT  hFont;
        long lfHeight=30;
        RECT rect;
        
        static bool isEllipse = true;
        
        
        static int thickness = 10;
        
        static int red=200;
        static int green=40;
        static int blue=60;
        
        static int die_num1 = 1;
        static int die_num2 = 1;
        
        switch(msg)
        {
            case WM_PAINT:
               
                hDC=BeginPaint(hwnd,&Ps);
                // pens are needed to draw things made with lines
                hPen1 =CreatePen(PS_SOLID, thickness, RGB(red,green,blue));
                
                //hBrush = CreateSolidBrush(RGB(255,0,0)); 
                
                //hFont = CreateFont(lfHeight, 0, 0, 0, 0, TRUE, 0, 0, 0, 0, 0, 0, 0, L"Times New Roman");
                
                SelectObject(hDC, hPen1);
                
                //SelectObject(hDC, hBrush);
                
                //SelectObject(hDC, hFont);
                
                rect.top=520;
                rect.bottom=665;
                rect.left=125;
                rect.right=1025;
                
                //DrawText(hDC,L"Art by Dr. Thomas Fernandez", -1,&rect, DT_WORDBREAK);
                
                /*for(int i=0;i<500;i=i+10)
                {
                    DrawTwoColoredLine(hDC,0,i,500,500,RGB(0,255,i/2),RGB(255,0,i/2));
                }
                
                //SelectObject(hDC, hPen);
                
                if(isEllipse)
                {
                    Ellipse(hDC, 520, 22, 750, 125);
                }
                
                else
                {
                    Rectangle(hDC, 520, 22, 750, 125);
                }
                
                //rCircle(hDC,233,230,210,25,25,250);
                //rCircle(hDC,233,230,140,250,25,250);
                
                //DeleteObject(hFont);
                //DeleteObject(hBrush);
                DeleteObject(hPen1);
                
                EndPaint(hwnd,&Ps);
                break;*/
            
            case WM_CLOSE:
                DestroyWindow(hwnd);
                break;
            
            case WM_CHAR:
                if(wParam=='r')
                {
                    red=rand()%256;
                    green=rand()%256;
                    blue=rand()%256;
                    InvalidateRect(hwnd,NULL,true);
                }
                else if(wParam==' ')
                {
                    die_num1=rand()%6+1;
                    die_num2=rand()%6+1;
                }
        break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
    
        //Step 1: Registering the Window Class
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "The title of my window",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
            NULL, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        // Step 3: The Message Loop
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to be sure you're linking with gdi32.lib, according to MSDN.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    12
    Im not sure what that means but would it have something to do with the fact that I'm using the Netbeans IDE and cygwin compiler?

  4. #4
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    90
    http://www.archivum.info/gnu.gcc.hel.../msg00004.html

    on gcc, they say -mwindows flag solved the problem...
    because that gdi belongs to windows...

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    The linker does not know where the CreatePen function is. You need to provide it that information. This is done by pointing it to one or more .lib files, which the linker can then read and find the relevant information. In this case, this information lies in a file called gdi32.lib.
    How to do it with cygwin/netbeans, I have no idea.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    12
    Thank you for your help so far. Does anyone know the way to link the file in Visual C++ 2008 Express?

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Indeed.
    Project options -> C/C++ -> Linker -> Additional Input Files
    Enter "gdi32.lib"
    Make sure you do it for both Debug & Release.
    Last edited by Elysia; 01-24-2009 at 08:22 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Elysia View Post
    Indeed.
    Project options -> C/C++ -> General -> Additional Include Directories
    Enter "gdi32.lib"
    Make sure you do it for both Debug & Release.
    Huh? No. Definitely not.

    Project options -> C/C++ -> Linker -> Additional Input Files
    is the correct place for it.


    But a VC++ Win32 project should link against the core Windows libs by default.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Oh, how the brain can play tricks on you X_X
    You are right, of course...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    12
    Thank you again for everyones support. I found out the problem. Instead of
    Code:
    const wchar_t g_szClassName[] = L"myWindowClass";
    I had
    Code:
    const char g_szClassName[] = "myWindowClass";
    Last edited by raptor1770; 01-24-2009 at 11:55 AM. Reason: CornedBee correction below added

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    There should be an L before the string literal in the wchar_t case, of course.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. undefined reference error while linking..help
    By rapture in forum C++ Programming
    Replies: 4
    Last Post: 12-04-2007, 03:54 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Question about OpenGL/Linux
    By Ideswa in forum Linux Programming
    Replies: 12
    Last Post: 09-10-2006, 05:56 AM
  5. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21