-1

winMain is a default main function nothing else. window.cpp included in it.

without the polygon the code can be compiled and a simple window shows. but when i want to draw a polygon i get the following error message:

D:\...\window.cpp|| undefined reference to `Polygon@12'|

tried to find the implementation of polygon but my IDE(codeblocks) cannot find it.

window.cpp

#include "window.h"

LRESULT CALLBACK window::windproc(HWND hMain, UINT msg, WPARAM wParam, LPARAM lParam)
//  PROCEDURE OF THE MAIN WINDOW
{
    HDC hdc;
    PAINTSTRUCT ps;
    POINT pt[3];
        pt[0].x = 300;
        pt[0].y = 200;
        pt[1].x = 350;
        pt[1].y = 100;
        pt[2].x = 400;
        pt[2].y = 200;

    switch(msg)
    {
    case WM_PAINT:
            hdc = BeginPaint(hMain,&ps);
            Polygon(hdc,pt,3);
            EndPaint(hMain,&ps);
        break;
    case WM_MBUTTONDOWN:
        PostQuitMessage(0);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc (hMain, msg, wParam, lParam);
    }

    return 0;
}

void center_window(HWND main)
{
    RECT rc;
    GetClientRect(main, &rc);
    int xPos = (GetSystemMetrics(SM_CXSCREEN) - rc.right)/2;
    int yPos = (GetSystemMetrics(SM_CYSCREEN) - rc.bottom)/2;
    SetWindowPos(main, 0, xPos, yPos, 0, 0, SWP_NOZORDER | SWP_NOSIZE );
}

void check_return(HWND main)
{
    if(!main)
        MessageBox(main, __FILE__, "ERROR", MB_OK | MB_ICONERROR);
}

window.h

#include <windows.h>

class window
{
public:
    class invalid{};    //  to throw an exception

    window(){}
    ~window(){}

    LPCSTR label{"main"};
    LPCSTR title{"Placeholder"};


    static LRESULT CALLBACK windproc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
};

//  AUX FUNCTIONS
void center_window(HWND hwnd);
void check_return(HWND hwnd);
Frank
  • 13
  • 3

1 Answers1

0

One way to find which library provides a given function, is to check the documentation.

The documentation reveals that Polygon is provided by gdi32.lib.

Apparently the compiler you're using doesn't link with that library by default, so add it.


Regarding

window.cpp included in it

don't #include implementation files. Include headers. Compiler implementation files separately and link with them (in an IDE project it's sufficient to have all the implementation files part of your project).

Regarding

winMain is a default main function

just use a standard main: there's no advantage in using Microsoft's non-standard monstrosity. With Microsoft tools (this is not necessary with g++) add /entry:mainCRTStartup to the linker options. You can specify that in the entry point linker option.

Cheers and hth. - Alf
  • 138,963
  • 15
  • 198
  • 315
  • I'm trying but no success. i saw the documentation. if i make a project and link that file i get `ld.exe||cannot find -lgdi32.lib|` error. same for that entry option – Frank Nov 13 '15 at 18:13
  • okay i created a new project, included int the main file my own window.h, i only added the cpp to the project....now it works....its annoying i do not know why – Frank Nov 13 '15 at 18:50
  • There's no advantage to using `main` either. In fact, there are lots of disadvantages in using `main` as the entry point to an application that has a GUI. For one, you'll have to set `/SUBSYSTEM:CONSOLE` in the linker, and the OS will allocate a console for you. If you still use `SUBSYSTEM:WINDOWS`, and force your `main` function as the entry point, you'll break the CRT. There is one significant advantage to using `WinMain`: You'll get the `HINSTANCE` of the module that started the application, and don't have to query for it when you need it. – IInspectable Nov 13 '15 at 19:56
  • @Frank: With g++ the relevant linker option is `-lgdi32` (no `.lib`), and I believe that name of the library file with g++ is `libgdi32.a`. However, as I recall gdi32 is linked in by default with g++. Checking... OK, the Windows command `g++ -dumpspecs 2>&1 | find "gdi32"` tells me that with g++ gdi32 is linked by default for a GUI subsystem build, but not for a console subsystem build. So that is probably the difference you encountered now. – Cheers and hth. - Alf Nov 13 '15 at 21:31