Erlang 编译 - Erlang 作为独立可执行文件
有没有办法将 Erlang 编译为独立的可执行文件? 这意味着,在没有 Erlang 运行时的情况下将其作为 exe 运行。
is there a way to compile Erlang to be a stand-alone executable?
this means, to run it as an exe without the Erlang runtime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然可以将所有内容封装在一个 EXE 中,但您无法摆脱 Erlang 运行时。例如,由于其本质,像 Erlang 这样的动态语言实际上无法编译为本机 x86 代码。那里一定有翻译员。
可以想出一个方案,将解释器和所有 BEAM 文件捆绑到一个 EXE 中,您可以双击并直接运行,但这可能比您想要的工作更多。我以前见过这样做的,但很少有充分的理由这样做,所以我不会在这里详细介绍这些技术。
相反,我建议您使用与 Python 的 py2exe 和 py2app 程序相同的技术来分别创建 Windows 和 Mac OS X 可执行文件。这些程序将程序的主模块加载到Python解释器中,使用该语言的内置反射机制找出它需要哪些其他模块,然后写出所有这些已编译的模块以及语言解释器的副本和一个小的包装程序使用解释器启动程序的主模块。包含这些文件的目录是一个独立的环境,包含运行程序所需的一切。 Erlang 情况下的唯一区别是 python.exe 变为 erl.exe,*.pyc 变为 *.beam。基本思想还是一样的。
如果您不需要它与任何任意 Erlang 程序一起工作,而只需要与您的程序一起工作,则可以简化它。在这种情况下,您只需将 Erlang 解释器和组成程序的所有 .beam 文件复制到一个目录中即可。例如,您可以将这部分添加到程序的 Makefile 中。
然后,您可以使用您最喜欢的 setup.exe 或 MSI 创建方法来创建可分发包,将该文件集合安装到最终用户系统上的
c:\Program Files\MyProgram
中,并创建“ erl mainmodule.beam”在开始菜单中。最终用户并不关心作为程序的一部分他们也获得了 Erlang 的副本。这是一个实现细节。While it's possible to wrap everything up in a single EXE, you're not going to get away from having an Erlang runtime. Dynamic languages like Erlang can't really be compiled to native x86 code, for instance, due to their nature. There has to be an interpreter in there somewhere.
It's possible to come up with a scheme that bundles the interpreter and all the BEAM files into a single EXE you can double-click and run directly, but that's probably more work than you were wanting to go to. I've seen it done before, but there's rarely a good reason to do it, so I won't bother going into detail on the techniques here.
Instead, I suggest you use the same technique they use for Python's py2exe and py2app programs for creating Windows and Mac OS X executables, respectively. These programs load the program's main module up into a Python interpreter, figure out which other modules it needs using the language's built-in reflection mechanisms, then write out all those compiled modules along with a copy of the language interpreter and a small wrapper program that launches the program's main module with the interpreter. The directory containing those files is then a stand-alone environment, having everything needed to run the program. The only difference in the Erlang case is that python.exe becomes erl.exe, and *.pyc becomes *.beam. The basic idea is still the same.
You can simplify this if you don't need it to work with any arbitrary Erlang program, but only yours. In that case, you just copy the Erlang interpreter and all the .beam files that make up your program into a single directory. You can make this part of your program's Makefile, for instance.
You can then use your favorite setup.exe or MSI creation method for creating a distributable package that installs this collection of files into
c:\Program Files\MyProgram
on the end user's system and creates a shortcut for "erl mainmodule.beam" in their Start menu. The end user doesn't care that as part of the program they also get a copy of Erlang. That's an implementation detail.您可以使用Warp。我添加了包装 Erlang 版本的示例。
you can use Warp. I've added examples for wrapping an Erlang release.