-2

I am trying to do one small pcap program in c++ and I am getting an error mentioned above. Below is the program I have written.

#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<pcap.h>

void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);
{
    cout<<"Got a Packet"<<endl;
}

int main()
{
    pcap_t *handle;
    char errbuf[PCAP_ERRUF_SIZE];
    struct bpf_program fp;
    char filter_exp[] = "icmp";
    bpf_u_int32 net;

    //Step 1
    handle = pcap_open_live("enp0se", BUFSIZ, 1, 1000, errbuf);

    //Step 2
    pcap-compile(handle, &fp, filter_exp, 0, net);
    pcap_setfilter(handle, &fp);

    //Step 3
    pcap_loop(handle, -1, got_packet, NULL);
    pcap_close(handle);
    return 0;
}
  • 3
    The usual questions apply, 1) Do you have the file pcap.h on your computer? 2) If you do have you told your compiler where to find that file? In general compilers do not find header files automatically. If you use non-standard header files you often have to tell your compiler where to find them, it doesn't happen automatically. – john Dec 06 '22 at 11:36
  • I think I don't have that in my system, I thought it wil be included like other header files like stdio.h Please help me doing that. – Ashok kumar Dec 06 '22 at 11:46
  • pcap.h is non-standard. The answer below covers what you need to do. – john Dec 06 '22 at 12:13
  • Another point `` is also non-standard. The correct header file is ``. However you don't seem to be using it, so it can just be deleted. – john Dec 06 '22 at 12:16
  • You need to install the `libpcap` library, it's `libpcap-dev` or `libpcap-devel`, depending on your linux distro. – MaxPlankton Dec 06 '22 at 15:29

2 Answers2

1

This problem might be caused by a couple things:

  1. You don't have pcap lib installed. (note: you need something like pcap-devel, not just pcap)
  2. Your compiler can't find it so you will need to provide the lib header path using the -I flag.

Also, don't forget to use -lpcap when linking.

0

If you're developing a program that uses a particular library, you will need to have the header files that declare functions and variables, and define data types and constants, required by programs using that library.

If you're doing this on Linux:

Linux distributions usually have more than one installable package for a library:

  • The package whose name is the name of the library, such as "libpcap", includes shared libraries, which are required in order to run programs built with the shared version of the library, but does not include the header files.
  • The package whose name is the name of the library plus a suffix such as "-devel" or "-dev", includes the library's headers.

The idea is that:

  • the first package is installed if any programs linked with the shared version of the library is installed - typically, the packages for those programs include the first package for the library as a dependency, so that if the program's package is installed, the first package for the library is also installed;
  • the second package is only installed if the user wants to develop a program using the library.

Thus, as you're developing a program using libpcap, you'd have to, as others have noted, install the libpcap-devel or libpcap-dev or... package (the name would depend on the distribution you're using).

Note that this isn't something special about libpcap; it applies to other libraries. For example, just as, on Debian and derivatives such as Ubuntu, there's a libpcap-0.8 package (the "0.8" is there for historical reasons) and a libpcap-0.8-dev package (and a libpcap-dev package, which I think is a better-named alias for libpcap-0.8-dev) there's a libssl package and a libssl-dev package.

Operating systems other than Linux may do this differently. For example, macOS doesn't provide headers, but the software development kits in Xcode do, and you get headers for all the macOS APIs, including libpcap - no need to install individual packages, just install Xcode. I think the BSDs either always provide the headers or provide them if you install the compilers.

user16139739
  • 862
  • 3
  • 5