Dune native compilation

Hey all, I’m trying to compile OCaml code to native executable through dune. Here is a part of my dune file:
(modes (native object))
that i thought must specify compilation type, but it’s not working.
It’s using bytecode compiler, any ideas?

There’s nothing special to do to build a native executable. With the following dune file:

(executable
 (name x))

dune build ./x.exe should work.

How are you invoking dune? What’s not working?

I want to use

ocamlopt

to compile OCaml code to native.
I’m using dune like this

dune --profile=release nameOfExe.exe

where release profile is defined like this

(release
(flags (:standard -O3 -w -39-33-20-50)))

This is missing the build part. Does the following work?

dune --profile=release build nameOfExe.exe

Sorry, yes, of course

dune build --profile=release nameOfExe.exe

Just typo

You say that it’s not working, but can you give more details? What happens when you run this command? How do you know that it is using the bytecode compiler?

When i run this command I’m getting error that ocamlc doesn’t have -O3 option(for every file of OCaml source code) and indeed it doesn’t, where’s ocamlopt does have.

Ah, I see. That’s because ocamlc is still used for some cases, like compiling mli files.

To pass options only to ocamlopt, you can use ocamlopt_flags instead of flags. That should fix your problem.

1 Like

hi there,

i’m not sure, but if you want to build an executable why don’t you use:

(modes (native exe))

instead of :