How can I execute a system command with C++ and capture its output and the status. It should look something like this:
Response launch(std::string command);
int main()
{
auto response = launch("pkg-config --cflags spdlog");
std::cout << "status: " << response.get_status() << '\n'; // -> "status: 0"
std::cout << "output: " << response.get_output() << '\n'; // -> "output: -DSPDLOG_SHARED_LIB -DSPDLOG_COMPILED_LIB -DSPDLOG_FMT_EXTERNAL"
}
Using std::system
you can only get status. I also tried this solution but it only captures the output and it seems to be very "hacky" and not safe. There must be a better way to do it but I haven't found one yet. If there isn't a simple and portable solution, I would also use a library.