I have been learning more C++ recently and I had a similar question. Most libraries and headers seem to at least share a similar name, but the differences between "libm" and "math.h" really got me wondering how to more systematically determine this.
For both static and dynamic libraries, a tool I found helpful is readelf
and the command I like is this:
readelf --symbols <elf_file> | sed -ne '/FUNC/p'
So for my motivating example:
$ readelf --symbols /lib/libm.so.5 | sed -ne '/FUNC/p'
2: 0000000000000000 476 FUNC WEAK DEFAULT UND __cxa_finalize@FBSD_1.0 (8)
4: 0000000000000000 62 FUNC GLOBAL DEFAULT UND __isinf@FBSD_1.0 (8)
5: 0000000000000000 310 FUNC GLOBAL DEFAULT UND ldexp@FBSD_1.0 (8)
6: 0000000000000000 41 FUNC GLOBAL DEFAULT UND __isinff@FBSD_1.0 (8)
9: 0000000000000000 8 FUNC GLOBAL DEFAULT UND __tls_get_addr@FBSD_1.0 (8)
11: 0000000000000000 16 FUNC GLOBAL DEFAULT UND __stack_chk_fail@FBSD_1.0 (8)
12: 0000000000000000 53 FUNC GLOBAL DEFAULT UND __isinfl@FBSD_1.0 (8)
14: 0000000000000000 84 FUNC GLOBAL DEFAULT UND memset@FBSD_1.0 (8)
16: 0000000000004ed0 6 FUNC GLOBAL DEFAULT 12 lrintf@@FBSD_1.0
17: 0000000000019930 405 FUNC GLOBAL DEFAULT 12 log10l@@FBSD_1.3
18: 0000000000016590 600 FUNC GLOBAL DEFAULT 12 floorl@@FBSD_1.0
...
This command also works for ar
archive files like libcurl.a.
So here you can see the functions defined in the library. From there I visually compared the names in the final column like "log10l" with the ones in the header file /usr/include/math.h and found a high correspondence. It seems feasible to me to write a program based on this. Parse the final column of the readelf command above, and do a grep for all .h
files in /usr/include. I did not take it that far, though.
After doing some experiments related to my answer, I found that the sed
command could probably be refined a bit more for your needs as well. When I used readelf
on libcurl.a, non-global functions (so, ones that do not need to be included in header files) are also output.