min-GW gcc cant link winsock2.h

tf7tbtn2  于 2023-04-05  发布在  其他
关注(0)|答案(1)|浏览(96)

我正在尝试使用winsock2库,我正在使用它从open62541编译教程服务器
我使用过一些代码,并犯了一些错误(来自于从amalgamation构建的open62541.c):

#include <winsock2.h>
#include <Windows.h>
#INCLUDE <ws2tcpip.h>

int main(){
   struct pollfd tmp_poll_fd;
   }

我使用mingw编译open62541服务器,并遵循tutorial

gcc -std=c99 server.c open62541.c -lws2_32 -o server.exe

错误:

error: storage size of 'tmp_poll_fd' isn't known
     struct pollfd tmp_poll_fd;

其他错误也与winsock2库有关,如POLLWRNORM undeclared
我已经检查了winsock2.h是否在该位置,
我还检查了winsock2.h的内部包含struct pollfdPOLLWRNORM
我已经尝试对gcc命令进行一些更改,例如:

gcc -std=c99 server.c open62541.c -libws2_32 -o server.exe

有办法解决吗?

e5njpo68

e5njpo681#

  • 免责声明:这个答案是基于我本地安装的MinGW版本(8.1.0,由MinGW-W 64项目构建),它可能是您的不同情况。

包含文件“winsock2.h”在目标Windows版本上有一个保护:

#if (_WIN32_WINNT >= 0x0600)

/* ... */

typedef struct pollfd {

/* ... */

#endif /*(_WIN32_WINNT >= 0x0600)*/

_WIN32_WINNT被“_mingw.h”设置为0x 502,如果你不通过其他方式更改它:

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x502
#endif

根据Microsoft's document here,值0x 502表示“Windows Server 2003”。如果您将宏设置为大于或等于0x 600(“Windows Vista”)的值,则编译将成功:

#define _WIN32_WINNT 0x600

#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>

int main(void) {
    struct pollfd tmp_poll_fd;
}

相关问题