Question

I am trying to compile the following code:

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

#define listen_device "eth0"

int main(int argc, char *argv[])
{
    char *dev, errbuf[PCAP_ERRBUF_SIZE];

    dev = pcap_lookupdev(errbuf); // err 'pcap_lookupdev undefined'
    if (dev == NULL) {
        fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
        return(2);
    }
    printf("Device: %s\n", dev);
    return(0);
}

Even know my pcap header defines pcap_lookupdev:

...
char    *pcap_lookupdev(char *);
...

I installed libpcap via:

sudo apt-get install libpcap0.8-dev  

Whenever I try to use a function that is declared in the pcap header, gcc gives the error function is undefined. Does anyone know what I am doing wrong?

Was it helpful?

Solution

How to compile libpcap applications under Ubuntu:

  1. Install libpcap: sudo apt-get install libpcap0.8-dev
  2. setup eclipse linker to handle libpcap:

    Project -> properties -> build settings -> linker -> libraries -> add library

All you have to do is type in pcap.

To compile from the command line all you have to do is:

gcc -o {output} file.c -lpcap
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top