使用 c++ 的不同 linux 发行版的问题可执行文件

发布于 2024-08-19 09:36:53 字数 337 浏览 9 评论 0原文

我有一个 C++ 代码,可以在我的 Linux 机器(Ubuntu Karmic)上完美运行。 当我尝试在另一个版本上运行它时,我缺少各种共享库。

有没有办法将所有共享库合并到单个可执行文件中?

编辑: 我想我问错了问题。我应该要求一种方法来静态链接我的可执行文件(当它已经构建时)。 我在 ermine & 中找到了答案。 statifier

I have a c++ code that runs perfect on my linux machine (Ubuntu Karmic).
When I try to run it on another version, I have all sort of shared libraries missing.

Is there any way to merge all shared libraries into single executable?

Edit:
I think I've asked the wrong question. I should have ask for a way to static-link my executable when it is already built.
I found the answer in ermine & statifier

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

╭⌒浅淡时光〆 2024-08-26 09:36:53

缺少共享库有 3 个可能的原因:

  • 您正在使用默认情况下在其他发行版上不存在的共享库,或者您已将它们安装在主机上,但没有在另一个发行版上安装,例如 libDBI.so
  • 您有过度的在链接时指定版本,例如 libz.so.1.2.3 并且另一台机器具有兼容的 API(主要版本 1),但不同的次要版本 2.3 它就可能适用于您的程序
  • ,只要它链接库的主要版本已更改, ,这意味着它与 libc.so.2libc.so 不兼容.1

修复方法是:

  • 不要链接您不需要的库,这些库可能不在不同的发行版上,或者,在其他计算机上安装附加库,手动或使它们成为安装程序包的依赖项(例如使用 RPM
  • )不要在命令行上如此严格地指定版本 - 链接 libz.so.1 而不是 libz.so.1.2.3
  • 针对不同的 libc 版本编译多个版本。

There are 3 possible reasons you have shared libraries missing:

  • you are using shared libraries which do not exist by default on the other distribution, or you have installed them on your host, but not the other one, e.g. libDBI.so
  • you have over-specified the version at link time, e.g. libz.so.1.2.3 and the other machine has an API compatible (major version 1) but different minor version 2.3, which would probably work with your program if only it would link
  • the major version of the library has changed, which means it is incompatible libc.so.2 vs libc.so.1.

The fixes are:

  • don't link libraries which you don't need that may not be on different distros, OR, install the additional libraries on the other machines, either manually or make them dependencies of your installer package (e.g. use RPM)
  • don't specify the versions so tightly on the command line - link libz.so.1 instead of libz.so.1.2.3.
  • compile multiple versions against different libc versions.
九歌凝 2024-08-26 09:36:53

您所描述的是使用静态库而不是共享库。

What you are describing is the use of static libraries instead of shared libraries.

ゃ人海孤独症 2024-08-26 09:36:53

对于这里提到的原始问题,有几种技术解决方案,例如

编译多个版本
不同的 libc 版本。

或者

安装附加库
其他机器

,但如果您处于 ISV 的位置,实际上只有一个明智的解决方案:

全新安装旧系统(例如,如果您的目标是桌面,则为 Ubuntu 6.x,也许可以追溯到 Red)如果您的目标是服务器,则为 Hat 9)并在此基础上构建您的软件。一般来说,库(当然还有 libc)是向后兼容的,因此在较新的系统上运行不会出现问题。

当然,如果您有非标准或最新版本的库依赖项,这并不能完全解决问题。在这种情况下,正如其他人所建议的那样,如果您想保持稳健,最好使用 dlopen() 并报告问题(或以减少的功能运行)。

There have been several technical solutions to the original problem noted here, e.g.

compile multiple versions against
different libc versions.

or

install the additional libraries on the
other machines

but if you're in the position of an ISV, there is really just one sane solution:

Get a clean install of an older system, (e.g. Ubuntu 6.x if you're targeting desktops, perhaps as far back as Red Hat 9 if you're targeting servers) and build your software on that. Generally libraries (and definitely libc) are backwards compatible, so you you won't have problems running on newer systems.

Of course if you have non-standard or recent-version lib dependencies this doesn't completely solve the problem. In that case, as other's have suggested, if you want to be robust it's better to dlopen() and report the problems (or run with reduced functionality).

故笙诉离歌 2024-08-26 09:36:53

我不太确定,但您可能想通过静态链接所有库来创建可执行文件。

I am not too sure, but you may want to create your executable by statically linking all the libraries.

许仙没带伞 2024-08-26 09:36:53

一种替代方法是使用dlopen()动态加载共享库如果加载失败,请正常退出,并显示可执行文件需要依赖库才能工作的消息。
然后用户可以安装适当的库。

One alternative is to dynamically load shared libraries using dlopen() and if it fails to load, exit gracefully with the message that the dependent library is required for the executable to work.
The user then may install the appropriate library.

奈何桥上唱咆哮 2024-08-26 09:36:53

另一种可能的解决方案是使用 statifier (http://statifier.sf.net) 或 Ermine (http://magicErmine.com)
它们都能够将动态可执行文件及其所需的所有库打包到一个独立的可执行文件中

Another possible solution is using statifier (http://statifier.sf.net) or Ermine (http://magicErmine.com)
Both of them are able pack dynamic executable and all of it's needed libraries into one self-containing executable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文