The name of the file which contains the lib and the name used in -l do not always match. So how can I figure out the name used when linking? Usually I googled, but it should be somewhere in the system I think..
views:
18answers:
2When linking a program with gcc on Linux or OSX how can I figure out how it's called when using -l
My answer's a bit messy, sorry about that. I'm pretty sure there's a better solution but this should help nevertheless.
During link-time, AFAIK the linker searches all library paths it knows about and looks for the arch-dependent library name, on Linux "-l foo" would search for "libfoo.so", on a Mac "libfoo.dylib". Now if you look for example in /usr/lib, you'll notice that there are a lot of symlinks. For example, if you have libfoo.so.1.2.3 there should also be symlinks libfoo.so.1.2 -> libfoo.so.1.2.3, libfoo.so.1 -> libfoo.so.1.2 and libfoo.so -> libfoo.so.1. The idea behind this is to support various versions. So if you need to know which file is used I suggest you do this:
Add "-v" to your LDFLAGS or directly to your call to gcc. This will result in a noisy output of gcc, the interesting thing is the call to "collect2". It has various arguments -L... and these are the directories the linker searches for libraries. You will also see -l... (lower case ell). You need to look into the -L directories for libraries given in -l and follow their symlinks.
If you need to know which library is used during runtime: that's a lot easier. Just run ldd some_program
, it will tell you which libraries are used. It actually calls the program so that the dynamic linker kicks in, but passes an environment variable that makes the linker print out what it has loaded and exit the program before even starting it. On a Mac, use otool -L some_program
.
For a running program, you need to find out the program's PID. Then do cat /proc/pid_of_program/maps
. That gives you the memory map. The interesting part is the right hand column which lists the loaded libraries (because they get mmap'ed into the process). I don't know the equivalent for that on a Mac.