← 返回命令列表

Linux command

ocaml 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Start interactive toplevel

ocaml

Run OCaml script

ocaml [script.ml]

Run with library

ocaml -I +[str] str.cma [script.ml]

Run expression

ocaml -e "print_endline \"Hello\""

Start with custom init

ocaml -init [.ocamlinit]

Include library path

ocaml -I [/path/to/lib]

Run with profiling

ocaml -p [script.ml]

说明

ocaml is the interactive toplevel (REPL) for OCaml, a statically-typed functional programming language with type inference. The toplevel reads expressions, compiles them, executes, and prints results with types. Expressions end with `;;`. The type inference system catches errors at compile time, even in interactive mode. Loading files with #use executes their definitions in the current session. Libraries (.cma bytecode) load with #load. The -I flag adds directories to search for modules. The .ocamlinit file in home or current directory runs on startup. It can load commonly used libraries, define utilities, and configure the environment. Integration with utop provides an enhanced toplevel with syntax highlighting, completion, and history. Many users prefer utop for interactive development. For larger projects, the Dune build system handles compilation, but ocaml remains useful for exploration, prototyping, and learning.

参数

-I _DIR_
Add directory to library search path.
-init _FILE_
Load initialization file.
-noinit
Don't load .ocamlinit.
-e _EXPR_
Evaluate expression and exit.
-stdin
Read script from stdin.
-noprompt
Suppress prompt in interactive mode.
-w _FLAGS_
Warning settings.
-warn-error _FLAGS_
Treat warnings as errors.
-version
Print version.
-vnum
Print version number only.
-help
Show help.

FAQ

What is the ocaml command used for?

ocaml is the interactive toplevel (REPL) for OCaml, a statically-typed functional programming language with type inference. The toplevel reads expressions, compiles them, executes, and prints results with types. Expressions end with `;;`. The type inference system catches errors at compile time, even in interactive mode. Loading files with #use executes their definitions in the current session. Libraries (.cma bytecode) load with #load. The -I flag adds directories to search for modules. The .ocamlinit file in home or current directory runs on startup. It can load commonly used libraries, define utilities, and configure the environment. Integration with utop provides an enhanced toplevel with syntax highlighting, completion, and history. Many users prefer utop for interactive development. For larger projects, the Dune build system handles compilation, but ocaml remains useful for exploration, prototyping, and learning.

How do I run a basic ocaml example?

Run `ocaml` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -I _DIR_ do in ocaml?

Add directory to library search path.