Determine via C ++ whether the program is installed on Linux

I would like to make a system call for a Linux program from my C ++ code, but I would like to check if the program is installed on the user's machine.

In Ubuntu, I can determine if the package associated with this program has been installed using a system call like dpkg -s gifsicle and analyze its output. gifsicle is the name of the program.

However, it is possible that the program (for example, gifsicle ) was compiled from the source and thus did not appear in the Ubuntu package repository.

What is a good programmatic way to determine if a program (e.g. gifsicle ) is available on a system running C ++ code?

+4
source share
5 answers

There is no standard package manager for Linux, so dpkg is definitely the wrong answer.

For security and correctness reasons, it is probably unwise to rely on the user's PATH to find the executable. Therefore, you should probably already use the full path (e.g. /usr/bin/gifsicle ) in your call to system .

If yes, then a simple answer to your question:

 if (access("/usr/bin/gifsicle", X_OK) == 0) { system("/usr/bin/gifsicle -my -args"); } else if (errno == EACCESS) { /* gifsicle not found */ } else { /* access() failed! Operating system is broken or Windows (or both) */ } 

(Bonus points if you enter /usr/bin/gifsicle into a variable)

The harder - but perhaps more "correct" - the answer is to avoid the system and make fork + execl yourself by checking execl to see if it leads to ENOENT or the like. However, announcing the abandonment of the parent process can be annoying.

+2
source

You can first call which .

The exit status indicates whether it can find the specified executable on the path.

+4
source

In principle, to cover the case when the program is installed manually and is not registered in the database of installed packages, you will have to scan the entire file system to ensure that the program is not installed.

If you are sure that the program is in the user's PATH, you can call the which command (also using system() ).

However, the general solution to this is to allow the user to redefine the path to the executable using the configuration parameter. For example, Doxygen can be configured to call points to generate diagrams. By default, it tries to call dot as it was in the PATH environment variable. If it cannot be found, it warns the user that he cannot find the dot program and that the DOT_PATH configuration DOT_PATH not set. The advantage of this solution is its simplicity and work with other systems.

+1
source

As you say, it’s not trivial to determine if something is installed. In fact, there is no clear definition of “established”; package managers are approaching, but not everything goes through the package manager.

Why not just try calling the executable? If the call fails and the system indicates that the executable was not found, just assume that it is not installed and / or unavailable - does it matter what? - and move on to an alternative alternative.

0
source

sounds like you are trying to configure configure script (or similar)

see autoconf

http://www.linuxselfhelp.com/gnu/autoconf/html_chapter/autoconf_3.html

-2
source

All Articles