LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-22-2005, 03:38 PM   #1
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
handy perl script for finding libraries


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;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
./configure not finding KDE libraries MadCowDzz SUSE / openSUSE 3 11-21-2005 06:49 AM
finding libraries [gaim installation] LordFrancis Linux - Newbie 4 10-05-2004 09:18 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
Program not finding java VM libraries gtanghookup Linux - Software 5 09-04-2003 01:20 AM
Help finding libraries biggiefatts Linux - Software 1 11-19-2002 12:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration