Quite often I am using third party snippets of C code that uses a Unix/Linux C library, an example #include <glib.h>
, #include <net/if.h>
. And they require me to know the specific library file name or pkg-config name, an example -lglib-2.0
, -lbluetooth
, pkg-config --libs dbus-1
. Note that a lot of them differ from the header file having a '2.0', '1' so you just can't guess it.
How the heck can I find out the exact name of the library file an API/library has? Using my examples above; how do I find that #include <glib.h>
's library file is called -lglib-2.0
? It's quite frustrating that the online references for Glib/dbus/all of them don't just say "the name of the library file is glib-2.0".
Are there any terminal commands I can use to find this out? Are there utilities I can use to find this out? Online API/Library references are really bad for finding this out.
If I want to find where hci.h
is located I can easily just go locate hci.h
and find its location. Is there anything like a terminal command for finding out the library name?
Best Answer
There's no general way to know which library is required to use a particular function. You need to look at the documentation of that library. A well-written tutorial or API reference should tell you, that's its job.
You can at least get an idea of what library package is required: it's the same library package that contains the header file. How to determine which package contains the header file depends on the distribution, for example
dpkg -S /usr/include/glib-2.0/glib.h
on Debian/Ubuntu/Mint/…,rpm -qf /usr/include/glib-2.0/glib.h
on RHEL/CentOS/Fedora/…, etc. Once you know the library package, list its contents to find what.so
files it contains, e.g.With Glib, there are several
.so
files, and there's no automatic way to tell which ones are required for a particular function (there may be more than one).If the library uses
pkg-config
, you should use it rather than hard-coding header and library paths. Not all libraries use it however. You can normally tell because your distribution's library package should depend on itspkg-config
package (e.g.dpkg -s libglib2.0-dev |grep '^Depends:.*pkg-config'
).If the documentation is really bad, try listing the symbols defined by the library:
This isn't a sure-fire way because it won't tell you if some other library is needed as well.