Reputation: 218
I have been having trouble compiling some code in C++.
Code:
#include <windows.h>
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nShowCmd )
{
static char name[] = "My Application";
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
// Step 1: Registering the Window Class
wc.cbSize = sizeof( WNDCLASSEX );
wc.style = CS_HREDRAW | CS_VREDRAW;
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 = name;
wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
if ( !RegisterClassEx( & wc ) )
{
MessageBox( NULL, "Window Registration Failed!", "Registration Failure", MB_ICONEXCLAMATION | MB_OK );
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, name, "My First Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 400, NULL, NULL, hInstance, NULL );
ShowWindow( hwnd, nShowCmd );
UpdateWindow( hwnd );
// Step 3: The Message Loop
while( GetMessage( & Msg, NULL, 0, 0 ) > 0 )
{
TranslateMessage( & Msg );
DispatchMessage( & Msg );
}
return ( int )Msg.wParam;
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch( msg )
{
case WM_PAINT:
hdc = BeginPaint( hwnd, & ps );
GetClientRect( hwnd, & rect );
Rectangle( hdc, rect.right / 2 - 50, rect.bottom / 2 - 20, rect.right / 2 + 50, rect.bottom / 2 + 20 );
DrawText( hdc, "Hello World!", -1, & rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER );
EndPaint( hwnd, & ps );
break;
case WM_CLOSE:
DestroyWindow( hwnd );
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
}
return DefWindowProc( hwnd, msg, wParam, lParam );
}
The tutorial I am following says that it compiles correctly with no problems, but this is what my compiler looks like:
NPP_EXEC: "Compile C++ File"
NPP_SAVE: G:\C++\src\Win32\Hello World! Window.cpp
g++ -o "G:\C++\src\Win32\Hello World! Window" "G:\C++\src\Win32\Hello World! Window.cpp" -static
Process started >>>
C:\Users\Braeden\AppData\Local\Temp\cc3N4Ls1.o:Hello World! Window.cpp:(.text+0x29b): undefined reference to `__imp_Rectangle'
collect2.exe: error: ld returned 1 exit status
<<< Process finished.
================ READY ================
I am not using any popular IDE. I am using Notepad++ as a text editor and a MinGW distro as my compiler. I am on Win7 x64. Could someone tell me what I am doing wrong?
Upvotes: 1
Views: 2582
Reputation: 137438
Rectangle
is provided by Gdi32.dll
. You should be linking against Gdi32.lib
.
Upvotes: 1