views:

54

answers:

1

Hello,

I would like to install and use latest version (1.1.1) of the libpcap on CentOS 5.5 machine. I configured, compiled and installed new libpcap library by:

[dima@localhost libpcap-1.1.1]$ ./configure
[dima@localhost libpcap-1.1.1]$ make
[dima@localhost libpcap-1.1.1]$ sudo make install

But when I'm trying to link with libpcap shared library (using -lpcap linker flag) my application is linked to the old version of the libpcap library (verified using pcap_lib_version() API call.

What I need to do in order to link with new libpcap library?

Thanks in advance

+2  A: 

Configuring without specifying --prefix or other installation location options will, by default, have installed the new libpcap in /usr/local/lib. Presumably the old version that you're trying to override is the system CentOS one, and so is in /usr/lib.

Therefore it would appear that the linker is searching /usr/lib before /usr/local/lib.

You can see exactly which libpcap is being linked against by adding -Wl,-Map,foo.map to the GCC command that is linking your application, and grepping the resulting foo.map file for libpcap.

You can see the library search path that the linker is using by looking at the output of (both of)

gcc -print-search-dirs | grep ^libraries
ld --verbose | grep SEARCH_DIR

If /usr/lib appears before /usr/local/lib, you can add -L/usr/local/lib to your link command to reorder them and pick up your new library. But really that's a hack.


All that was for the case of a problem at link time. Depending on how this shared library is versioned, the real problem may be occurring when you run your application, during dynamic linking. Or perhaps a bit of both.

What path do you see listed for libpcap when you run ldd on your application? What about when you've built your application with -L/usr/local/lib?

ldd yourapp

To force the dynamic linker to find your shared library in /usr/local/lib, you might like to look into the linker's -rpath option, or the LD_LIBRARY_PATH environment variable. Adding -L/usr/local/lib -Wl,-rpath,/usr/local/lib to your link command will surely ensure your new version of the library gets used. But both -rpath and LD_LIBRARY_PATH are even more of a hack, and introduce other problems if you try to give your application binary to someone else without careful consideration.


The non-hackish approach to all this is to ensure that you install the new shared library into a directory that is already known to the system. That probably means /usr/lib if that is where the existing version of the library is.

You can do this by adding --prefix=/usr to the configure command when you build libpcap. After installing the new libpcap there, you should be able to compile and link your application against it without any extra linker options.

However this interferes with package management, so will cause other problems when updating via your package manager. So you might want to uninstall the system libpcap package first, or in general look into the right ways to replace system packages on CentOS.

John Marshall