Detecting the presence of C libraries (sys crate)

How are -sys crates detecting that a library is present in the standard locations, especially when the library is not known to pkg-config? I'm a bit surprised that there does not seem to be a crate to help with that (besides pkg-config, vcpkg) so many sys crates seem to roll their own search procedure to find whether the library is present on various systems. Did I miss something? Are there recommendations about how to proceed?

Enjoy the festive season!



Most -sys crates just use pkg-config combined with some library-specific alternatives (e.g. there's llvm-config and png-config that work like pkg-config, but just for their own library).

There's no universal tool for this, because C libraries themselves are not uniform. The rules vary between platforms (e.g. on macOS it depends if a library ships with the OS, whether the OS version is any good, and whether Brew has a better alternative) and details vary between libraries (e.g. libjpeg has multiple ABI versions).


Thanks for your fast reply.

It is good when possible — the library I'm interested in does not provide a .pc file nor a special tool (such as llvm-config).

Right but isn't the logic of finding where the library is (checking each the above possibilities — if enabled) often similar?

There was an attempt to make it automatic/universal: 2196-metabuild - The Rust RFC Book

I'm afraid there's a wide spectrum of "it depends", from just searching a handful of dirs for to a complete wild west where everything is broken in ways you haven't imagined.

If you target only dynamic linking, only Linux and similar-enough BSDs, with a library that has only one ABI, one name, then it's pretty simple, because the distros did all the hard work for you.

But beyond that, you start doing all of the work that Linux distros do, but as a Rust crate, and you're going to have to deal with all of the quirkiness of Windows, macOS, and maybe iOS and Android too.

1 Like

If library doesn't use pkgconfig then no, logic is not similar and is different from one library to another.

That's why linux distributions exist… only they just go the very well known way.

Indeed. It's funny to look on the way to do detection thingies (pkg-config) and then turn around and ask: why there no more “standards”.

Of course there are, C have bazillion standards, each one supported by one or two libraries.

1 Like

Thanks for your perspective @kornel. It is not always easy to decide what the best course of action is when a library doesn't use the standard tools to advertise itself...

Have you considered submitting a PR to the library to add a pkg-config?

Yes but I'm not familiar with writing pkg-config files and the library has sub-libraries that can individually be turned on/off so I'll need some time to work that out. I'll eventually probably submit a PR (instead of an issue for now) but, given it will certainly take more than a year to end up in Linux distributions, I also need a shorter term solution.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.