Seth
Seth

Reputation: 80

Is there a C++ API Linux System call that tells you which shared libraries your executable has linked?

I am wondering if anyone knows a C++ system call that produces output similar to the ldd command. I am trying to get a list of all the shared libraries that the executable I am running has linked (just like ldd, but a C++ API). My end goal is to get the absolute paths of all the shared libraries my program has linked in.

Upvotes: 0

Views: 168

Answers (1)

yugr
yugr

Reputation: 21916

You can use dl_iterate_phdr:

#define _GNU_SOURCE
#include <link.h>
#include <stdlib.h>
#include <stdio.h>

static int callback(struct dl_phdr_info *info, size_t size, void *data) {
  printf("name=%s (%d segments)\n", info->dlpi_name, info->dlpi_phnum);
  return 0;
}

int main() {
  dl_iterate_phdr(callback, NULL);
  return 0;
}

This program will produce the following output:

name= (9 segments)
name= (4 segments)
name=/lib64/libdl.so.2 (7 segments)
name=/lib64/libc.so.6 (10 segments)
name=/lib64/ld-linux-x86-64.so.2 (7 segments)

Upvotes: 2

Related Questions