2 New package troubleshooting
TimothyGu edited this page 2014-09-15 18:12:42 -07:00

So you wrote your new foo.mk but discovered that it isn't working. This page will help you track down the causes of the problem and help you fix it.

If you have additional questions, we are always happy to help at https://lists.nongnu.org/mailman/listinfo/mingw-cross-env-list.

undefined reference to '_imp__*'

If your log shows linking failures with undefined reference to _imp__* on static builds, then you've come to the right place.

The log usually shows something like:

'i686-pc-mingw32.static-gcc' -W -Wall -Werror -std=c99 -pedantic './src/libmikmod-test.c' -o '/mxe/usr/i686-pc-mingw32.static/bin/test-libmikmod.exe' -lmikmod
/tmp/cckad1Ds.o:libmikmod-test.c:(.text+0xf): undefined reference to `_imp__MikMod_RegisterAllDrivers'
/tmp/cckad1Ds.o:libmikmod-test.c:(.text+0x1d): undefined reference to `_imp__MikMod_Init'
/tmp/cckad1Ds.o:libmikmod-test.c:(.text+0x24): undefined reference to `_imp__MikMod_Exit'
/mxe/usr/bin/i686-pc-mingw32.static-ld: /tmp/cckad1Ds.o: bad reloc address 0x20 in section `.eh_frame'
/mxe/usr/bin/i686-pc-mingw32.static-ld: final link failed: Invalid operation

Short answer: you have to get rid of the __declspec(dllimport) in the headers of the package somehow, either through a sed command or with a patch. See a3b505c for a cleaner approach of the problem, and f55a255 for a quicker but dirtier solution.

Long answer:

__declspec(dllimport) is used for projects supporting DLL building. But DLLs are weird. They don't directly contain symbols like foo but a mangled one like _imp__foo. This forces the use of import libraries (.dll.a on MinGW, .lib on MSVC) that do contain symbols with correct names and "redirects" them to _imp__* ones in the DLL.

To allow developers to be able to use the DLL without an import lib, Micro$soft got the idea of dllimport, which tells the compiler to directly call _imp__foo instead of foo. This works for DLL, great. But this doesn't work for traditional static libraries, which contain the symbols with correct names. Hence the errors you are seeing.

As DLLs are much more popular than static libs on Windows, projects started using __declspec(dllimport) by default. Again hence the errors you are seeing.

Many projects however, offer a macro that disables the use of __declspec(dllimport), such as libmikmod. This allows for a cleaner way of fixing the problem. See a3b505c.

If the package does not offer a similar variable, or if you are too lazy and want a quick 'n' dirty fix, you'll have to go old-style and use a sed like f55a255.