PDA

View Full Version : libpcap and OS X?


g3joel
July 5th, 2003, 09:57 AM
I'm trying to compile a simple C program which relies on the libpcap library, except everytime I try to compile it i get the error:

ld: Undefined symbols:
_pcap_lookupdev

Here is the C code:
#include [pcap.h]
#include [stdio.h]

int main()
{
char *dev, errbuf[PCAP_ERRBUF_SIZE];
dev = pcap_lookupdev(errbuf);
printf("Device %s\n", dev);
return(0);
}

It sounds like the linker is having trouble locating the *pcap_lookupdev(char *) function, however it is clearly present in the pcap.h header file. libcap is also properly installed (I'm pretty sure anyway).

Does anyone have any advice?

Captain Code
July 5th, 2003, 10:31 AM
I have no idea what the libpcap library does, but as a guess, does it use it's own namespace? If yes, then try namespace::pcap_lookupdev(errbuf);
where namespace is whatever that library uses.

g3joel
July 5th, 2003, 10:59 AM
What is a namespace, and how would I go about finding out what it is for libpcap?

P.S. libpcap is the library which programs like tcpdump, etherpeek, ntop, etc., use to capture and analyse network packets from a network device.

wadesworld
July 5th, 2003, 07:42 PM
Yep, that's very strange. Running "nm" on the library clearly shows it's not in there.

I'd contact Assar Westerlund, whose email address you can find in:

http://www.opensource.apple.com/darwinsource/10.2.6/libpcap/libpcap/CHANGES

Since he made the last changes to that routine, maybe he can tell you what the story is.

It may in fact, be a bug.

Make sure to report back what you find out.

Wade

bracken
July 5th, 2003, 11:01 PM
I got it to compile using "cc temp.c -o temp -lpcap" ... it gave me "Device en0" when ran.

(I have libpcap installed via Fink.)

g3joel
July 6th, 2003, 12:21 AM
Thanks for the link wadesworld, but I'm not sure it's entirely that guy's fault. I tried using some other pcap routines in my code which were also designated "undefined symbols" by the linker. That is, until I tried bracken's method of using the "-l" flag which seems to work for me too. One question though, is it always necessary to use this flag when linking against a library like pcap? I've never linked against a library before :p

Thanks for your input guys! ;)

anarchie
July 6th, 2003, 03:36 AM
One question though, is it always necessary to use this flag when linking against a library like pcap? I've never linked against a library before

Yes. The -l flag is passed to gcc (and indirectly, the linker 'ld') indicating that it should link in a library. When the linker encounters -l, it takes the following characters and uses them to construct filenames to search for the library as. It does this by prepending 'lib' and appending first '.dylib' for dynamically loaded libraries, then '.a' for statically linked libraries. So, by supplying -lpcap, ld would first look for libpcap.dylib, then libpcap.a. The usual place where ld searches for libraries is /usr/lib.

Running "nm" on the library clearly shows it's not in there.

Uh, it's in there. Did you use grep? ;)

What is a namespace, and how would I go about finding out what it is for libpcap?

In C++, a namespace serves to separate the scope of global identifiers, i.e. functions - if you had two functions both named "foobar", you could put one of them in the namespace "baz" and the other in the namespace "xyzzy", and be able to signify the required one by prepending its namespace and the scope-resolution operator, '::'. Since pcap is a C library, it has no notion of namespaces.

wadesworld
July 6th, 2003, 01:24 PM
Thanks for the link wadesworld, but I'm not sure it's entirely that guy's fault.


I wasn't suggesting it was his fault - just that he has intimate knowledge of the code.


I tried using some other pcap routines in my code which were also designated "undefined symbols" by the linker. That is, until I tried bracken's method of using the "-l" flag which seems to work for me too. One question though, is it always necessary to use this flag when linking against a library like pcap? I've never linked against a library before


Doh! I assumed you were showing a case of the only routine which didn't link, not that it was the only routine you were trying to link.

Yes, you always have to tell the compiler to actually link against a library you're trying to use.

Uh, it's in there. Did you use grep?

Yes, but looking back in my history, I made a typo - my apologies for confusing folks on that one.

Wade