简体   繁体   English

'inet_ntoa'未声明错误

[英]'inet_ntoa' not declared error

I'm tring to use inet_ntoa function writing to following code: 我正在尝试使用inet_ntoa函数编写以下代码:

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

Using the following includes: 使用以下内容包括:

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

But, using g++/gcc I'm always get the error: 但是,使用g ++ / gcc总是会出现错误:

error: 'inet_ntoa' was not declared in this scope 错误:在此范围内未声明“ inet_ntoa”

If I'm comment that line, the code is just fine. 如果我对此行发表评论,那么代码就很好。

Running under Ubuntu12.04. 在Ubuntu12.04下运行。

Can anybody tell me what I missed? 谁能告诉我我错过了什么?

Thanks! 谢谢!

From man inet_ntoa it appears you have to define _BSD_SOURCE before including the headers: man inet_ntoa看来,您必须先定义_BSD_SOURCE然后再包含标头:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM