Click here to Skip to main content
15,886,822 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Have this error from main() Need help! :(


C++
#include "stdafx.h"
#include <pcap.h>


/* 4 bytes IP address */
typedef struct ip_address{
    u_char byte1;
    u_char byte2;
    u_char byte3;
    u_char byte4;
}ip_address;

/* IPv4 header */
typedef struct ip_header{
    u_char  ver_ihl;        // Version (4 bits) + Internet header length (4 bits)
    u_char  tos;            // Type of service 
    u_short tlen;           // Total length 
    u_short identification; // Identification
    u_short flags_fo;       // Flags (3 bits) + Fragment offset (13 bits)
    u_char  ttl;            // Time to live
    u_char  proto;          // Protocol
    u_short crc;            // Header checksum
    ip_address  saddr;      // Source address
    ip_address  daddr;      // Destination address
    u_int   op_pad;         // Option + Padding
}ip_header;

/* UDP header*/
typedef struct udp_header{
    u_short sport;          // Source port
    u_short dport;          // Destination port
    u_short len;            // Datagram length
    u_short crc;            // Checksum
}udp_header;

/* prototype of the packet handler */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);


main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
u_int netmask;
char packet_filter[] = "ip and udp";
struct bpf_program fcode;

    /* Retrieve the device list */
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
        exit(1);
    }
    
    /* Print the list */
    for(d=alldevs; d; d=d->next)
    {
        printf("%d. %s", ++i, d->name);
        if (d->description)
            printf(" (%s)\n", d->description);
        else
            printf(" (No description available)\n");
    }

    if(i==0)
    {
        printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
        return -1;
    }
Posted
Updated 24-May-20 12:32pm
v4
Comments
Sergey Alexandrovich Kryukov 6-May-13 3:24am    
In what line of code? No matter, locate this line and actually declare the variable with appropriate type.
—SA
Volynsky Alex 6-May-13 3:28am    
Do not see the class declaration pcap_if_t....
Try to add a class declaration before the main function...
For example:

class pcap_if_t;


P.S. main should return int

Quote:
main()

should be
C++
int main()
 
Share this answer
 
The error indicates the missing return type of the main() function. Because main() must return an int, use:
C++
int main()
{
    // ...
    return 0;
}
 
Share this answer
 
Comments
decayer23 6-May-13 3:38am    
This is the error i had after putting int main()

error LNK2019: unresolved external symbol "void __cdecl packet_handler(unsigned char *,struct pcap_pkthdr const *,unsigned char const *)" (?packet_handler@@YAXPAEPBUpcap_pkthdr@@PBE@Z) referenced in function _main
Jochen Arndt 6-May-13 3:46am    
That is another error which has been introduced later (my answer is for the initial question). You should not edit your questions in such a way that the context to answers may be lost.

However, the error is from the linker telling you that it can not find the packet_handler() function in object files (created by compiling source files) or a library.
PrafullaVedante 6-May-13 3:48am    
add .lib file inwhich packet_handler function is located in linker path .....

For visual studio you should add .lib file at Project Settings-->Linker--->Input
decayer23 6-May-13 3:57am    
sorry for that, its my first time posting and do you know what kind of .lib file should i add in? I have wpcap.lib and packet.lib in the linker input project already.
Jochen Arndt 6-May-13 4:15am    
It is a handler function that must be implemented by you. Your code looks like those from http://www.winpcap.org/docs/docs_41b5/html/group__wpcap__tut3.html. So just add the function as done there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900