Ask Your Question

Revision history [back]

I am sure there is a simpler way, but this is how I will do if I am in a pinch. Since echo $ROS_DISTRO will give me the information, then I will use a program to read the output of this command:

From this example I will make a slight modification accordingly.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

string GetStdoutFromCommand(string cmd) {

  string data;
  FILE * stream;
  const int max_buffer = 256;
  char buffer[max_buffer];
  cmd.append(" 2>&1");

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

int main() {

  string ros_version = GetStdoutFromCommand("echo $ROS_DISTRO");
  cout << "ROS version: " << ros_version << endl;
  return 0;
}