cc99winsock2open62541

min-GW gcc cant link winsock2.h


I'm trying to use winsock2 library, i'm using it to compile tutorial server from open62541

some code that i used and make error (from open62541.c that built from amalgamation):

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

int main(){
   struct pollfd tmp_poll_fd;
   }

i'm using mingw to compile the open62541 server, and follow the tutorial

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

the error :

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

the other error also related to winsock2 library such as POLLWRNORM undeclared

i already check that winsock2.h is in the place,

and i also check the inside to winsock2.h contain struct pollfd and POLLWRNORM

i already try some change in gcc command such as :

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

is there a way to solve this?


Solution

  • Disclaimer: This answer is based on my locally installed version of MinGW (8.1.0, built by MinGW-W64 project), it can be a different situation on yours.

    The include file "winsock2.h" has a guard on the target Windows version:

    #if (_WIN32_WINNT >= 0x0600)
    
    /* ... */
    
    typedef struct pollfd {
    
    /* ... */
    
    #endif /*(_WIN32_WINNT >= 0x0600)*/
    

    The macro _WIN32_WINNT is set by "_mingw.h" to 0x502, if you don't change it by other means:

    #ifndef _WIN32_WINNT
    #define _WIN32_WINNT 0x502
    #endif
    

    The value 0x502 means "Windows Server 2003" according to Microsoft's document here. If you set the macro to a value greater than or equal to 0x600 ("Windows Vista"), your compile will succeed:

    #define _WIN32_WINNT 0x600
    
    #include <winsock2.h>
    #include <windows.h>
    #include <ws2tcpip.h>
    
    int main(void) {
        struct pollfd tmp_poll_fd;
    }