19

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'
halfer
  • 18,701
  • 13
  • 79
  • 158
Yahya Uddin
  • 18,489
  • 26
  • 104
  • 189

1 Answers1

28

Move -lpcap to the end of the command line

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

Community
  • 1
  • 1
sehe
  • 328,274
  • 43
  • 416
  • 565
  • 1
    As in `gcc example_pcap.c -o example_pcap -lpcap` (I added -o to get an executable with a better name than `a.out`) – Jite Nov 24 '14 at 15:26
  • This worked but now I'm a little confused. As part of my coursework they have already given me the make file. Now the make file has the -lpcap parameter BEFORE the rest. Did they make a mistake, or is there a way to make sure it stays at the start? – Yahya Uddin Nov 24 '14 at 15:31
  • linking ../build/idsniff gcc -lpcap -lpthread -o ../build/idsniff ../build/analysis.o ../build/dispatch.o ../build/main.o ../build/sniff.o ../build/sniff.o: In function `sniff': /home/yahya/workspace/osCoursework/src/sniff.c:15: undefined reference to `pcap_open_live' /home/yahya/workspace/osCoursework/src/sniff.c:27: undefined reference to `pcap_next' /home/yahya/workspace/osCoursework/src/sniff.c:31: undefined reference to `pcap_geterr' – Yahya Uddin Nov 24 '14 at 15:33
  • 1
    @YahyaUddin yes they made a mistake. It's possible that on the particular system they used back when they wrote the course material, it happened to work. – sehe Nov 24 '14 at 15:36