【问题标题】:Getting 'undefined reference to' using mingw32, but no linker error using mingw64, why?使用 mingw32 获取“未定义的引用”,但使用 mingw64 没有链接器错误,为什么?
【发布时间】:2021-03-29 06:57:41
【问题描述】:

我正在尝试通过在 cygwin 上使用 mingw 来编译 LeapMotion API 的示例

当我使用 mingw64 编译时,它工作正常:

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

但如果我切换到 32 位:

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

我明白了:

/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

我尝试更改命令字符串中 -lLeapC 的位置,并颠倒 .cpp 文件的顺序,但总是遇到相同的错误。

【问题讨论】:

  • 您是否拥有正确的 32 库 (libLeapC.a/libLeapC.dll.a)?
  • /lib/x86 中只有 LeapC.lib 和 LeapC.dll,文件名与 /lib/x64 中相同
  • .lib 通常表示 MSVC。 MinGW 将 .a 用于静态库,将 .dll.a 用于共享库(连同 .dll)。
  • 但 64 位版本运行良好,文件名相同
  • 你能检查一下 DLL 文件导出了哪些符号(例如使用pexports)吗?

标签: linker mingw undefined-reference


【解决方案1】:

我通过将 LeapC.lib 转换为 /lib/x86/ 中的 LeapC.a(并从文件夹中删除 LeapC.lib)解决了这个问题

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

【讨论】:

  • 请注意,您通常不能链接由两个编译器生成的 C++ 代码。如果它有效,那么您很幸运没有使用这个或那个语言功能或者没有遇到问题。链接器找到其符号这一事实并不意味着 ABI、异常处理、甚至标准库类型甚至分配器在代码的两部分中都是相同的。
猜你喜欢
  • 1970-01-01
  • 2013-08-10
  • 2014-06-27
  • 1970-01-01
  • 1970-01-01
  • 2010-12-25
  • 2019-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多