gpt4 book ai didi

c - 在windows上编译socket proram时mingw出错

转载 作者:行者123 更新时间:2023-11-30 14:19:54 25 4
gpt4 key购买 nike

我在 mingw 上收到此错误:pr1.c 是文件名

我遇到了一些问题,然后添加了这样的库,但现在出现了这些错误:

$ gcc pr1.c -o pr1.exe -lwsock32 -lws2_32

pr1.c: In function 'main':
pr1.c:54:2: warning: incompatible implicit declaration of built-in function 'b
zero' [enabled by default]
C:\Users\rak\AppData\Local\Temp\ccMtb7Wt.o:pr1.c:(.text+0xc9): undefined refe
rence to `inet_pton'
C:\Users\rak\AppData\Local\Temp\ccMtb7Wt.o:pr1.c:(.text+0xe2): undefined refe
rence to `bzero'
C:\Users\rak\AppData\Local\Temp\ccMtb7Wt.o:pr1.c:(.text+0x1b5): undefined ref
erence to `bzero'
C:\Users\rak\AppData\Local\Temp\ccMtb7Wt.o:pr1.c:(.text+0x224): undefined ref
erence to `bzero'
c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: C:\Users\rak\AppData\Local\Temp\ccMtb7Wt.o: bad reloc address 0x20 in section `.eh_frame'
c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: final link
failed: Invalid operation
collect2.exe: error: ld returned 1 exit status

最佳答案

#include <string.h>

错误消息告诉您 gcc 有 bzero 作为内置函数。但是如果范围内没有原型(prototype),您的用法将被视为引用返回 int (默认返回类型)的函数。所以它与内置函数不匹配,gcc 只是将责任传递给链接器,而链接器同样找不到这样的函数。添加头文件,以便编译器找到正确的原型(prototype)。

您可能需要启用 bzero,例如:

#define _BSD_SOURCE

如果这不能消除 bzero 错误,那么您确实应该放弃它并使用 memset

对于 inet_pton,您还需要:

#include <arpa/inet.h>

这篇关于 Windows 上套接字编程的说明也可能有用:from Beej's Guide .

关于c - 在windows上编译socket proram时mingw出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15966357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com