简体   繁体   English

为多个平台构建linux二进制文件

[英]Building linux binaries for multiple platforms

Help me settle a score. 帮我解决一个分数。

I have a piece of software written in C++ that's meant to run on as many linux distributions as possible and I need to figure out a strategy that's effective. 我有一个用C ++编写的软件,它可以在尽可能多的Linux发行版上运行,我需要找出一个有效的策略。 I'm trying to ship binaries in this case not source code (might be good to know). 我试图在这种情况下发送二进制文件而不是源代码(可能很高兴知道)。 It's already a commercial product and I have intellectual property issues that prevent me from open sourcing the product but also means I have to deal with a myriad of GPL issues. 它已经是商业产品,我有知识产权问题阻止我公开采购产品,但也意味着我必须处理无数的GPL问题。

The current line of reasoning has been to pick a least common denominator and build everything off that. 目前的推理方法是选择一个最小公分母,然后将其完成。 That has two major implications that I find counter productive. 这有两个主要含义,我觉得反作用。

  1. The C++ support in old versions of GCC lacks some more modern C++ features. 旧版GCC中的C ++支持缺少一些更现代的C ++特性。
  2. The least common denominator entails Red Hat Enterprise Linux 4 (RHEL4) 最不常见的分母需要红帽企业Linux 4(RHEL4)

I definitely don't need the the entire C++11 feature set but I'd like to bring the C++ support up to that of Visual C++ 2010. I'm perusing the idea to use Clang/libc++ as opposed to GCC/libstdc++ where possible. 我绝对不需要整个C ++ 11功能集,但我想将C ++支持带到Visual C ++ 2010的支持。我正在尝试使用Clang / libc ++而不是GCC / libstdc ++在可能的情况。

RHEL4 doesn't seem to have extensive cross platform support for building C++ applications, more over, I have little insight in the stability of ABI across different versions of linux but I'm concerned that RHEL4 is more trouble than worth. RHEL4似乎没有为构建C ++应用程序提供广泛的跨平台支持,更重要的是,我对不同版本的linux中ABI的稳定性几乎一无所知,但我担心RHEL4比实际值更麻烦。 Trying to build for all distributions based on a few is not a viable strategy. 尝试基于少数几个版本构建所有版本并不是一个可行的策略。

I'm under the assumption that compiling software for different distributions of linux is best accomplished by compiling the software for the target platform with tools on the target platform. 我假设通过在目标平台上使用工具编译目标平台的软件来最好地完成为不同Linux发行版编译软件。 I'm also currently operating under the assumption that you'll run into vast portability issues cross linux platforms if you do not embrace this. 我目前也在假设如果你不接受这个问题,你会遇到跨越Linux平台的大量可移植性问题。 Not to talk about the many libraries that you can or cannot link with because of C++ ABI instability across platforms/distributions. 不要谈论由于跨平台/分布的C ++ ABI不稳定而导致您可以或不能链接的许多库。

But I could be wrong, I'd like to hear from the people that deal with this on a regular basis. 但我可能错了,我想听听那些定期处理这个问题的人。 What will work and why? 什么会起作用,为什么? or more importantly, what will not work? 或者更重要的是,哪些不起作用?

You may try to focus on a few major platforms rather than individual distributions. 您可以尝试专注于几个主要平台而不是单个发行版。 What I mean by that is to build on what I call the "foundational distros" (Debian and RedHat) and hope for the best on the others. 我的意思是建立在我称之为“基础发行版”(Debian和RedHat)的基础上,并希望在其他人身上做到最好。

Most likely, Debian binaries (that are statically linked) will run just fine on Ubuntu and Mint and other Debian derived distributions. 最有可能的是,Debian二进制文件(静态链接)在Ubuntu和Mint以及其他Debian派生的发行版上运行得很好。 RedHat binaries will probably run on Centos, Scientific Linux and SuSE Linux. RedHat二进制文件可能运行在Centos,Scientific Linux和SuSE Linux上。 If you care about less popular distros (Say you have a lot of customers running some uncommon Linux), and neither your Debian or RedHat executable works on them or can be made to work somehow, then setup a VM of that distro and build one executable specifically for that flavor. 如果你关心不太受欢迎的发行版(假设你有很多客户运行一些不常见的Linux),并且你的Debian或RedHat可执行文件都不能正常工作或者可以以某种方式工作,那么设置一个该发行版的虚拟机并构建一个可执行文件特别是那种味道。

I've taken this approach in the past and it has worked well. 我过去采用过这种方法,效果很好。

The best way to do that it to make your software some open-source free software (eg GPLv3+ license), then if your software is interesting enough it will become packaged in distributions (by the distribution maintainer). 这样做的最佳方法是使您的软件成为一些开源免费软件 (例如GPLv3 +许可证),然后如果您的软件足够有趣,它将被打包在发行版中(由发行版维护者)。

You always want to provide distribution packages (eg .deb files for Ubuntu or Debian) because these are the easiest to install. 您总是希望提供分发包(例如,Ubuntu或Debian的.deb文件),因为这些是最容易安装的。

If you still want to make proprietary software (but ask yourself if you will be able to sell, or even distribute gratis, successfully your software), you might take the following steps: 如果您仍想制作专有软件(但问问自​​己是否能够销售,甚至免费分发, 成功运行您的软件),您可以采取以下步骤:

  • compile a recent GCC compiler (eg 4.8) by enabling a static stdc++ & a static libgcc (most distribution provided GCC don't do that). 通过启用静态stdc ++和静态libgcc来编译最近的GCC编译器(例如4.8)(大多数分发提供GCC不这样做)。

  • link your program perhaps statically (but you might not be able to do this, eg for /etc/nsswitch.conf related functions, including getaddrinfo and related DNS services). 也许是静态链接你的程序(但你可能无法做到这一点,例如/etc/nsswitch.conf相关函数,包括getaddrinfo和相关的DNS服务)。

Even by linking all C++ related stuff statically you still depend on libc.so.6 and then you may have some Gnu Libc versioning issues (hence a binary compiled for a libc version 2.17 won't always run with a libc 2.16 or vice versa). 即使通过静态链接所有与C ++相关的东西,你仍然依赖于libc.so.6然后你可能会有一些Gnu Libc版本问题(因此为libc版本2.17编译的二进制文件不会总是用libc 2.16运行,反之亦然) 。 Also notice that GNU libc are usually tied to some kernel version (you cannot use a recent libc on some quite old kernel). 还要注意GNU libc通常与某些内核版本绑定(你不能在一些非常老的内核上使用最近的libc)。 You could consider some alternative libc like MUSL libc 你可以考虑一些像MUSL libc这样的替代libc

BTW, you usually can use some chroot -ed target environment (in which you might install some other distribution, eg. with debootstrap ) 顺便说一句,你通常可以使用一些chroot -ed目标环境(你可以在其中安装一些其他的发行版,例如debootstrap

If anyone is curious we did solved the problem by building a GCC 4.8 toolchain on top of a RHEL 4.8 dist that continued to be our build agent. 如果有人好奇,我们通过在RHEL 4.8 dist之上构建GCC 4.8工具链来解决问题,该工具链仍然是我们的构建代理。

The crux of it is outlined here . 这里概述了它的关键。 The problem was a little bit simpler since we don't need a fully functional cross compiler. 问题稍微简单一点,因为我们不需要一个功能齐全的交叉编译器。 Just a GCC 4.8 toolchain on the target host. 只是目标主机上的GCC 4.8工具链。

Interoperability was still a pain because this old version of Linux had virtually no support for SMB and/or other tech. 互操作性仍然很痛苦,因为这个旧版本的Linux几乎不支持SMB和/或其他技术。 We ended up with a Bash script that put build outputs in a FTP server and this worked reasonably well. 我们最终得到了一个Bash脚本,它将构建输出放在FTP服务器中,这种方法运行得相当好。 It solved major pains. 它解决了重大的痛苦。

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

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