Valhalla Legends Forums Archive | General Programming | libpcap Issue [Solved]

AuthorMessageTime
Yegg
I'm having trouble getting libpcap to function properly. I'm on Linux, so yes I have the correct version of pcap. However, various functions seems to be missing from pcap.h. All I can come up with is that certain files were missing when libpcap was installed. Oddly enough though, when I ran configure that came with the libpcap package, it didn't complain about any serious issue. There were a few things that it checked for which were missing, but that seems pretty normal to me. Anyways, when I try to compile with gcc I receive the following:

[code][ryan@localhost Desktop]$ gcc ./test.c -o ./test
/tmp/ccwIyBV7.o(.text+0x2a): In function `main':
: undefined reference to `pcap_lookupdev'
/tmp/ccwIyBV7.o(.text+0x87): In function `main':
: undefined reference to `pcap_lookupnet'
collect2: ld returned 1 exit status[/code]

When gcc complains that an undefined reference has been made to something, I am assuming that it could not locate the source of that function? If so, then it must be because pcap.h is missing something?

Anyways, I'd really appreciated help with this. I tried compiling with two different sources which came from two tutorials located on tcpdump's site. The smaller program is as follows:

[code] #include <stdio.h>
#include <pcap.h>

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

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

With this code, it is pcap_lookupdev() that is missing.

Hopefully someone knows why this is so.


Update: My issue has been solved. I found a forum, which was in a very strange language, however they had the exact same issue. It turns out that you needed some extra arguments for gcc when compiling the application.

[code][ryan@localhost Desktop]$ gcc ./test.c -o ./test -lpcap -L/usr/lib[/code]
Would compile the code I showed.

The site url is:
http://bsdguru.org/dyskusja/viewtopic.php?t=4887&sid=de536a3019934b061465f75aee7b3ec6
February 21, 2006, 5:25 PM
Grok
Thank you for updating it with your solution.
February 21, 2006, 5:55 PM
Kp
-L/usr/lib is not required unless you've done something very strange to your compiler.  /usr/lib is part of the default search path.  -lpcap is required since libpcap.a is not part of the default list of libraries.  There was nothing wrong with your header file.  The problem was that your linker (ld -- which gcc was invoking for you) was unable to find the implementation of those functions in the libraries available to it.  By adding -lpcap, you led it to search libpcap.a as well, so that it now finds the functions it needed.  You could've also resolved the problem by adding /usr/lib/libpcap.a to the command line (or wherever you put libpcap.a), although what you did is generally considered a nicer solution for system libraries.
February 22, 2006, 3:06 AM

Search