socket.error C2011: “sockaddr”: “struct”类型重定义

本文提供了解决在C/C++项目中因包含顺序不当导致的Windows.h与Winsock2.h文件重复定义问题的方法。只需调整头文件的包含顺序,将Windows.h置于Winsock2.h之前,即可避免冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解决方法:

重复定义问题,

将#include <windows.h>放在#include<winsock2.h>之后即可。

mouse@mouse-virtual-machine:~/workspace/code/socket$ make gcc ./src/socket.c ./src/util.c ./server.c -o server ./src/socket.c: In function ‘init_tcp’: ./src/socket.c:5:5: warning: implicit declaration of function ‘memset’ [-Wimplicit-function-declaration] 5 | memset(&(sock->_addr), 0, sizeof(sock->_addr)); | ^~~~~~ ./src/socket.c:5:5: warning: incompatible implicit declaration of built-in function ‘memset’ ./src/socket.c:3:1: note: include ‘<string.h>’ or provide a declaration of ‘memset’ 2 | #include "../include/util.h" +++ |+#include <string.h> 3 | ./src/socket.c: In function ‘_accept’: ./src/socket.c:21:76: warning: passing argument 3 of ‘accept’ makes pointer from integer without a cast [-Wint-conversion] 21 | errif((clnt->_fd = accept(serv->_fd, (struct sockaddr*)&(clnt->_addr), (socklen_t)sizeof(clnt->_addr))), "socket accept error!"); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | unsigned int In file included from ./src/../include/socket.h:3, from ./src/socket.c:1: /usr/include/x86_64-linux-gnu/sys/socket.h:307:28: note: expected ‘socklen_t * restrict’ {aka ‘unsigned int * restrict’} but argument is of type ‘unsigned int’ 307 | socklen_t *__restrict __addr_len); | ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~ gcc ./src/socket.c ./src/util.c ./client.c -o client ./src/socket.c: In function ‘init_tcp’: ./src/socket.c:5:5: warning: implicit declaration of function ‘memset’ [-Wimplicit-function-declaration] 5 | memset(&(sock->_addr), 0, sizeof(sock->_addr)); | ^~~~~~ ./src/socket.c:5:5: warning: incompatible implicit declaration of built-in function ‘memset’ ./src/socket.c:3:1: note: include ‘<string.h>’ or provide a declaration of ‘memset’ 2 | #include "../include/util.h" +++ |+#include <string.h> 3 | ./src/socket.c: In function ‘_accept’: ./src/socket.c:21:76: warning: passing argument 3 of ‘accept’ makes pointer from integer without a cast [-Wint-conversion] 21 | errif((clnt->_fd = accept(serv->_fd, (struct sockaddr*)&(clnt->_addr), (socklen_t)sizeof(clnt->_addr))), "socket accept error!"); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | unsigned int In file included from ./src/../include/socket.h:3, from ./src/socket.c:1: /usr/include/x86_64-linux-gnu/sys/socket.h:307:28: note: expected ‘socklen_t * restrict’ {aka ‘unsigned int * restrict’} but argument is of type ‘unsigned int’ 307 | socklen_t *__restrict __addr_len); | ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值