Linux Ubuntu:如何链接二进制文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4608496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:23:08  来源:igfitidea点击:

Ubuntu: How to link a binary

linuxubuntubinaryfiles

提问by Vivek

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

我有一个 C++ 代码。我编译了它,现在我有了二进制 xyz。现在,每次我需要执行二进制文件时,我都必须切换到相应的目录才能使用 ./xyz 执行它,但是如何从任何地方使用 xyz 命令运行二进制文件。如何将此二进制文件链接到 ubuntu 中的命令。我目前使用 Ubuntu 10.10

采纳答案by unutbu

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

Ubuntu 将您的 PATH 环境变量设置为包含~/bin. 因此,xyz从任何地方创建可执行文件的最简单方法是移动xyz~/bin,或者从 ~/bin 创建一个符号链接到xyz's 目录:

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

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

或者,您可以添加/path/to/xyz/directory/到 PATH环境变量中。

回答by Poff

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. 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 是正常存储二进制文件的地方。但是,我不完全确定安装新二进制文件的普遍接受的位置是什么(/usr/bin 通常可能是为系统二进制文件保留的)。

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.

Ubuntu 默认使用 bash shell。在您的主目录 (~) 中,您可以检查/编辑您的 .profile 文件并编辑或查看将哪些目录添加到您的 PATH 变量中。