"inet_ntoa"未声明错误

'inet_ntoa' not declared error

本文关键字:错误 未声明 inet ntoa      更新时间:2023-10-16

我想使用inet_ntoa函数写入以下代码:

printf("Got connection from: %s:%dn", inet_ntoa(cli_addr.sin_addr.s_addr), cli_addr.sin_port);

使用以下内容包括:

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

但是,使用 g++/gcc 我总是收到错误:

错误:"inet_ntoa"未在此范围内声明

如果我评论那行,代码就很好。

在 Ubuntu12.04 下运行。

谁能告诉我我错过了什么?

谢谢!

man inet_ntoa看来,您必须在包含标头之前定义_BSD_SOURCE

#define _BSD_SOURCE
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>