多次链接单个跨平台目标文件?
如果我将一段跨平台代码编译到目标文件中,是否可以使用链接器从该单个二进制文件创建单独的依赖于平台的可执行文件(.exe、.bin)?
编辑:看来回答者并不真正理解我的问题。我问您是否可以使用跨平台对象并从中生成依赖于平台的可执行文件。
If I compile a cross-platform piece of code into an object file, will it be possible to use the linker to create separate platform-dependent executables (.exe,.bin) from that single binary file?
EDIT: It seems the answerers don't really understand my question. I'm asking if you can use a cross-platform object and generate platform-dependent executables from that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很大程度上完全取决于您拥有的链接器和其他开发工具。当然,在像 gcc 这样的高级环境中,交叉编译是可能的,您可以在其中为不同的体系结构生成目标代码。
但是将所有这些不同架构对象打包到一个可执行文件中并不是我在
gcc
中见过的事情。我在 Apple 平台上见过胖二进制文件(可执行文件可以在旧的 68K Mac 或较新的 PowerPC 上运行),但我从来都不是它们的忠实粉丝,而且 Apple 完全控制着那里的环境。
此外,加载程序代码(通常是操作系统的一部分)必须能够检测应该从这样的胖二进制文件中提取和运行哪种架构(这就是苹果的控制派上用场的地方 - 他们可以修改各种操作系统来检测并加载正确的版本)。
就个人而言,我认为您最好使用可移植语言(Java、Perl、Python 等)或将应用程序打包到不同的二进制文件中 - 您始终可以使用优秀的跨平台安装工具包之一来安装正确的版本。
根据您澄清问题的编辑:是的。如果对象文件确实是跨平台的,那么它们将在所有这些平台上运行。因此,根据定义,您可以基于此构建特定于平台的可执行文件。请注意,这与编译某些跨平台源代码不同,因为编译过程本身很可能会将其锁定到特定平台。
而且,这又取决于所使用的工具链(编译器、链接器、加载器等)。
That pretty much depends entirely on the linker and other development tools that you have. Certainly cross compilation is possible in an advanced environment like
gcc
, where you can generate object code for different architectures.But packaging all those different-architecture objects into a single executable is not something I've ever seen done in
gcc
.I've seen fat binaries on the Apple platforms (where an executable would run on the old 68K Macs or the newer PowerPC ones) but I've never really been a big fan of them and Apple was in total control of the environment there.
In addition, the loader code (part of the OS usually) has to be able to detect which architecture it should extract and run from such a fat binary (this is where Apple's control came in handy - they could modify their various operating systems to detect and load the correct version).
Personally, I think you'd either be better off using a portable language (Java, Perl, Python et al) or packaging your application into different binaries - you could always use one of the excellent cross-platform installation toolkits to install the correct version.
Based on your edit clarifying the question: yes. If the object files are truly cross-platform, they will work on all those platforms. So, by definition, you can construct a platform-specific executable based on that. Note that this is not the same as compiling some cross-platform source code since the compilation process itself will most likely lock it to a specific platform.
And, again, it depends on the tool chain being used (compiler, linker, loader, etc).
胖二进制文件应该在 Linux 上也受支持,但从未尝试过。
FatELF 页面说他们支持静态和动态库,他们的常见问题解答似乎很广泛。
Fat binaries are supposed to be supported on linux as well, never tried however.
The FatELF page says they support static and dynamic libraries, their faq seems extensive.