Paul Moore Paul Moore - 7 months ago 41
C++ Question

How to find an exe in $PATH using Boost

I'm writing a C++ program using the Boost libraries. I need to be able to find the full path of an executable, given only the name. This would be the equivalent of the Unix shell's

utility, Windows'
, or Python's

Is there a way of doing this? I could of course write my own, by iterating over the PATH environment variable, but for portability I'd need to consider things like splitting on
, etc, and if there's a pre-written (and tested!) routine for this I'd prefer to use it.


There's nothing that directly implements this in boost. You could either:

  1. Read contents of PATH with getenv(), use Boost File System to deal with the paths in a portable manner, and handle the multi-path splitting manually - doesn't seem like the number of split characters could be insane, just a few alternatives
  2. invoke your shell's existing which program (or variant thereof)
  3. invoke a simple python program calling shutil.which, e.g. system("python -c \"import shutil; shutil.which('ls');\"")