FFI::CheckLib(3)
π FFI::CheckLib π
Okay bestie, welcome to the User Contributed Perl Documentation for FFI::CheckLib β yes, the manual name is right there, we're fancy like that. This is the page that tells you how to check if a dynamic library is available for FFI, and it is lowkey iconic.
πΈ NAME πΈ
FFI::CheckLib β Check that a library is available for FFI.
β¨ VERSION β¨
version 0.31
π SYNOPSIS π
use FFI::CheckLib;
check_lib_or_exit( lib => 'jpeg', symbol => 'jinit_memory_mgr' );
check_lib_or_exit( lib => [ 'iconv', 'jpeg' ] );
# or prompt for path to library and then:
print "where to find jpeg library: ";
my $path = <STDIN>;
check_lib_or_exit( lib => 'jpeg', libpath => $path );
π DESCRIPTION π
Okay so this module checks whether a particular dynamic library is available for FFI to use. It's modeled heavily on Devel::CheckLib, but it will find dynamic libraries even when development packages are not installed. It also gives you find_lib, which returns the full path to the found dynamic library, and that path can be fed directly into FFI::Platypus or another FFI system.
Even though it's intended mainly for FFI modules via FFI::Platypus and similar, this module does not actually use any FFI to do its detection and probing. It has no non-core runtime dependencies. The test suite does depend on Test2::Suite.
π«Ά FUNCTIONS π«Ά
Okay so all of these take the same named parameters and are exported by default.
π find_lib π
my(@libs) = find_lib(%args);
This returns a list of dynamic libraries, or an empty list if none were found.
[version 0.05]
If called in scalar context it will return the first library found.
Arguments are key value pairs with these keys:
libβ Must be either a string with the name of a single library or a reference to an array of strings of library names. Depending on your platform,CheckLibwill prependlibor append.dllor.sowhen searching.
[version 0.11]
As a special case, if "*" is specified then any libs found will match.
libpathβ A string or array of additional paths to search for libraries.systempathβ [version0.11] A string or array of system paths to search for instead of lettingFFI::CheckLibdetermine the system path. You can set this to[]in order to not search any system paths.symbolβ A string or a list of symbol names that must be found.verifyβ A code reference used to verify a library really is the one that you want. It should take two arguments, which is the name of the library and the full path to the library pathname. It should return true if it is acceptable, and false otherwise. You can use this in conjunction withFFI::Platypusto determine if it is going to meet your needs. Example:
use FFI::CheckLib;
use FFI::Platypus;
my($lib) = find_lib(
lib => 'foo',
verify => sub {
my($name, $libpath) = @_;
my $ffi = FFI::Platypus->new;
$ffi->lib($libpath);
my $f = $ffi->function('foo_version', [] => 'int');
return $f->call() >= 500; # we accept version 500 or better
},
);
recursiveβ [version0.11] Recursively search for libraries in any non-system paths (those provided vialibpathabove).try_linker_scriptβ [version0.24] Some vendors provide.sofiles that are linker scripts that point to the real binary shared library. These linker scripts can be used bygccorclang, but are not directly usable byFFI::Platypusand friends. On select platforms, this option will use the linker command (ld) to attempt to resolve the real.sofor non-binary files. Since there is extra overhead this is off by default.
An example is libyaml on Red Hat based Linux distributions. On Debian these are handled with symlinks and no trickery is required.
alienβ [version0.25] If no libraries can be found, try the given aliens instead. The Alien classes specified must provide theAlien::Baseinterface for dynamic libraries, which is to say they should provide a method calleddynamic_libsthat returns a list of dynamic libraries.
[version 0.28]
In 0.28 and later, if the Alien is not installed then it will be ignored and this module will search in system or specified directories only. This module will still throw an exception, if the Alien doesn't look like a module name or if it does not provide a dynamic_libs method (which is implemented by all Alien::Base subclasses).
[version 0.30] [breaking change]
Starting with version 0.30, libraries provided by Aliens is preferred over the system libraries. The original thinking was that you want to prefer the system libraries because they are more likely to get patched with regular system updates. Unfortunately, the reason a module needs to install an Alien is likely because the system library is not new enough, so we now prefer the Aliens instead.
π assert_lib π
assert_lib(%args);
This behaves exactly the same as find_lib, except that instead of returning an empty list on failure it throws an exception.
π¦ check_lib_or_exit π¦
check_lib_or_exit(%args);
This behaves exactly the same as assert_lib, except that instead of dying, it warns (with exactly the same error message) and exits. This is intended for use in Makefile.PL or Build.PL.
π find_lib_or_exit π
[version 0.05]
my(@libs) = find_lib_or_exit(%args);
This behaves exactly the same as find_lib, except that if the library is not found, it will call exit with an appropriate diagnostic.
π§ find_lib_or_die π§
[version 0.06]
my(@libs) = find_lib_or_die(%args);
This behaves exactly the same as find_lib, except that if the library is not found, it will die with an appropriate diagnostic.
π check_lib π
my $bool = check_lib(%args);
This behaves exactly the same as find_lib, except that it returns true (1) on finding the appropriate libraries or false (0) otherwise.
π which π
[version 0.17]
my $path = which($name);
Return the path to the first library that matches the given name.
Not exported by default.
π where π
[version 0.17]
my @paths = where($name);
Return the paths to all the libraries that match the given name.
Not exported by default.
β has_symbols β
[version 0.17]
my $bool = has_symbols($path, @symbol_names);
Returns true if all of the symbols can be found in the dynamic library located at the given path. Can be useful in conjunction with verify with find_lib above.
Not exported by default.
πΈ system_path πΈ
[version 0.20]
my $path = FFI::CheckLib::system_path;
Returns the system path as a list reference. On some systems, this is PATH; on others it might be LD_LIBRARY_PATH; on still others it could be something completely different. So although you may add items to this list, you should probably do some careful consideration before you do so.
This function is not exportable, even on request.
πΏ ENVIRONMENT πΏ
FFI::CheckLib responds to these environment variables:
π FFI_CHECKLIB_PACKAGE π
On macOS platforms with Homebrew (http://brew.sh) and/or MacPorts (https://www.macports.org) installed, their corresponding lib paths will be automatically appended to $system_path. In case of having both managers installed, Homebrew will appear before.
This behaviour can be overridden using the environment variable FFI_CHECKLIB_PACKAGE.
Allowed values are:
"none"β Won't use either Homebrew's path nor MacPorts"homebrew"β Will append$(brew --prefix)/libto the system paths"macports"β Will appendport's default lib path
A comma separated list is also valid:
export FFI_CHECKLIB_PACKAGE=macports,homebrew
Order matters. So in this example, MacPorts' lib path appears before Homebrew's path.
π± FFI_CHECKLIB_PATH π±
List of directories that will be considered by FFI::CheckLib as additional "system directories". They will be searched before other system directories but after libpath. The variable is colon separated on Unix and semicolon separated on Windows. If you use this variable, FFI_CHECKLIB_PACKAGE will be ignored.
π³ PATH π³
On Windows the PATH environment variable will be used as a search path for libraries.
On some operating systems LD_LIBRARY_PATH, DYLD_LIBRARY_PATH, DYLD_FALLBACK_LIBRARY_PATH or others may be used as part of the search for dynamic libraries and may be used (indirectly) by FFI::CheckLib as well.
π FAQ π
Why not just use dlopen?
Calling dlopen on a library name and then dlclose immediately can tell you if you have the exact name of a library available on a system. It does have a number of drawbacks as well.
- No absolute or relative path β It only tells you that the library is somewhere on the system, not having the absolute or relative path makes it harder to generate useful diagnostics.
- POSIX only β This doesn't work on non-POSIX systems like Microsoft Windows. If you are using a POSIX emulation layer on Windows that provides
dlopen, like Cygwin, there are a number of gotchas there as well. Having a layer written in Perl handles this means that developers on Unix can develop FFI that will more likely work on these platforms without special casing them. - inconsistent implementations β Even on POSIX systems you have inconsistent implementations. OpenBSD for example don't usually include symlinks for
.sofiles meaning you need to know the exact.soversion. - non-system directories β By default
dlopenonly works for libraries in the system paths. Most platforms have a way of configuring the search for different non-system paths, but none of them are portable, and are usually discouraged anyway. Alien and friends need to do searches for dynamic libraries in non-system directories for"share"installs.
My 64-bit Perl is misconfigured and has 32-bit libraries in its search path. Is that a bug in FFI::CheckLib?
Nope.
The way FFI::CheckLib is implemented it won't work on AIX, HP-UX, OpenVMS or Plan 9.
I know for a fact that it doesn't work on AIX as currently implemented because I used to develop on AIX in the early 2000s, and I am aware of some of the technical challenges. There are probably other systems that it won't work on. I would love to add support for these platforms. Realistically these platforms have a tiny market share, and absent patches from users or the companies that own these operating systems (patches welcome), or hardware / CPU time donations, these platforms are unsupportable anyway.
π SEE ALSO π
FFI::Platypusβ call library functions dynamically without a compiler. iconic behavior, honestly β¨.Dist::Zilla::Plugin::FFI::CheckLibβ the Dist::Zilla plugin for this module. if you're in the Dist::Zilla cinematic universe, this is your girl π.
π AUTHOR π
Author: Graham Ollis β <plicease@cpan.org>
Contributors (the iconic group-chat credits):
- Bakkiaraj Murugesan (
bakkiaraj) - Dan Book (
grinnz,DBOOK) - Ilya Pavlov (
Ilya,ILUX) - Shawn Laffan (
SLAFFAN) - Petr PΓsaΕ (
ppisar) - Michael R. Davis (
MRDVT) - Shawn Laffan (
SLAFFAN) β yes, twice; we are not editing the guest list, bestie π - Carlos D. Γlvaro (
cdalvaro)
π COPYRIGHT AND LICENSE π
Legal tea time β: this software is copyright (c) 2014-2022 by Graham Ollis.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. periodt π«.
That's a wrap, bestie π«Ά. Footer deets: perl v5.42.0, dated 2022-10-01, for FFI::CheckLib(3). go forth and link things iconically π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-FFI-CheckLib 0.31-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms