One things that I find troubling is trying to remember the name and location of a library file. Was it librng, librnng, lib_grng, and do I have static binaries as well as dynamic? Where is it?
Well, I wrote this handy dandy little perl script to help me out. Just copy all the following code, and paste it into
/usr/bin/findlib, then
chmod +x /usr/bin/findlib.
Then, in order to locate a library, all you have to do is:
Code:
~/c> findlib pbm
/usr/lib/libpbm.so.1
~/c> findlib --all pbm
/usr/lib/libpbm.so.1
/usr/lib/libpbm.so
/usr/lib/libpbm.so.1.0.0
/usr/lib/libpbm.a
~/c> findlib max
No match in /usr/local/lib /usr/X11R6/lib /usr/lib /usr/X11/lib
/programs/imageMagick/lib /lib /programs/apache/lib /programs/named/lib
/programs/openssl/lib /usr/i386-slackware-linux/lib /programs/mysql/lib
And now you have an easy way to find libraries and their paths! The code automatically looks in the three basic locations (/lib /usr/lib and /usr/local/lib) and also uses the contents of the file /etc/ld.so.conf, for any other dynamic library locations.
Code:
#!/usr/bin/perl
use strict;
use warnings;
#MAIN
{
my($libpath) = {}; # paths to all lib locations
my($ldsoconf) = '/etc/ld.so.conf'; # dynamic lib paths
my($found) = []; # matching lib names
my($findre) = ''; # regexp for matching
my($all) = 0; # show all defaults to no
# parse CL args quickly
#
for my $arg(@ARGV) {
$arg eq '--all' and $all = 1 and next;
$findre eq '' or $findre .= '|';
$findre .= $arg; # build regexp of all passed names
}
length($findre) < 1 and die "Usage:\n $0 [--all] partial_filename\n";
# add defaults paths
#
for my $path(qw(/lib /usr/lib /usr/local/lib)) {
defined($libpath->{$path}) or $libpath->{$path} = 1;
}
# add paths in ld.so.conf
#
open IN, "$ldsoconf" or die "Cannot open $ldsoconf\n";
while(<IN>) {
next if(/^s*$/);
chomp;
defined($libpath->{$_}) or $libpath->{$_} = 1;
}
close IN;
# parse through paths to find matches
#
for my $path(keys(%$libpath)) {
if(opendir DIR, "$path") {
my(@matches) = grep { /$findre/ } readdir DIR;
if(scalar(@matches) > 0) {
push @$found, $path . '/' . shift(@matches);
last if(!$all);
push @$found, map { "$path/$_" } @matches;
}
closedir DIR;
}
else {
warn "Unable to open dir $path: $!\n";
}
}
if(scalar(@$found) > 0) {
print join("\n", @$found), "\n";
}
else {
print "No match in ", join(' ', keys(%$libpath)), "\n";
}
exit;
}