# Compilation of 'Hello World' gives likely wrong machine code

**URL:** <https://discuss.ocaml.org/t/compilation-of-hello-world-gives-likely-wrong-machine-code/2180>\
**Category:** Learning\
**Created:** [June 20, 2018, 6:04pm UTC](https://discuss.ocaml.org/t/compilation-of-hello-world-gives-likely-wrong-machine-code/2180 "2018-06-20T18:04:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mirco](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@Mirco](https://discuss.ocaml.org/u/Mirco)\
**Post date:** [June 20, 2018, 6:04pm UTC](https://discuss.ocaml.org/t/compilation-of-hello-world-gives-likely-wrong-machine-code/2180/1 "2018-06-20T18:04:32Z")

</div>

Im on Debian GNU/Linux 9 (stretch) 64-bit with OCaml version 4.05.0 and I want to compile the following file helloworld.ml into machine code using ocamlopt. To be more precise I have a .ml filethat which contains

print\_string “Hello world!\n”;;

but is otherwise empty. I cd into, say …/folder than run

xx~$ ocamlopt …/folder/helloworld.ml

and I get a file helloworld.o … On debian I chmod this file to be executeable. I execute the file

xx~$ …/folder/helloworld.o

and I get the error massage:

bash: …/folder/hello\_world.o: cannot execute binary file: Exec format error

I think this means that the machine code is for another machine. Maybe a 64bit/32bit mismatch. I don’t know. Am I doing something wrong?

Note: Since I needed the latest Ocaml version, which is not in the debian repository, I might have installed a 32bit version. Can I check that?

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [June 20, 2018, 6:12pm UTC](https://discuss.ocaml.org/t/compilation-of-hello-world-gives-likely-wrong-machine-code/2180/2 "2018-06-20T18:12:29Z")

</div>

The `.o` file is not an executable file, it’s a C object file. You need to run the `a.out` file that was produced in the directory.

---

<div class="post-metadata">

**Author:** ![Mirco](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@Mirco](https://discuss.ocaml.org/u/Mirco)\
**Post date:** [June 20, 2018, 6:14pm UTC](https://discuss.ocaml.org/t/compilation-of-hello-world-gives-likely-wrong-machine-code/2180/3 "2018-06-20T18:14:19Z")

</div>

Oh… my mistake. Tanks

---

<div class="post-metadata">

**Author:** ![ttamttam](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ttamttam/32/132_2.png) [@ttamttam](https://discuss.ocaml.org/u/ttamttam)\
**Post date:** [June 20, 2018, 6:17pm UTC](https://discuss.ocaml.org/t/compilation-of-hello-world-gives-likely-wrong-machine-code/2180/4 "2018-06-20T18:17:48Z")

</div>

Since you did not specify the executable you want to be generated (option -o), the default a.out should be in the same directory.

Run it.

---

<div class="post-metadata">

**Author:** ![ttamttam](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ttamttam/32/132_2.png) [@ttamttam](https://discuss.ocaml.org/u/ttamttam)\
**Post date:** [June 20, 2018, 6:21pm UTC](https://discuss.ocaml.org/t/compilation-of-hello-world-gives-likely-wrong-machine-code/2180/5 "2018-06-20T18:21:03Z")

</div>

> [@Mirco](#):
>
> Note: Since I needed the latest Ocaml version, which is not in the debian repository, I might have installed a 32bit version. Can I check that?

`ocamlc -config | grep architecture` maybe?

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [June 20, 2018, 6:28pm UTC](https://discuss.ocaml.org/t/compilation-of-hello-world-gives-likely-wrong-machine-code/2180/6 "2018-06-20T18:28:56Z")

</div>

> [@Mirco](#):
>
> But xx~$ ocamlopt …/folder/helloworld.ml doesn’t produce this file.

Check the current working directory of where you invoked the command (for that reason it may be better to use the `-o` to specify the name and location of the executable to produce).

---

<div class="post-metadata">

**Author:** ![Mirco](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@Mirco](https://discuss.ocaml.org/u/Mirco)\
**Post date:** [June 20, 2018, 6:29pm UTC](https://discuss.ocaml.org/t/compilation-of-hello-world-gives-likely-wrong-machine-code/2180/7 "2018-06-20T18:29:28Z")

</div>

Problem solved. Thanks
