# 移植

**移植**（Porting）是指对软件进行改造，使其能够在不同于最初编写时所针对的计算环境中执行——例如不同的处理器架构、操作系统或设备——同时保持其行为不变的过程。这一术语涵盖了一系列工作：对可移植的源代码做少量修改后重新编译、重写应用程序中与平台相关的层次，或者根据参考实现重新构建一个程序。

| 关键事实 | 详情 |
|---|---|
| 定义 | 改造软件使其在不同于原环境的环境（架构、操作系统或设备）中运行，同时保持行为不变 |
| 首次 Unix 移植 | 1976–77 年，澳大利亚伍伦贡大学的理查德·米勒（Richard Miller）在尤里斯·莱因费尔兹（Juris Reinfelds）指导下，将第六版 Unix 移植到 Interdata 7/32 上；该移植后来成为首个由计算机厂商支持的 UNIX<sup>[1](https://www.tuhs.org/cgi-bin/utree.pl?file=Interdata732)</sup><sup> • </sup><sup>[2](https://www.usenix.org/conference/1998-usenix-annual-technical-conference/20th-anniversary-first-port-unix)</sup> |
| 贝尔实验室移植 | 1977 年，史蒂夫·约翰逊（Steve Johnson）和丹尼斯·里奇（Dennis Ritchie）独立于伍伦贡团队将 Unix 移植到类似的 Interdata 8/32 上<sup>[3](https://www.nokia.com/bell-labs/about/dennis-m-ritchie/portpapers.html)</sup> |
| POSIX 目标 | 一个清晰、一致、无歧义的接口标准，以在源代码层面而非目标代码层面促进应用程序在 UNIX 系统环境之间的可移植性<sup>[4](https://pubs.opengroup.org/onlinepubs/9699919799/frontmatter/preface.html)</sup> |
| 二进制翻译 | 苹果的 Rosetta 可在 Apple 芯片上运行 x86_64 Mac 应用，但不能翻译内核扩展、x86_64 虚拟化应用或 AVX512 指令<sup>[5](https://developer.apple.com/documentation/Apple-Silicon/about-the-rosetta-translation-environment?changes=l_1)</sup> |
| 实测普遍程度 | 对 500 个 Python 项目进行跨操作系统测试，发现 11.2% 存在与操作系统相关的测试失败；在抽样的 240 个 GitHub 问题中，有 102 个是真正的可移植性问题<sup>[6](https://arxiv.org/abs/2609.25531)</sup> |

## 最早的 Unix 移植

1976–77 年，当 Unix 系统离开其编写时所基于的 PDP-11 小型计算机时，可移植性成为 Unix 的一项标志性属性。两个团队各自独立工作，直到移植完成后才得知对方的存在。在澳大利亚[伍伦贡大学](https://www.edgepedia.org/zh/university-of-wollongong)，理查德·米勒为 32 位 Interdata 7/32 移植了 C 编译器、操作系统内核、设备驱动程序和汇编器，罗斯·尼隆（Ross Nealon）负责命令和子程序库方面的工作，尤里斯·莱因费尔兹教授负责指导；到 1977 年 4 月 28 日，Unix 第六版已在该机器上启动成功。<sup>[1](https://www.tuhs.org/cgi-bin/utree.pl?file=Interdata732)</sup><sup> • </sup><sup>[7](https://gunkies.org/wiki/Interdata_7/32)</sup> 在[贝尔实验室](https://www.edgepedia.org/zh/bell-labs)，史蒂夫·约翰逊和[丹尼斯·里奇](https://www.edgepedia.org/zh/dennis-ritchie)将 Unix 移植到类似的 Interdata 8/32 上，1977 年初开始工作，到六月内核已可运行。<sup>[3](https://www.nokia.com/bell-labs/about/dennis-m-ritchie/portpapers.html)</sup><sup> • </sup><sup>[7](https://gunkies.org/wiki/Interdata_7/32)</sup>

两个团队选择了不同的方法。伍伦贡小组与他们在悉尼的 PDP-11 相距约 100 公里，他们从 PDP-11 版本改造了 C 编译器，在 Interdata 上厂商的操作系统下运行它，然后将 Unix 内核分块迁移——最初作为 Interdata 系统下的一个任务、配合占位设备驱动程序运行，后来才独立运行。<sup>[8](https://www.bell-labs.com/usr/dmr/www/portpap.pdf)</sup> 伍伦贡的移植后来成为首个由计算机厂商支持的 UNIX。<sup>[2](https://www.usenix.org/conference/1998-usenix-annual-technical-conference/20th-anniversary-first-port-unix)</sup>

**对 C 语言的影响。** 用史蒂夫·约翰逊的话说，这次移植工作迫使 C 语言在可移植性方面“认真起来”。它直接促成了结构体成员独立命名空间、`sizeof` 运算符、lint 等工具，以及用头文件收集多个例程共享的结构体声明等惯例。约翰逊和里奇指出，在字节对齐、字节序和位域方面，他们的移植不太成功。<sup>[2](https://www.usenix.org/conference/1998-usenix-annual-technical-conference/20th-anniversary-first-port-unix)</sup><sup> • </sup><sup>[8](https://www.bell-labs.com/usr/dmr/www/portpap.pdf)</sup> 1977 年前后的语言变更聚焦于可移植性和类型安全，包括增加无符号类型和强制类型转换。<sup>[9](https://www.bell-labs.com/usr/dmr/www/chist.pdf)</sup> 在向 VAX 完成 32V 移植之后，贝尔实验室的其他人将该系统移植到了多种机器上，包括 36 位、反码表示的 Univac 1100 系列。<sup>[3](https://www.nokia.com/bell-labs/about/dennis-m-ritchie/portpapers.html)</sup>

## 源代码可移植性标准

一旦操作系统能在多种机器上运行，下一个问题就是保持应用程序在这些机器之间的可移植性。IEEE 的 1003 委员会为此制定了 POSIX 系列标准；该名称最初是“可移植操作系统标准”（Portable Operating System Standard），后来加上后缀成为“可移植操作系统接口”（Portable Operating System Interface）。<sup>[10](https://www.linuxtopia.org/online_books/programming_books/art_of_unix_programming/ch01s05_1.html)</sup> 1985 年，IEEE 的 Unix 项目成为 P1003 工作组，使命是为 UNIX 操作系统规定标准接口；IEEE 1003.1（POSIX.1）定义了基本操作系统接口，IEEE 1003.2（POSIX.2）定义了 shell 和常用工具。<sup>[11](https://ndl.ethernet.edu.et/bitstream/123456789/34252/1/36.pdf.pdf)</sup>

POSIX.1 定义了一个标准的操作系统接口和环境，包括命令解释器和常用工具程序，以在源代码层面支持应用程序的可移植性：为一个符合标准的实现编写并翻译的程序，也可以翻译到另一个符合标准的实现上执行。目标代码或二进制代码的可移植性明确不在该标准的范围之内。<sup>[4](https://pubs.opengroup.org/onlinepubs/9699919799/frontmatter/preface.html)</sup><sup> • </sup><sup>[12](https://standards.ieee.org/ieee/9945/12489/)</sup> 该标准的抽象概念——包括进程、文件系统、虚拟内存、套接字和线程——源自 1970 年代至 1980 年代间开发的各种 Unix 变体，如 Research Unix、System V、BSD 和 SunOS。<sup>[13](https://www.usenix.org/publications/loginonline/transcending-posix-end-era)</sup>

标准化是对真实碎片化的回应。1990 年代初期，Sun、HP 和 IBM 等领先供应商出货的 UNIX 变体彼此无法协同工作，阻碍了跨平台软件的可移植性；最终统一它们的规范非正式地被称为“无名规范”（The Spec with No Name），后称 Spec 1170，并成为[单一 UNIX 规范](https://www.edgepedia.org/zh/single-unix-specification)的基础。<sup>[14](https://www.unix.org/unix_history.html)</sup>

## 移植策略

策略的选择取决于目标环境能在多大程度上重现原始环境。微软关于在 Windows 上运行 Linux 程序的指南列出了主要选项：在 [Windows Subsystem for Linux](https://www.edgepedia.org/zh/windows-subsystem-for-linux)（WSL）下按原样运行程序，此时程序直接在硬件上执行而非在虚拟机中运行；在 Linux 虚拟机或 [Docker](https://www.edgepedia.org/zh/docker-software) 容器中运行；在 MinGW 或 MinGW-w64 下用 gcc 或 clang 重新编译——这些是用于构建原生 Windows 应用程序的工具链和支持库；使用 Cygwin，它在 Windows 上提供更完整的 Linux 环境；或者手动将代码移植到 Microsoft C++，将平台无关的代码重构为独立库，并针对 Win32 或 DirectX API 重写 Linux 特定的代码——微软称对于需要高性能图形的应用，这可能是最佳选择。<sup>[15](https://learn.microsoft.com/en-us/cpp/porting/porting-from-unix-to-win32?view=msvc-170)</sup><sup> • </sup><sup>[26](https://www.mingw-w64.org/?lang=en)</sup>

**作为桥梁的二进制翻译。** 当源代码不可用或重新编译不切实际时，二进制翻译可以携带应用程序跨越架构变更。苹果的 [Rosetta](https://www.edgepedia.org/zh/rosetta-software) 翻译 x86_64 Mac 应用程序以在 Apple 芯片上运行，对仅含 Intel 指令的二进制文件自动启动；当二进制文件中存在原生 arm64 指令时，系统优先使用原生指令。Rosetta 翻译所有 x86_64 指令，包括 AVX 和 AVX2，但不包括 AVX512，且无法翻译内核扩展或虚拟化 x86_64 平台的虚拟机应用。内核扩展必须支持原生架构，因为内核始终作为原生进程运行。<sup>[5](https://developer.apple.com/documentation/Apple-Silicon/about-the-rosetta-translation-environment?changes=l_1)</sup><sup> • </sup><sup>[16](https://developer.apple.com/documentation/apple-silicon/porting-your-macos-apps-to-apple-silicon?changes=_11)</sup> 推荐的长期方案是通用二进制文件——一个同时包含 arm64 和 x86_64 代码的单一可执行文件，系统在运行时从中选择原生版本；Xcode 12 及更高版本会为标准架构自动构建此类二进制文件。<sup>[16](https://developer.apple.com/documentation/apple-silicon/porting-your-macos-apps-to-apple-silicon?changes=_11)</sup> 苹果于 2026 年宣布，macOS 27 将是最后一个支持 Rosetta 的版本，此后仅含 Intel 指令的应用将无法在 Apple 芯片 Mac 上运行，但会保留一部分 Rosetta 功能以支持较旧的、无人维护的游戏。<sup>[17](https://developer.apple.com/news/?id=w5ngl9k2)</sup>

**跨平台框架**减少了逐平台的工作量。Flutter 应用几乎无需修改代码即可为多种受支持的平台构建，平台特定的行为通过插件或平台通道实现——它们在 Dart 代码和原生代码之间传递消息。<sup>[18](https://docs.flutter.dev/platform-integration)</sup><sup> • </sup><sup>[19](https://docs.flutter.dev/platform-integration/platform-channels?sid=y2sbpB)</sup>

## 移植实际涉及的工作

移植工作集中在可预测的地方。一个案例研究通过兼容层、再以原生方式移植一款遗留的 Blupi 游戏，测量了游戏 C++ 源文件和头文件共约 31,773 行代码，其中超过 21,000 行是模拟、世界、对象和事件逻辑，研究建议重写平台服务层（图形、输入、音频、视频、文件、网络），同时保持模拟相关文件的可辨识性。<sup>[20](https://libcna.com/deep-dives/practice/porting-case-studies.html)</sup> 一次从 Java 到 Unity 的游戏迁移采取了相反的方法：不是逐行翻译，而是将原始 Java 源代码作为游戏逻辑、谜题机制和关卡数据的参考，在 Unity C# 中逐系统原生重建——开发者发现，对于将原生 Java Ouya 应用带到现代移动环境而言，这种做法更为实用。<sup>[21](https://oceanviewgames.co.uk/case-studies/nub-java-to-unity-platform-migration)</sup>

即使是编写良好的代码也带有隐藏的平台假设。一项 2026 年关于 Python 跨操作系统可移植性的实证研究分析了 2,042 个开源仓库：对 500 个项目进行跨平台测试重执行，发现其中 56 个（11.2%）存在行为差异，440,728 个测试用例中有 9,508 个（2.16%）表现出与操作系统相关的结果，人工分析确认了抽样的 240 个 GitHub 问题中有 102 个是真正的可移植性问题。该研究的分类法识别出 7 个主要故障类别，其中文件和目录操作、进程管理以及库依赖最为普遍。<sup>[6](https://arxiv.org/abs/2609.25531)</sup>

**作为一种文化实践的移植。** 1993 年发布的《毁灭战士》（Doom）已被移植到异常广泛的目标平台上，近期的项目既展示了这门手艺，也展示了它的局限。为了在环绕轨道运行的[欧洲空间局](https://www.edgepedia.org/zh/european-space-agency)卫星上运行《毁灭战士》，一个黑客团队使用了 Chocolate Doom——一个忠于源代码、使用 SDL 图形和声音后端的移植版本——随后转向 doomgeneric，这是《毁灭战士》的一个变体，旨在使移植到其他系统更加容易，将图形输出分配给虚拟显卡；卫星相机拍摄的图像由星载 AI 模型进行色彩缩减，以适应游戏固定的 256 色调色板。<sup>[22](https://www.tomshardware.com/video-games/doom-can-run-just-about-anywhere-including-space-hacker-recounts-tale-of-running-the-game-on-an-orbiting-satellite)</sup> 在另一个项目中，一位 YouTuber 在 Krups Cook4Me 智能压力锅上运行了《毁灭战士》——他先转储并重新刷写了其触摸屏控制器的固件，围绕《毁灭战士》编写了足够的包装代码以将其移植到固件环境，而没有修改负责加热或安全的电子部件。<sup>[23](https://www.tomshardware.com/video-games/retro-gaming/youtuber-makes-doom-run-on-a-smart-cooking-pot-after-a-full-firmware-refresh)</sup>

## 局限与失败模式

移植并不总能忠实地转移行为。在安全研究中，前向移植（forward-porting）将旧版软件中先前已检测并修补的漏洞重新引入较新版本，以构建模糊测试基准；2024 年的一项评估揭示了损害其成功的七个因素，包括软件架构变更、配置错误、供应链问题和开发者错误。<sup>[24](https://www.sciencedirect.com/science/article/pii/S0020025524000550)</sup> 另一方面，单一 UNIX 规范的开发者称，当应用程序使用了良好的现代实践（如 ISO C [函数原型](https://www.edgepedia.org/zh/function-prototype)）时，将其移植到 64 位 UNIX 系统只需极少的精力，而在可移植性成为主要关注点之前编写的大量遗留代码则缺乏这些实践。<sup>[25](https://usenix.org/legacy/publications/login/standards/10.data.html)</sup>

## References

1. [Interdata732, The Unix Heritage Society](https://www.tuhs.org/cgi-bin/utree.pl?file=Interdata732)
2. [20th Anniversary of the First Port of UNIX, USENIX ATC 1998](https://www.usenix.org/conference/1998-usenix-annual-technical-conference/20th-anniversary-first-port-unix)
3. [Papers about Unix Portability, Dennis M. Ritchie, Bell Labs](https://www.nokia.com/bell-labs/about/dennis-m-ritchie/portpapers.html)
4. [The Base Specifications Issue 7, Preface, The Open Group](https://pubs.opengroup.org/onlinepubs/9699919799/frontmatter/preface.html)
5. [About the Rosetta translation environment, Apple Developer](https://developer.apple.com/documentation/Apple-Silicon/about-the-rosetta-translation-environment?changes=l_1)
6. [Silva, Farahat, d'Amorim. An Empirical Analysis of Cross-OS Portability Issues in Python Projects, MSR 2026](https://arxiv.org/abs/2609.25531)
7. [Interdata 7/32, Computer History Wiki](https://gunkies.org/wiki/Interdata_7/32)
8. [Johnson, S. and Ritchie, D. M. Portability of C Programs and the UNIX System, Bell System Technical Journal (1978)](https://www.bell-labs.com/usr/dmr/www/portpap.pdf)
9. [Ritchie, D. M. The Development of the C Language](https://www.bell-labs.com/usr/dmr/www/chist.pdf)
10. [What Unix Gets Right: Cross-Platform Portability and Open Standards, The Art of Unix Programming](https://www.linuxtopia.org/online_books/programming_books/art_of_unix_programming/ch01s05_1.html)
11. [Open Systems and Standards for Software Product Development, Artech House](https://ndl.ethernet.edu.et/bitstream/123456789/34252/1/36.pdf.pdf)
12. [IEEE/ISO/IEC 9945 (POSIX), IEEE SA](https://standards.ieee.org/ieee/9945/12489/)
13. [Transcending POSIX: The End of an Era?, USENIX ;login:](https://www.usenix.org/publications/loginonline/transcending-posix-end-era)
14. [History of UNIX, The Open Group](https://www.unix.org/unix_history.html)
15. [Running Linux programs on Windows, Microsoft Learn](https://learn.microsoft.com/en-us/cpp/porting/porting-from-unix-to-win32?view=msvc-170)
16. [Porting your macOS apps to Apple silicon, Apple Developer](https://developer.apple.com/documentation/apple-silicon/porting-your-macos-apps-to-apple-silicon?changes=_11)
17. [Upcoming changes to Rosetta support for Intel-based macOS apps, Apple Developer News](https://developer.apple.com/news/?id=w5ngl9k2)
18. [Build for and integrate with multiple platforms, Flutter docs](https://docs.flutter.dev/platform-integration)
19. [Platform-specific code, Flutter docs](https://docs.flutter.dev/platform-integration/platform-channels?sid=y2sbpB)
20. [Porting case studies: the Blupi games, CNA Deep Dives](https://libcna.com/deep-dives/practice/porting-case-studies.html)
21. [Java to Unity Migration: Nub Remake Case Study, Oceanview Games](https://oceanviewgames.co.uk/case-studies/nub-java-to-unity-platform-migration)
22. [Programmer installed and ran Doom on an orbiting ESA satellite, Tom's Hardware](https://www.tomshardware.com/video-games/doom-can-run-just-about-anywhere-including-space-hacker-recounts-tale-of-running-the-game-on-an-orbiting-satellite)
23. [Doom conquers the kitchen through an electric cooking pot, Tom's Hardware](https://www.tomshardware.com/video-games/retro-gaming/youtuber-makes-doom-run-on-a-smart-cooking-pot-after-a-full-firmware-refresh)
24. [Forward-porting and its limitations in fuzzer evaluation, Information Sciences 662 (2024)](https://www.sciencedirect.com/science/article/pii/S0020025524000550)
25. [Single UNIX Specification, Version 2, USENIX ;login:](https://usenix.org/legacy/publications/login/standards/10.data.html)
26. [Introduction - mingw-w64](https://www.mingw-w64.org/?lang=en)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Software engineering and development process › Software testing and quality

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/porting

English original: https://www.edgepedia.org/porting
