简体   繁体   中英

'inet_ntoa' not declared error

I'm tring to use inet_ntoa function writing to following code:

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:

error: 'inet_ntoa' was not declared in this scope

If I'm comment that line, the code is just fine.

Running under 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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