Three Ways to Bind: Inline::C vs. Affix vs. FFI::Platypus #5
sanko
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
You've installed a native library with
Alien::Xrepo. Now what? The whole point is to call a C function from Perl but there are three very different ways to do it. This article walks through the same task (callingzlibVersion()from Perl) using three different binding tools, shows the trade-offs, and helps you pick the right one.All three examples assume:
After this,
$zlib->libpathpoints tozlib.dll/libz.so,$zlib->includedirshaszlib.h, and$zlib->linkshas the-lzflag.Affix: one-liner FFI
Best for: Single functions, quick prototypes, CPAN-distributed bindings.
affixis a declarative one-liner: tell it the shared library, function name, argument types, and return type. No XS compilation, no compiler needed at runtime.Pros
libpathfromAlien::Xrepoplugs directly intoaffix.Cons
affixline.Affix::Wrap(parses C headers via a compiler), which can be very slow on some platforms.FFI::Platypus: the classic
Best for: Old code you wrote before Affix existed?
FFI::Platypusis an established workhorse. It reads the samelibpathand also accepts an explicitlibcall, which is handy when you need to load several libraries.Pros
Cons
lib, thenattach).FFI::Platypusto be installed.Inline::C: full C extensions
Best for: Performance-critical paths, code that needs headers and multiple libraries linked together, XS authorship.
Inline::Ccompiles a real C extension at build time. You get full access to the C preprocessor (macros, headers) and can call any function, define any struct, and chain any number of libraries in a single XS.Pros
Cons
(void)in parameter lists; Inline::C's parser mishandles that signature.Comparison
affixper lib)libcalls)INC/LIBSWhich should you choose?
In every case,
Alien::Xrepohands youlibpath,includedirs,linkdirs, andlinks; the same four things each binding tool needs to find the library. Swap one binding for another without changing the install step.All reactions