Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ubuntu: How to link a binary

I have a C++ code. I compiled it and I now have the binary xyz. Now everytime I need to execute the binary, I had to switch to the corresponding directory to execute it using ./xyz But how do I run the binary using a command say xyz from anywhere. How do I link this binary to a command in ubuntu. I currently use Ubuntu 10.10

like image 401
Vivek Avatar asked Jan 05 '11 19:01

Vivek


People also ask

How do I link a file in Ubuntu?

Ubuntu users utilize the “ln” command for creating symbolic links in their system. This command creates hard links by default. However, you can add the “-s” or the “–symbolic” option if you want to create a soft link.

How do I create a link to a folder in Ubuntu?

To create a symbolic link in Nautilus, press and hold the Ctrl and Shift keys on your keyboard. Drag and drop a file or folder to another location. Nautilus will create a symbolic link to the original file or folder at the location you drop the file or folder rather than moving the original file or folder.


2 Answers

Ubuntu sets your PATH environment variable to include ~/bin. So the easiest way to make xyz executable from anywhere is move xyz to ~/bin, or to make a symlink from ~/bin to the xyz's directory:

ln -s /path/to/xyz/directory/ ~/bin

Or, you could add /path/to/xyz/directory/ to your PATH environment variable.

like image 191
unutbu Avatar answered Oct 28 '22 06:10

unutbu


The problem is that Ubuntu doesn't know where to look for binary xyz.

You need to either add a path to your shell profile (it'll tell your shell where to look for programs like xyz) or add xyz to a directory that's already included in your path.

For example, /usr/bin is one place where binaries are stored normally. However, I'm not entirely sure what the generally accepted place to install new binaries is (/usr/bin might be generally reserved for system binaries).

Ubuntu by default uses a bash shell. In your home directory (~), you can check/edit your .profile file and either edit or see what directories are added to your PATH variable.

like image 26
Poff Avatar answered Oct 28 '22 08:10

Poff