1

I am trying to make a software in "C++" for linux that reads the console output of the ldd console application. I would like to know if there is any '.so' library in the shared files of the system or another way of purely read the output of this command in console. Here is an example of the output of the command:

ldd ./echo
    linux-vdso.so.1 =>  (0x00007fffdd8da000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe95daf4000)
    /lib64/ld-linux-x86-64.so.2 (0x000055a6179a6000)

This command print a list of the dependencies and the locations that has a binary file. I want to save this output in a variable or something else for being formatted later.

Jorge Luis
  • 902
  • 12
  • 25
  • 4
    Have you ever piped the output of one executable into another? – NathanOliver Mar 22 '18 at 14:20
  • also consider `boost.process` https://stackoverflow.com/questions/3190514/popen-equivalent-in-c – Lanting Mar 22 '18 at 14:31
  • That's the point, I want to know if there is some way of doing that using a library on the "/usr/include" or just simply read it from the console and save it into a variable. I dont have much experience on linux programming. – Jorge Luis Mar 22 '18 at 14:33
  • 1
    Check this https://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c-using-posix – Victor Gubin Mar 22 '18 at 14:35

1 Answers1

1

For that, usually one has to run the program we want to get output from with a pipe function: popen().

string data;
FILE * stream;
const int max_buffer = 256;
char buffer[max_buffer];

    stream = popen(cmd.c_str(), "r");
    if (stream) {
        while (!feof(stream)) {
            if (fgets(buffer, max_buffer, stream) != NULL) {
                data.append(buffer);
            }
        }
        pclose(stream);
    }
}

This way you can get the output of ldd and do whatever you like with it.

There is other question you may find useful:

popen() writes output of command executed to cout

Manuel
  • 2,526
  • 1
  • 13
  • 17