Pcap functions have "undefined reference"
Asked Answered
T

1

22

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'
Tirrell answered 24/11, 2014 at 15:11 Comment(2)
@NTN 1. linker errors are usually unrelated to header files, and 2. the error message is not "library not found". The issue is that the library is in the wrong place – it should be after the source file.Bound
Ohh yes, you could use -L/your/path/ to add new path to locate libGaskell
P
34

Move -lpcap to the end of the command line

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

Penchant answered 24/11, 2014 at 15:18 Comment(4)
As in gcc example_pcap.c -o example_pcap -lpcap (I added -o to get an executable with a better name than a.out)Gable
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?Tirrell
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'Tirrell
@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.Penchant

© 2022 - 2024 — McMap. All rights reserved.