Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Unexpected behavior when capturing shell output in C++ program

This thread is locked; no one can reply to it. rss feed Print
Unexpected behavior when capturing shell output in C++ program
Mark Oates
Member #1,146
March 2001
avatar

Hey all, I ran into a peculiar oddity and I'm sure you brilliant minds might have some insight.

I'm writing some "tests" that assert different conditions regarding my entire computer system's state. I frequently hop between my laptop/desktop/etc, both undergo frequent changes and need to be kept in sync. Changes include synchronicity between multiple project repos, ruby versions, OS updates/versioning, .dotfiles, etc.

I need to be confidant that my system has the latest compiled versions of programs that I'm developing, and is, essentially, not going to blow up with a random error, or provide unexpected or outdated behavior while I'm in the middle of doing some work.

So, whenever I finish for the day, the first thing I do is run my program status and make sure everything is pushed, there are no unstaged files, etc. Whenever I hop onto a different computer, I run status and then update/sync repos etc until everything is in the proper state.

One of these tests checks the current version of clang. The reason I wrote this test is because one computer does have `#include <filesystem>` while the other does not, despite both using clang and both using the same build flags. This, being a point of discrepancy that could occur between systems, is the perfect condition to write one of these new tests.

So when in the terminal, I type the shell command "clang -v", it outputs the usual output. For the test itself, in a C++ program, I simply call a shell command "clang -v", then extract the first line from the return output and compare it to an expected string value. If the value matches the string "Apple clang version 11.0.0 (clang-1100.0.33.8)", then the test passes, otherwise the test fails and reports the discrepancy between the expected and actual values.

Ok, now here's the weird part. When I run the "clang -v" command through the c++ component, the output dumps to the terminal as normal, but the return value is not captured (The calling component results in holding an empty string for the return value). However... when I use the shell command "clang --version", the output is captured.

Why would the output of "clang --version" be captured while the output of "clang -v" not? Both appear to have the same behavior in the shell.

Here's the component, ShellCommandExecutorWithCallback, that handles this capture:

Header: Blast/ShellCommandExecutorWithCallback.hpp
Source: Blast/ShellCommandExecutorWithCallback.cpp

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

torhu
Member #2,727
September 2002
avatar

Maybe `clang -v` outputs to stderr and not stdout, since you are not giving it a valid command line? Assuming that -v just means verbose. Anyway, I would check stderr vs. stdout here

Mark Oates
Member #1,146
March 2001
avatar

That is what man clang indicates. Nevertheless, "clang -v" still manages to output all the version info in the exact same format as "clang --version". I would think it odd that it should choose stderr as an output destination for verbose output.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

torhu
Member #2,727
September 2002
avatar

Well, just `clang -v` is not a command, it will see that as an error, and then output some usage info, etc. But with `clang --version` your are asking it to output version information.

I haven't used clang much, just guessing based on the behavior of other tools.

Go to: