You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Beej网络编程指南中showip.c编译错误求助

网络编程代码编译错误排查(Beej《网络编程指南》5.1节showip.c)

问题背景

我正在通过Beej的《网络编程指南》学习网络编程,书中5.1节的代码如下:

/*
** showip.c -- show IP addresses for a host given on the command line
*/

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int main(int argc, char *argv[])
{
    struct addrinfo hints, *res, *p;
    int status;
    char ipstr[INET6_ADDRSTRLEN];

    if (argc != 2) {
        fprintf(stderr,"usage: showip hostname\n");
        return 1;
    }

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
    hints.ai_socktype = SOCK_STREAM;

    if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
        return 2;
    }

    printf("IP addresses for %s:\n\n", argv[1]);

    for(p = res;p != NULL; p = p->ai_next) {
        void *addr;
        char *ipver;

        // get the pointer to the address itself,
        // different fields in IPv4 and IPv6:
        if (p->ai_family == AF_INET) { // IPv4
            struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
            addr = &(ipv4->sin_addr);
            ipver = "IPv4";
        } else { // IPv6
            struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
            addr = &(ipv6->sin6_addr);
            ipver = "IPv6";
        }

        // convert the IP to a string and print it:
        inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
        printf("  %s: %s\n", ipver, ipstr);
    }

    freeaddrinfo(res); // free the linked list

    return 0;
}

编译错误信息

在Ubuntu终端编译该代码时,出现如下错误:

showip.c: In function ‘main’:
showip.c:15:25: error: storage size of ‘hints’ isn’t known
   15 |         struct addrinfo hints, *res, *p;
      |                         ^~~~~
showip.c:28:23: warning: implicit declaration of function ‘getaddrinfo’ [-Wimplicit-function-declaration]
   28 |         if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0) {
      |                       ^~~~~~~~~~~
showip.c:29:54: warning: implicit declaration of function ‘gai_strerror’; did you mean ‘strerror’? [-Wimplicit-function-declaration]
   29 |                 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
      |                                                      ^~~~~~~~~~~~
      |                                                      strerror
showip.c:29:48: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
   29 |                 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
      |                                               ~^     ~~~~~~~~~~~~~~~~~~~~
      |                                                |     |
      |                                                |     int
      |                                                char *
      |                                               %d
showip.c:35:37: error: invalid use of undefined type ‘struct addrinfo’
   35 |         for(p = res;p != NULL; p = p->ai_next) {
      |                                     ^~
showip.c:41:22: error: invalid use of undefined type ‘struct addrinfo’
   41 |                 if (p->ai_family == AF_INET) { // IPv4
      |                      ^~
showip.c:42:75: error: invalid use of undefined type ‘struct addrinfo’
   42 |                         struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
      |                                                                           ^~
showip.c:46:77: error: invalid use of undefined type ‘struct addrinfo’
   46 |                         struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
      |                                                                             ^~
showip.c:52:28: error: invalid use of undefined type ‘struct addrinfo’
   52 |                 inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
      |                            ^~
showip.c:56:9: warning: implicit declaration of function ‘freeaddrinfo’ [-Wimplicit-function-declaration]
   56 |         freeaddrinfo(res); // free the linked list
      |         ^~~~~~~~~~~~

我已查阅该书勘误页但未找到有效解决方法,所需头文件看似均已包含,请求帮忙排查错误原因。

错误原因分析

这些错误的核心是缺少POSIX扩展宏定义,导致<netdb.h>未正确暴露struct addrinfo及相关函数的声明。

在Linux系统中,getaddrinfo、freeaddrinfo、gai_strerror和struct addrinfo属于POSIX标准扩展内容,默认情况下编译器不会启用这些扩展,因此头文件不会展开相关定义,进而引发:

  • struct addrinfo未定义导致的存储大小错误
  • 相关函数隐式声明的警告
  • 访问未定义结构体成员的编译错误

解决方法

以下两种方法均可修复问题:

方法1:在代码开头添加宏定义

在所有#include语句之前添加POSIX宏定义,强制头文件展开扩展内容:

#define _POSIX_C_SOURCE 200112L
// 或使用#define _GNU_SOURCE,覆盖更多GNU扩展

修改后的代码开头示例:

#define _POSIX_C_SOURCE 200112L
/*
** showip.c -- show IP addresses for a host given on the command line
*/

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
// ... 剩余代码保持不变

方法2:编译时通过gcc参数指定宏

编译命令中添加-D参数定义宏,无需修改代码:

gcc showip.c -o showip -D_POSIX_C_SOURCE=200112L

或使用GNU扩展宏:

gcc showip.c -o showip -D_GNU_SOURCE

验证修复

完成上述操作后重新编译,代码可正常通过编译。运行时执行./showip example.com即可输出目标主机的IPv4和IPv6地址。

内容的提问来源于stack exchange,提问作者user9026

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 11:05:04