in gcc compiler error: parse error before string constsnt
hi,
I got the following error when compiling a packet capture program. I checked it out with lot of people I not getting the problem.please help me .This is the first step in my project work. [root@CORE-2-02 bin]# gcc testcap2.c testcap2.c:3: parse error before string constant testcap2.c:4:29: warning: extra tokens at end of #include directive In file included from /usr/include/pcap.h:41, from testcap2.c:4: /usr/include/sys/time.h:62: parse error before '}' token this is the code I am trying to complile.I have all the header files installed. #include <stdio.h> #include <stdlib.h> extern "C" { #include <pcap.h> } #include <errno.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netinet/if_ether.h> int main(int argc, char **argv) { int i; char *dev; char errbuf[PCAP_ERRBUF_SIZE]; pcap_t* descr; const u_char *packet; struct pcap_pkthdr hdr; struct ether_header *eptr; u_char *ptr; dev = pcap_lookupdev(errbuf); if(dev == NULL) { printf("%s\n",errbuf); exit(1); } printf("DEV: %s\n",dev); descr = pcap_open_live(dev,BUFSIZ,0,-1,errbuf); if(descr == NULL) { printf("pcap_open_live(): %s\n",errbuf); exit(1); } packet = pcap_next(descr,&hdr); if(packet == NULL) { printf("Didn't grab packet\n"); exit(1); } printf("Grabbed packet of length %d\n",hdr.len); printf("Recieved at ..... %s\n",ctime((const time_t*)&hdr.ts.tv_sec)); printf("Ethernet address length is %d\n",ETHER_HDR_LEN); eptr = (struct ether_header *) packet; if (ntohs (eptr->ether_type) == ETHERTYPE_IP) { printf("Ethernet type hex:%x dec:%d is an IP packet\n", ntohs(eptr->ether_type), ntohs(eptr->ether_type)); }else if (ntohs (eptr->ether_type) == ETHERTYPE_ARP) { printf("Ethernet type hex:%x dec:%d is an ARP packet\n", ntohs(eptr->ether_type), ntohs(eptr->ether_type)); }else { printf("Ethernet type %x not IP", ntohs(eptr->ether_type)); exit(1); } ptr = eptr->ether_dhost; i = ETHER_ADDR_LEN; printf(" Destination Address: "); do{ printf("%s%x",(i == ETHER_ADDR_LEN) ? " " : ":",*ptr++); }while(--i>0); printf("\n"); ptr = eptr->ether_shost; i = ETHER_ADDR_LEN; printf(" Source Address: "); do{ printf("%s%x",(i == ETHER_ADDR_LEN) ? " " : ":",*ptr++); }while(--i>0); printf("\n"); return 0; } thanks regards cynthia |
Remove the extern "C" { before the include of pcap.h and the } after.
When compiling your program, add the option -lpcap to tell gcc to link your program with the pcap library. |
All times are GMT -5. The time now is 01:26 AM. |