compiling with std=c99

G

genoeffo

Hi all,
I'm trying to compile compile a little ping program with
gcc -o myping myping.c
I have no problems, but if I compile it with
gcc -o myping -std=c99 myping.c

the result is:
"
myping.c: In function 'main':
myping.c:62: warning: implicit declaration of function 'inet_aton'
myping.c:67: error: dereferencing pointer to incomplete type
myping.c:68: error: dereferencing pointer to incomplete type
myping.c:69: error: dereferencing pointer to incomplete type
myping.c:70: error: dereferencing pointer to incomplete type
myping.c:71: error: dereferencing pointer to incomplete type
myping.c:72: error: dereferencing pointer to incomplete type
myping.c:73: error: dereferencing pointer to incomplete type
myping.c:78: error: dereferencing pointer to incomplete type
myping.c:85: error: dereferencing pointer to incomplete type
myping.c:87: error: dereferencing pointer to incomplete type
myping.c:89: error: dereferencing pointer to incomplete type
"
How can I solve this problem?

Thank you in advance for your help,
Gg.


#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <netdb.h>

#define MAX_PACKET 1000
#define ICMP_SIZE 16

unsigned short check_sum(unsigned short *addr, int len)
{
int nleft = len;
int sum = 0;
unsigned short *w = addr;
unsigned short answer = 0;

while (nleft > 1)
{
sum += *w++;
nleft -= 2;
}
if (nleft == 1)
{
*(unsigned char *)(&answer) = * (unsigned char *) w;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xffff);
sum = sum + (sum >> 16);
answer = ~sum;
return answer;
}

void timeval_sub(struct timeval *out, struct timeval *in)
{
out->tv_sec = out->tv_sec - in->tv_sec;
out->tv_usec = out->tv_usec - in->tv_usec;
if (out->tv_sec < 0)
{
out->tv_usec += 1000000;
out->tv_sec--;
}
}

int main(int argc, char *argv[])
{
int icmp_socket_fd;
struct sockaddr_in icmp_socket_info;
struct in_addr addr;
struct icmp *icmp_buf;
char pkt_buf[MAX_PACKET];
struct ip *ip = (struct ip *)pkt_buf;
struct timeval tv, appo_tv;

icmp_socket_fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
inet_aton(argv[1], &addr);
icmp_socket_info.sin_family = AF_INET;
icmp_socket_info.sin_addr.s_addr = addr.s_addr;

icmp_buf = (struct icmp *)pkt_buf;
icmp_buf->icmp_type = ICMP_ECHO;
icmp_buf->icmp_code = 0;
icmp_buf->icmp_id = getpid();
icmp_buf->icmp_seq = 0;
gettimeofday((struct timeval *)icmp_buf->icmp_data, NULL);
icmp_buf->icmp_cksum = 0;
icmp_buf->icmp_cksum = check_sum((unsigned short *)icmp_buf,
ICMP_SIZE);
sendto(icmp_socket_fd, icmp_buf, ICMP_SIZE, 0, (struct sockaddr *)
&icmp_socket_info, sizeof(icmp_socket_info));
do
{
gettimeofday(&appo_tv, NULL);
timeval_sub(&appo_tv, (struct timeval *)icmp_buf->
icmp_data);
recvfrom(icmp_socket_fd, pkt_buf, sizeof(pkt_buf), 0, NULL,
NULL);
icmp_buf = (struct icmp *) (pkt_buf+ip->ip_hl*4);;
}
while(icmp_buf->icmp_type != ICMP_ECHOREPLY );
gettimeofday(&tv, NULL);
timeval_sub(&tv, (struct timeval *)icmp_buf->icmp_data);
printf("Ping received in %ld micro sec\n", tv.tv_sec* 1000000
+tv.tv_usec);
}

PS
gcc --version

gcc (GCC) 4.1.0 20060304 (Red Hat 4.1.0-3)
 
A

Arthur J. O'Dwyer

Hi all,
I'm trying to compile compile a little ping program with
gcc -o myping myping.c
I have no problems, but if I compile it with
gcc -o myping -std=c99 myping.c

the result is:
"
myping.c: In function 'main':
myping.c:62: warning: implicit declaration of function 'inet_aton'
myping.c:67: error: dereferencing pointer to incomplete type
myping.c:68: error: dereferencing pointer to incomplete type
myping.c:69: error: dereferencing pointer to incomplete type
myping.c:70: error: dereferencing pointer to incomplete type
myping.c:71: error: dereferencing pointer to incomplete type
myping.c:72: error: dereferencing pointer to incomplete type
myping.c:73: error: dereferencing pointer to incomplete type
myping.c:78: error: dereferencing pointer to incomplete type
myping.c:85: error: dereferencing pointer to incomplete type
myping.c:87: error: dereferencing pointer to incomplete type
myping.c:89: error: dereferencing pointer to incomplete type
"
How can I solve this problem?

I don't blame you for being confused, but you should really learn
to read your system's header files for yourself. Take a look at
/usr/include/arpa/inet.h, and notice that it includes features.h,
and then read the documentation at the top of that file. (The answer
to your question is to #define _GNU_SOURCE.)

And FYI, your question had nothing to do with standard C, and
everything to do with GCC options and POSIXy extensions. A newsgroup
devoted to GCC, or to Linux, would have been the correct place for
it.

HTH,
-Arthur
 
G

genoeffo

Arthur J. O'Dwyer ha scritto:
I don't blame you for being confused, but you should really learn
to read your system's header files for yourself. Take a look at
/usr/include/arpa/inet.h, and notice that it includes features.h,
and then read the documentation at the top of that file. (The answer
to your question is to #define _GNU_SOURCE.)
Thank you very much for your help,
Sorry for the Off Topic.
Gg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top