簡體   English   中英

在Windows上編譯套接字程序時mingw中的錯誤

[英]error in mingw while compiling socket proram on windows

我在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

bzero是一個非標准函數,它已被棄用並從POSIX 2008中刪除。來自bzero手冊頁:

符合
4.3BSD。 不推薦使用此函數(在POSIX.1-2001中標記為LEGACY):在新程序中使用memset(3)。 POSIX.1-2008刪除了bzero()的規范。

正如cnicutar 建議的那樣 ,你應該使用memset代替:

memset(ptr, 0, sizeof *ptr);

作為旁注,請注意以下事實:如果ptr的目標包含指針,則上面的memset不會使指針為NULL,而只是0,這可能與NULL不同。

struct情況下,最好的方法是這樣的:

struct something
{
    int a;
    int b;
    char *c;
    int d;
};

struct something var;
...
var = (struct something){0};  /* or NULL if first argument is a pointer */
/* or alternatively */
var = (struct something){ .a = 0 };

對於struct的數組,最標准的方法是for循環,然后將struct清零,如上所述。


關於inet_pton函數,也許這個問題可以幫助你,建議使用WSAAddressToString函數。

#include <string.h>

錯誤消息告訴您gcc將bzero作為內置函數。 但是如果沒有范圍內的原型,則會將您的用法視為引用返回int (默認返回類型)的函數。 因此它與內置函數不匹配,並且gcc只是將降壓傳遞給鏈接器,類似地找不到這樣的函數。 添加頭文件,以便編譯器找到正確的原型。

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

#define _BSD_SOURCE

如果這沒有擺脫bzero錯誤,那么你真的應該拋棄它並使用memset

對於inet_pton ,您還需要:

#include <arpa/inet.h>

關於Windows上的套接字編程的這個注釋可能也很有用: 來自Beej的指南

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM