简体   繁体   English

Ubuntu:如何链接二进制文件

[英]Ubuntu: How to link a binary

I have a C++ code. 我有一个C ++代码。 I compiled it and I now have the binary xyz. 我编译它,我现在有二进制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. 现在,每次我需要执行二进制文件时,我都必须切换到相应的目录来使用./xyz执行它。但是如何使用命令xyz从任何地方运行二进制文件。 How do I link this binary to a command in ubuntu. 如何将此二进制文件链接到ubuntu中的命令。 I currently use Ubuntu 10.10 我目前使用的是Ubuntu 10.10

Ubuntu sets your PATH environment variable to include ~/bin . Ubuntu将您的PATH环境变量设置为包含~/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: 因此,从任何地方制作xyz可执行文件的最简单方法是将xyz移动到~/bin ,或者从〜/ bin创建一个符号链接到xyz的目录:

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

Or, you could add /path/to/xyz/directory/ to your PATH environment variable. 或者,您可以/path/to/xyz/directory/到PATH环境变量中。

The problem is that Ubuntu doesn't know where to look for binary xyz. 问题是Ubuntu不知道在哪里寻找二进制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. 您需要为shell配置文件添加一个路径(它将告诉您的shell在哪里查找xyz等程序)或者将xyz添加到已包含在路径中的目录中。

For example, /usr/bin is one place where binaries are stored normally. 例如,/ usr / bin是正常存储二进制文件的地方。 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). 但是,我不完全确定安装新二进制文件的普遍接受的地方是(/ usr / bin通常可以保留给系统二进制文件)。

Ubuntu by default uses a bash shell. 默认情况下,Ubuntu使用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. 在您的主目录(〜)中,您可以检查/编辑.profile文件,并编辑或查看添加到PATH变量的目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM