Marcus Tik
Marcus Tik

Reputation: 1811

Undefined reference to GetStockObject@4

I've created a program in Eclipse / MinGW / C (project type: C) which should just present an empty window. It also has the folowing line:

wndclassex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

The call to GetStockObject() produces a compiler error:

Z:/mtsts_workspace/MTSTS/Debug/../WinMain.c:29: undefined reference to `GetStockObject@4'

Has anyone an idea what's wrong ?

Upvotes: 4

Views: 12463

Answers (3)

Mark H. Harris
Mark H. Harris

Reputation: 79

If your Win32 application is a non trivial GUI you will need these link directives to bring in the right libraries:

-lgdi32 -luser32 -lkernel32 -lcomctl32 -lm -mwindows
  • lgdi32: graphics subsystem
  • luser32: user subsystem
  • lkernel32: kernel subsystem
  • lcomctl32: common control dll versions
  • lm: math libraries
  • mwindows: standalone GUI (console free) application

The above directives tell the linker which libraries to link; this is a different concept than compiler header files which define the library function prototypes (among other things). For instance, declaring <math.h> does not automatically link the math lib; you need -lm. Also, <wingdi.h> does not automatically link the gdi32 lib; you need -lgdi32.

At the command line you literally specify -lgdi32 (along with others) after all your file names. Otherwise, how you specify how to link the various subsystem libraries depends on the IDE you are using for the specific build target you're working on. Check your documentation for the IDE of choice.

Upvotes: 0

Jon Bright
Jon Bright

Reputation: 13748

Best guess: you need to link gdi32.lib.

Upvotes: 4

unwind
unwind

Reputation: 400039

Check the documentation, and make sure you link to the required libraries (-lgdi32).

Upvotes: 13

Related Questions