# Dune native compilation

**URL:** <https://discuss.ocaml.org/t/dune-native-compilation/3614>\
**Category:** Ecosystem\
**Tags:** dune\
**Created:** [April 2, 2019, 6:02pm UTC](https://discuss.ocaml.org/t/dune-native-compilation/3614 "2019-04-02T18:02:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![p3rsik](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@p3rsik](https://discuss.ocaml.org/u/p3rsik)\
**Post date:** [April 2, 2019, 6:02pm UTC](https://discuss.ocaml.org/t/dune-native-compilation/3614/1 "2019-04-02T18:02:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![emillon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/emillon/32/1206_2.png) [@emillon](https://discuss.ocaml.org/u/emillon)\
**Post date:** [April 2, 2019, 7:30pm UTC](https://discuss.ocaml.org/t/dune-native-compilation/3614/2 "2019-04-02T19:30:48Z")

</div>

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

```auto
(executable
 (name x))

```

`dune build ./x.exe` should work.

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

---

<div class="post-metadata">

**Author:** ![p3rsik](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@p3rsik](https://discuss.ocaml.org/u/p3rsik)\
**Post date:** [April 2, 2019, 7:42pm UTC](https://discuss.ocaml.org/t/dune-native-compilation/3614/3 "2019-04-02T19:42:22Z")

</div>

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)))

---

<div class="post-metadata">

**Author:** ![emillon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/emillon/32/1206_2.png) [@emillon](https://discuss.ocaml.org/u/emillon)\
**Post date:** [April 2, 2019, 7:57pm UTC](https://discuss.ocaml.org/t/dune-native-compilation/3614/4 "2019-04-02T19:57:57Z")

</div>

> [@p3rsik](#):
>
> `dune --profile=release nameOfExe.exe`

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

> `dune --profile=release build nameOfExe.exe`

---

<div class="post-metadata">

**Author:** ![p3rsik](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@p3rsik](https://discuss.ocaml.org/u/p3rsik)\
**Post date:** [April 2, 2019, 8:12pm UTC](https://discuss.ocaml.org/t/dune-native-compilation/3614/5 "2019-04-02T20:12:09Z")

</div>

Sorry, yes, of course

> dune build --profile=release nameOfExe.exe

Just typo

---

<div class="post-metadata">

**Author:** ![emillon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/emillon/32/1206_2.png) [@emillon](https://discuss.ocaml.org/u/emillon)\
**Post date:** [April 2, 2019, 8:24pm UTC](https://discuss.ocaml.org/t/dune-native-compilation/3614/6 "2019-04-02T20:24:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![p3rsik](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@p3rsik](https://discuss.ocaml.org/u/p3rsik)\
**Post date:** [April 2, 2019, 8:36pm UTC](https://discuss.ocaml.org/t/dune-native-compilation/3614/7 "2019-04-02T20:36:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![emillon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/emillon/32/1206_2.png) [@emillon](https://discuss.ocaml.org/u/emillon)\
**Post date:** [April 2, 2019, 9:27pm UTC](https://discuss.ocaml.org/t/dune-native-compilation/3614/8 "2019-04-02T21:27:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![marianaF](https://avatars.discourse-cdn.com/v4/letter/m/839c29/32.png) [@marianaF](https://discuss.ocaml.org/u/marianaF)\
**Post date:** [April 2, 2019, 9:27pm UTC](https://discuss.ocaml.org/t/dune-native-compilation/3614/9 "2019-04-02T21:27:17Z")

</div>

hi there,

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

(modes (native exe))

instead of :

> [@p3rsik](#):
>
> (modes (native object))
