OCaml shebang 语法错误

标签 ocaml syntax-error shebang

$ 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/

相关文章:

c# - LINQ 无法识别 select 子句中的连接表

python - python中 Selenium 代码中xpath中的语法错误

python - 通过映射到 autocmd 自动完成 python Shebang(在非 .py 文件中)

perl - 在 git-bash 中运行 Perl 可执行文件

vim - 在 Vim 中使用 OCaml 的当前设置是什么?

pattern-matching - 模式匹配 OCaml 中的变量?

c - 尽管轮询文件描述符,但不可靠的 http 客户端

java - 不能在定义之前引用一个字段,但前提是你没有限定它

python - "shebang/usr/bin/env python"调用了错误的 Python 解释器

functional-programming - 如何高效学习OCaml?