giampaolo / psutil

Cross-platform lib for process and system monitoring in Python
BSD 3-Clause "New" or "Revised" License
10.31k stars 1.39k forks source link

[macOS] error: field has incomplete type 'struct sockaddr' #2365

Closed ryandesign closed 8 months ago

ryandesign commented 10 months ago

Summary

Description

Build failure on old versions of macOS (prior to 10.9):

In file included from psutil/arch/osx/net.c:12:
/usr/include/net/if.h:306:19: error: field has incomplete type 'struct sockaddr'
                struct  sockaddr ifru_addr;
                                 ^
/usr/include/net/if.h:306:10: note: forward declaration of 'struct sockaddr'
                struct  sockaddr ifru_addr;
                        ^
/usr/include/net/if.h:307:19: error: field has incomplete type 'struct sockaddr'
                struct  sockaddr ifru_dstaddr;
                                 ^
/usr/include/net/if.h:306:10: note: forward declaration of 'struct sockaddr'
                struct  sockaddr ifru_addr;
                        ^
/usr/include/net/if.h:308:19: error: field has incomplete type 'struct sockaddr'
                struct  sockaddr ifru_broadaddr;
                                 ^
/usr/include/net/if.h:306:10: note: forward declaration of 'struct sockaddr'
                struct  sockaddr ifru_addr;
                        ^
/usr/include/net/if.h:352:18: error: field has incomplete type 'struct sockaddr'
        struct  sockaddr ifra_addr;
                         ^
/usr/include/net/if.h:306:10: note: forward declaration of 'struct sockaddr'
                struct  sockaddr ifru_addr;
                        ^
/usr/include/net/if.h:353:18: error: field has incomplete type 'struct sockaddr'
        struct  sockaddr ifra_broadaddr;
                         ^
/usr/include/net/if.h:306:10: note: forward declaration of 'struct sockaddr'
                struct  sockaddr ifru_addr;
                        ^
/usr/include/net/if.h:354:18: error: field has incomplete type 'struct sockaddr'
        struct  sockaddr ifra_mask;
                         ^
/usr/include/net/if.h:306:10: note: forward declaration of 'struct sockaddr'
                struct  sockaddr ifru_addr;
                        ^
/usr/include/net/if.h:437:26: error: field has incomplete type 'struct sockaddr_storage'
        struct sockaddr_storage addr;   /* in/out */
                                ^
/usr/include/net/if.h:437:9: note: forward declaration of 'struct sockaddr_storage'
        struct sockaddr_storage addr;   /* in/out */
               ^
/usr/include/net/if.h:438:26: error: field has incomplete type 'struct sockaddr_storage'
        struct sockaddr_storage dstaddr; /* out */
                                ^
/usr/include/net/if.h:437:9: note: forward declaration of 'struct sockaddr_storage'
        struct sockaddr_storage addr;   /* in/out */
               ^
8 errors generated.

This is a common problem, caused by <net/if.h> on OS X versions earlier than 10.9 not including <sys/socket.h>. The solution is to include <sys/socket.h> yourself before including <net/if.h>. (So much for my recommendation to include headers in alphabetical order!) I'll send a PR.