【问题标题】:__imp link errors using g++ running under mingw__imp 使用在 mingw 下运行的 g++ 链接错误
【发布时间】:2014-04-14 09:07:00
【问题描述】:

我有一个简单的套接字程序,我正在尝试使用在 Win8 系统上的 mingw(两个最新版本)中运行的 g++ 进行编译。我遇到了常见的链接器错误

undefined reference to `__imp_socket'
undefined reference to `__imp_gethostbyname'

我尝试添加 -lws2_32 没有运气;即它仍然找不到参考。有人可以建议我可能缺少的其他东西吗?

这是完整的输出:

G:\source\kak>g++ -o ./test_client -lws2_32 test_client.C
C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o:test_client.C:(.text+0x4f): undefined reference to `__imp_inet_addr'
C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o:test_client.C:(.text+0x6b): undefined reference to `__imp_socket'
C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o:test_client.C:(.text+0x8b): undefined reference to `__imp_connect'
d:/program files/mingw/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.1/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o: bad reloc address 0xc in section `.xdata'
collect2.exe: error: ld returned 1 exit status

【问题讨论】:

  • 发布g++ 命令的输出,并将-v 添加到选项中。另外,请记住,“最新版本”对 gcc 或 MinGW 不是很有帮助,因为它们有很多不同的来源。例如,您使用的是 gcc 4.8.1 - 我安装了带有 gcc 4.8.2 的 MinGW。

标签: c++ windows mingw imp


【解决方案1】:

尝试将-lws2_32 放在test_client.C 参数之后。 gcc (ld) 的链接器对可链接事物的顺序很敏感,这可能是它在链接时找不到您导入的函数的原因。

【讨论】:

  • 如果此答案解决了您的问题,请接受(绿色勾号)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-09
  • 2014-04-19
  • 2014-03-27
  • 1970-01-01
相关资源
最近更新 更多