What's the best way to check for external applications?

I have an application that has additional options depending on whether the user has the software installed.

On Linux, what's the best way to determine if something like python and PyUsb is installed?

I am developing a C ++ Qt application if that helps.

0


source to share


4 answers


It is inefficient (requires forking and exec'ing / bin / sh). There must be a better way! But as a general approach ... There is always system ().

(Don't forget to use WEXITSTATUS ()! Don't forget to make your programs run smooth!)

#define SHOW(X)  cout << # X " = " << (X) << endl

int main()
{
  int status;

  SHOW( status = system( "which grep > /dev/null 2>&1" ) );
  SHOW( WEXITSTATUS(status) );

  SHOW( status = system( "which no_matching_file > /dev/null 2>&1" ) );
  SHOW( WEXITSTATUS(status) );
}

      



There is also popen (), which can be useful for capturing the output from programs to check version numbers, libraries, or whatever.

If you need bi-directional (read and write) access to a subprocess, it is best to use pipe (), fork (), exec (), close (), and dup2 ().

+1


source


You can require them to be on the path, etc. Check for the executables you need (using which

or similar). You can also use the arguments of the executables and check the required versions if needed.



+1


source


I don't know how to do this for Linux in general, as each distribution can have its own package manager. But, assuming you want to support the most popular distributions, you can query their package manager for the installed software (I would suggest supporting apt-get, rpm, and yum as a starting point) and parse the output to find packages you recognize. Each manager has a way to list installed packages, my suggestion as a start:

apt-get --no-act check
rpm -qa
yum list installed

      

+1


source


Another possibility is to present all the features to the user and prompt them to install additional features if they try to use them (for example, see http://0install.net ).

0


source







All Articles