Calling Custom path binaries using System command

Viewed 40

In a linux box my rpm's used to install my software binaries to a predefined "/opt//bin" and "/opt//lib" , and from some binaries(c executable) i used to call these binaries located in /opt//bin by harcoding its full path using system call. for example : system("/opt/<my_loc>/bin/myBin");

Now I would like to install my software to a custom path so what's the best approach to call the binaries from the new custom path ?

1 Answers

Stop hard-coding the path in each system call. Or at least use a #define so you can update it from your old path to your new path. Then you can generate a suitable config file at build-time via a script.

The better approach is to just configure your PATH environment variable correctly, so you can just tell it what binary you want to run. I order it like this: ~/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin (if your /bin and /sbin is not merged into /usr you will need to add those too). system() uses sh which on my system is dash so you would set your PATH in ~/.profile and/or /etc/profile.

I don't like a binary path per package, so I use the program stow to merge a bunch of per package directories in /usr/local/stow/$package/bin to /usr/local/bin.

Related