gpt4 book ai didi

OCaml shebang 语法错误

转载 作者:行者123 更新时间:2023-12-01 11:55:42 30 4
gpt4 key购买 nike

$ cat hello.ml 
#!/usr/bin/env ocamlrun ocaml

let rec main = print_string "Hello World!\n"

$ ./hello.ml
Hello World!

$ ocaml hello.ml
Hello World!

$ ocamlc -o hello hello.ml
File "hello.ml", line 1, characters 0-1:
Error: Syntax error

$ ocamlopt -o hello hello.ml
File "hello.ml", line 1, characters 0-1:
Error: Syntax error

与 Erlang 类似,OCaml 允许在脚本模式下使用 shebang,但在编译模式下会出错。 OCaml 中是否有更惯用的 shebang,它不会在编译期间触发语法错误?

最佳答案

据我所知,没有一个指令可以同时适用于顶层和 OCaml 编译器,但您可以使用 ocamlscript 作为中间选项。

http://martin.jambon.free.fr/ocamlscript.html

ocamlscript 在执行代码时在后台使用 ocamlfind + ocamlopt 来(重新)编译您的代码。安装 ocamlscript 后,您可以使用:

#!/usr/bin/env ocamlscript

它不会为您提供直接的顶级兼容性,但可以让您避免对许多程序进行单独的编译步骤。该网站有不错的文档和示例。

关于OCaml shebang 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7601474/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com