Flutter main channel 提供了一个新功能:支持交叉编译 Dart AOT 可执行文件。
目前支持从 Windows 和 macOS 编译为 Linux 二进制文件:
-
--target-os=linux
-
--target-arch=value
:目标体系结构,可以是arm64
(64 位 ARM 处理器)或x64
(64 位处理器)
例如 :dart compile exe --target-os=linux --target-arch=x64 hello.dart -o hello
目前,执行这个命令会下载额外的 Dart SDK 二进制文件,并将它们缓存在 ~/.dart
目录 :
Downloading https://storage.googleapis.com/dart-archive/channels/dev/signed/hash/...4864.../sdk/gen_snapshot_macos_arm64_linux_x64...
Downloading https://storage.googleapis.com/dart-archive/channels/dev/raw/hash/...64e44.../sdk/dartaotruntime_linux_x64...
Specializing Platform getters for target OS linux.
Generating AOT kernel dill.
Compiling /tmp/hello.dart to /tmp/hello.exe using format Kind.exe:
Generating AOT snapshot. path/to/dir/.dart/3.8.0-265.0.dev/gen_snapshot_macos_arm64_linux_x64 []
Generating executable.
Marking binary executable.
Generated: /tmp/hello.exe
例如在 window 上通过 dart compile exe --target-os=linux hello.dart -o hello
编译下方代码,然后到 linux 下执行,可以看到代码可以正常运行:
void main() {
for (var i = 0; i < 10; i++) {
print('hello ${i + 1}');
}
}
你觉得 Dart 上的交叉编译是否会是刚需?