I would like to make a system call to a Linux program from my C++ code, but I would like to check whether the program is installed on the user’s machine first.
In Ubuntu, I can determine whether a package associated with that program was installed using a system call like dpkg -s gifsicle and parse its output. gifsicle here is the program name.
However, it’s possible that the program (e.g. gifsicle) was compiled from source and thus does not appear in the Ubuntu package repository.
What’s a good programmatic way of determining whether a program (e.g. gifsicle) is available on the system executing the C++ code?
There is no standard package manager for Linux, so
dpkgis definitely the wrong answer.For security and correctness reasons, it is probably unwise to rely on the user’s PATH to locate an executable. So you probably should already be using the fully-qualified path (e.g.
/usr/bin/gifsicle) in your call tosystem.If so, the easy answer to your question is:
(Bonus points if you put
/usr/bin/gifsicleinto a variable)The harder — but arguably “more correct” — answer is to avoid
systemand dofork+execlyourself, checking theexeclto see if it results inENOENTor similar. Communicating the failure back to the parent process could be annoying, though.