How to get a list of executable shared libraries from C ++?

I would like to programmatically get a list of shared libraries linked by my binary on Linux and Solaris. Right now I am posting on pmap (I cannot use lddbinary because it will not include dlopen'd libraries, and I cannot use plddbecause it is only Solaris):

std::ostringstream cmd;
cmd << "/usr/bin/pmap " << getpid() << " | awk '{ print $NF }' | grep '\\.so' | sort -u";
FILE* p = popen(cmd.str().c_str(), "r");

It's a bit hacky, but it works on both Solaris and Linux (the pmap output is slightly different, but the desired information is always in the last column). Is there a way to get the same information without shelling? Does it work on both platforms? I assume that the files are /proc/$PIDformatted differently between them, but I don’t know where the headers are usually located in order to help with their parsing (if there is a common place at all?).

+5
source share
1 answer

You can use the pmap 1234c command 1234as a process identifier.

( Linux) - /proc/self/maps.

cat /proc/self/maps

Linux: , cat .

, dladdr ( GNU/Linux Glibc), , .

+2

All Articles