Yahya Uddin
Yahya Uddin

Reputation: 28931

Pcap functions have "undefined reference"

I'm trying to go through this tutorial: http://www.tcpdump.org/pcap.html

Now I have install pcap (code hints and all that is working) using :

sudo apt-get install libpcap-dev

and so far I have the following code (file name is example_pcap.c):

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

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

    dev = pcap_lookupdev(errbuf);

    return 0;
}

According to many questions I have seen already they said to compile it using this:

gcc -lpcap example_pcap.c -o example_pcap

However I still get the following error:

example_pcap.c:(.text+0x32): undefined reference to `pcap_lookupdev'

Upvotes: 22

Views: 29601

Answers (1)

sehe
sehe

Reputation: 393769

Move -lpcap to the end of the command line

See Why does the order in which libraries are linked sometimes cause errors in GCC?

Upvotes: 34

Related Questions