0

I'm trying to use the x86_64-w64-mingw32-g++ utility to compile a Win64 binary on my Debian machine. I'm running into an issue with undefined reference * linker errors even though I'm passing the libraries to the linker:

# /usr/bin/x86_64-w64-mingw32-g++ -lgdi32 -luser32 -lkernel32 -lcomctl32 -m64 -o main.exe main.cpp code.obj
/tmp/ccSKB1pW.o:main.cpp:(.text+0x82a): undefined reference to `__imp_CreateCompatibleDC'
/tmp/ccSKB1pW.o:main.cpp:(.text+0x847): undefined reference to `__imp_CreateCompatibleBitmap'
/tmp/ccSKB1pW.o:main.cpp:(.text+0x85f): undefined reference to `__imp_SelectObject'
/tmp/ccSKB1pW.o:main.cpp:(.text+0x871): undefined reference to `__imp_CreateSolidBrush'
/tmp/ccSKB1pW.o:main.cpp:(.text+0x8da): undefined reference to `__imp_BitBlt'
/tmp/ccSKB1pW.o:main.cpp:(.text+0x8ee): undefined reference to `__imp_SelectObject'
/tmp/ccSKB1pW.o:main.cpp:(.text+0x8fe): undefined reference to `__imp_DeleteObject'
/tmp/ccSKB1pW.o:main.cpp:(.text+0x90e): undefined reference to `__imp_DeleteDC'
collect2: error: ld returned 1 exit status

I'm not able to find much information about this issue online. Any idea why it's not finding the symbols in the libraries?

Edit: As far as I can tell, a GDI library was provided by the debian package:

# dpkg -L mingw-w64-x86-64-dev | grep gdi32
/usr/x86_64-w64-mingw32/lib/libgdi32.a

Additionally, I am able to find the symbols in the library file:

# objdump -t /usr/x86_64-w64-mingw32/lib/libgdi32.a | grep CreateCompat
[  7](sec  1)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x0000000000000000 CreateCompatibleDC
[  8](sec  5)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x0000000000000000 __imp_CreateCompatibleDC
[  7](sec  1)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x0000000000000000 CreateCompatibleBitmap
[  8](sec  5)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x0000000000000000 __imp_CreateCompatibleBitmap
Gogeta70
  • 881
  • 1
  • 9
  • 23
  • The `__imp_XXX` tells you you are missing an import library. The ones you are missing are part of GDI32. When you use a DLL on Windows, there is a `*.lib` file that provides the DLL's exports, and then the DLL itself that provides the implementation. Th `*.lib` extension is the same extension as a static library, but it serves a different purpose. Can you (1) remove referenced to `gdi32.dll`, or (2) copy the import lib's from a Windows machine to your Debian machine? – jww Nov 15 '17 at 23:17
  • @jww Curious. I am using GDI functions in my code, so I do require the library. I thought the one provided by the debian package would be sufficient? I'll update my question to show the location of the gdi32 lib. – Gogeta70 Nov 15 '17 at 23:25
  • You may be using the import library for 32-bit, to build a 64-bit binary you need 64-bit import libraries at link time, and 64-bit DLLs at runtime. – Ben Voigt Nov 15 '17 at 23:32
  • @BenVoigt I ran `objdump -p libgdi32.a` and it tells me `file format pe-x86-64`. That would indicate that this is a 64-bit library, right? – Gogeta70 Nov 15 '17 at 23:38
  • @Gogeta70: Yes, that looks ok. And if you dump the exports from it (`objdump` or `x86_64-w64-mingw32-nm` or similar)? – Ben Voigt Nov 15 '17 at 23:48
  • You might also try moving the `-l` options to the end of the command-line. – Ben Voigt Nov 15 '17 at 23:50
  • @BenVoigt Moving the `-lgdi32` and other linker options to the end of the command did the trick! I find this very strange... Any explanation for this behavior? – Gogeta70 Nov 16 '17 at 00:07
  • have a look [here](https://stackoverflow.com/a/409470/2747962) – Darklighter Nov 16 '17 at 00:13

0 Answers0