ocaml
Static linking is preferred for some cases although it has its own various problems. Static building/linking is not always possible for some languages on some platform. For OCaml, the answer to this question is yes. In this post, we will introduce 2 methods to statically linking OCaml: static linking with runtime glibc required and static linking with musl (as libc alternative which does not require runtime library).
尽管静态链接有其自身的各种问题,但在某些情况下还是首选静态链接。 在某些平台上,某些语言并非总是可以进行静态构建/链接。 对于OCaml ,这个问题的答案是肯定的。 在本文中,我们将介绍2种静态链接OCaml的方法:静态链接需要运行时glibc和静态链接musl(作为不需要运行时库的libc替代品)。
OCaml与glibc的静态链接(需要可用的运行时glibc) (OCaml static linking with glibc (requires runtime glibc available))
If the program is fine to have glic not "really" (see later parts for why) statically linked it, it is simple enough just to pass the -static
option to the C linker by giving the OCaml compiler -ccopt -static
option.
如果该程序可以使glic不是“真正”(请参见后面的原因)进行静态链接,那么通过给OCaml编译器-ccopt -static
选项将-static
选项传递给C链接器就足够简单了。
To test, let’s write a small hello.ml program as follows
为了测试,让我们编写一个小的hello.ml程序,如下所示
let () = print_endline "hello world"
Now let’s build the program.
现在,我们来构建程序。
$ ocamlopt -ccopt -static -o hello hello.ml
It may print a warning message like
它可能会打印一条警告消息,例如
/usr/lib/ocaml/libasmrun.a(unix.o): In function `caml_dlopen':
(.text+0x37f): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
The warning message explains itself the problem of static linking against glibc: when dlopen
(and more similar ones) is used during run time, the glibc version’s shared libraries will be needed for linking.
警告消息本身说明了针对glibc进行静态链接的问题:在运行时使用dlopen
(以及更多类似的链接)时,需要使用glibc版本的共享库进行链接。
We can verify the linked binary executable is "static".
我们可以验证链接的二进制可执行文件是“静态”的。
$ ldd ./hello
not a dynamic executable
使用musl libc进行OCaml静态链接(不需要运行时库) (OCaml static linking with musl libc (without runtime libraries required))
Linking against glibc is not 100% fully static linking as shown above. However, there are some libc alternatives available and supported by the OCaml compiler. One is the musl libc. Let’s see how to build OCaml program with musl statically.
如上所述,针对glibc的链接不是100%完全静态的链接。 但是,OCaml编译器有一些可用的libc替代方法并受其支持。 一个是音乐库。 让我们看看如何用musl静态构建OCaml程序。
On Debian/Ubuntu Linux distros, you can install musl libc and tools by
在Debian / Ubuntu Linux发行版上,您可以通过以下方式安装musl libc和工具:
$ sudoapt-get install musl-tools
You may use other ways to install it if you are using other distros.
如果使用其他发行版,则可以使用其他方式安装它。
Now, let’s switch the OCaml tool chain to the musl+static one as follows.
现在,让我们如下将OCaml工具链切换为musl + static。
$ opam switch install 4.07.1+musl+static+flambda
(here, we use version 4.07.1 as an example)
(在这里,我们以4.07.1版为例)
Install ocamlfind
if the system does not have it yet.
如果系统尚没有ocamlfind
,请安装它。
$ opam install ocamlfind
Then, let’s build it.
然后,让我们构建它。
$ ocamlfind ocamlopt -ccopt -static -o hello hello.ml
Now, let’s verify the built binary is not a dynamic one any more.
现在,让我们确认构建的二进制文件不再是动态的。
$ ldd ./hello
not a dynamic executable
翻译自: https://www.systutorials.com/how-to-statically-link-ocaml-programs/
ocaml