Search code examples
linkermingwundefined-reference

Getting 'undefined reference to' using mingw32, but no linker error using mingw64, why?


I am trying to compile the examples for the LeapMotion API by using mingw on cygwin

When I compile with mingw64, it works fine:

x86_64-w64-mingw32-g++ -s -I ../include -L ../lib/x64/ ExampleConnection.cpp -lLeapC PollingSample.cpp -oPollingSample

but if I switch to 32 bit:

i686-w64-mingw32-g++ -s -I ../include -L ../lib/x86/ ExampleConnection.cpp -lLeapC PollingSample.cpp -oPollingSample

I get:

/usr/lib/gcc/i686-w64-mingw32/10/../../../../i686-w64-mingw32/bin/ld: /tmp/ccg2UNs3.o:ExampleConnection.cpp:(.text+0x31): undefined reference to `LeapCreateConnection'
/usr/lib/gcc/i686-w64-mingw32/10/../../../../i686-w64-mingw32/bin/ld: /tmp/ccg2UNs3.o:ExampleConnection.cpp:(.text+0x52): undefined reference to `LeapOpenConnection'
/usr/lib/gcc/i686-w64-mingw32/10/../../../../i686-w64-mingw32/bin/ld: /tmp/ccg2UNs3.o:ExampleConnection.cpp:(.text+0xb5): undefined reference to `LeapCloseConnection'
/usr/lib/gcc/i686-w64-mingw32/10/../../../../i686-w64-mingw32/bin/ld: /tmp/ccg2UNs3.o:ExampleConnection.cpp:(.text+0xe7): undefined reference to `LeapDestroyConnection'
/usr/lib/gcc/i686-w64-mingw32/10/../../../../i686-w64-mingw32/bin/ld: /tmp/ccg2UNs3.o:ExampleConnection.cpp:(.text+0xfd): undefined reference to `LeapDestroyConnection'
/usr/lib/gcc/i686-w64-mingw32/10/../../../../i686-w64-mingw32/bin/ld: /tmp/ccg2UNs3.o:ExampleConnection.cpp:(.text+0x16a): undefined reference to `LeapOpenDevice'
/usr/lib/gcc/i686-w64-mingw32/10/../../../../i686-w64-mingw32/bin/ld: /tmp/ccg2UNs3.o:ExampleConnection.cpp:(.text+0x1d2): undefined reference to `LeapGetDeviceInfo'
/usr/lib/gcc/i686-w64-mingw32/10/../../../../i686-w64-mingw32/bin/ld: /tmp/ccg2UNs3.o:ExampleConnection.cpp:(.text+0x205): undefined reference to `LeapGetDeviceInfo'
/usr/lib/gcc/i686-w64-mingw32/10/../../../../i686-w64-mingw32/bin/ld: /tmp/ccg2UNs3.o:ExampleConnection.cpp:(.text+0x26d): undefined reference to `LeapCloseDevice'
/usr/lib/gcc/i686-w64-mingw32/10/../../../../i686-w64-mingw32/bin/ld: /tmp/ccg2UNs3.o:ExampleConnection.cpp:(.text+0x4ac): undefined reference to `LeapPollConnection'
collect2: error: ld returned 1 exit status

I tried to change the position of -lLeapC in the command string, and inverting the order of .cpp files, but I get always the same error.


Solution

  • I solved the problem by converting LeapC.lib to LeapC.a in /lib/x86/ (and removing LeapC.lib from the folder)

    gendef LeapC.dll //this generates LeapC.def
    i686-w64-mingw32-dlltool.exe --def LeapC.def --dllname LeapC.dll --output-lib LeapC.a