18

I've been attempting to test out GLFW with C++ for quite a while and am having constant linker issues. I am fairly new to C++, although I have experience in Java and C#, working directly with the compiler is fairly new to me. Here's my setup information.

IDE: Qt Creator

OS: Windows 7 64-bit

Compiler: MinGW32 4.8.1

01:23:26: Starting: "C:\MinGW\bin\mingw32-make.exe" 
C:/MinGW/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'A:/workspace_cpp/Test-Debug'
g++ -Wl,-subsystem,console -mthreads -o debug\Test.exe debug/main.o  -lglfw3 -lopengl32 
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libglfw3.a(win32_monitor.c.obj):win32_monitor.::(.text+0x2c7): undefined reference to `CreateDCW@16'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x358): undefined reference to `GetDeviceCaps@8'
Makefile.Debug:77: recipe for target 'debug\Test.exe' failed
mingw32-make[1]: Leaving directory 'A:/workspace_cpp/Test-Debug'
Makefile:34: recipe for target 'debug' failed
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libglfw3.a(win32_monitor.c.obj):win32_monitor.c:(.text+0x370): undefined reference to `GetDeviceCaps@8'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libglfw3.a(win32_monitor.c.obj):win32_monitor .c:(.text+0x39e): undefined reference to `DeleteDC@4'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe:     c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libglfw3.a(win32_monitor.c.obj): bad reloc address 0x20 in section `.eh_frame'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status

The code I'm testing is the code on the GLFW Documentation Page, I'm using my own build of GLFW, and have already tried this and several other potential solutions. I have tried using the prebuilt GLFW mingw libraries but I couldn't get them to work.

Community
  • 1
  • 1
Matthew D
  • 481
  • 1
  • 4
  • 10
  • 4
    It looks like you've missed a library there. `CreateDCW`, `GetDeviceCaps` and `DeleteDC` all exist in `gdi32.dll` - you need to add the `gdi32` import lib as a minimum. Just add it in the same way you added the `glfw3` and `opengl32` libraries. – enhzflep Feb 25 '14 at 11:27
  • Thank you very much! I was struggling with that for so long, I wasn't aware of the gdi32 library, it wasn't mentioned anywhere, I don't know how to upvote your comment or select a best answer but you're a huge help! – Matthew D Feb 26 '14 at 00:27
  • You're welcome. I have old copies of "Win32API.hlp" and "win32sdk.hlp". In each of these, there's a button labelled "Quick Info", which lists (a) the .h file the function is declared in and (b) the .lib (or .a in the case of gcc) file that contains the actual code required to use the dll files. I've found them to be invaluable. If you can't find anything online, for offline help, drop me an email and I'll forward one to you. Email's on my profile page. – enhzflep Feb 26 '14 at 00:39
  • When the problem has been solved, please do not add "Solved" or similar to the title. Just post an answer and mark it as accepted. (Or get @enhzflep to post the answer). That is much more helpful to others who might read this and be interested in the solution. – jalf Feb 26 '14 at 10:01
  • My mistake, I've gone ahead and posted the solution that worked for me as an answer. – Matthew D Feb 26 '14 at 17:31

1 Answers1

30

The solution as posted by enhzflep was to include to gdi32 library when compiling, making all the necessary linkers -lglfw3 -lgdi32 -lopengl32, as the missing methods, CreateDCW, GetDeviceCaps and DeleteDC are all inside of libgdi32.a in the MinGW lib folder C:\MinGW\lib\libgdi32.a in this case.

Matthew D
  • 481
  • 1
  • 4
  • 10
  • Had to add these -lglfw3 -lgdi32 to the end of my build command, after the -o and main.cpp file as links must be at the end with mingw – abelito May 31 '19 at 22:22