# Issue with Dynlink and bytecode

**URL:** <https://discuss.ocaml.org/t/issue-with-dynlink-and-bytecode/14293>\
**Category:** Learning\
**Created:** [March 13, 2024, 1:01pm UTC](https://discuss.ocaml.org/t/issue-with-dynlink-and-bytecode/14293 "2024-03-13T13:01:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![silene](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/silene/32/2707_2.png) [@silene](https://discuss.ocaml.org/u/silene)\
**Post date:** [March 13, 2024, 1:01pm UTC](https://discuss.ocaml.org/t/issue-with-dynlink-and-bytecode/14293/1 "2024-03-13T13:01:31Z")

</div>

While trying to make Why3 plugins also work in bytecode mode, I am encountering some difficulties:

```console
$ cat foo.ml
let () = Dynlink.loadfile_private ".../why3/plugins/microc.cma"

$ ocamlfind ocamlc -package why3 -linkpkg -linkall foo.ml && ./a.out 
Fatal error: exception Dynlink error: error while linking .../why3/plugins/microc.cma.
Reference to undefined global `Mc_parser'

```

The thing is, `Mc_parser` is the very first unit in `microc.cma`, so I am confused as to why Dynlink complains about it being undefined.

```console
$ ocamlobjinfo .../why3/plugins/microc.cma | head
File .../why3/plugins/microc.cma
Force custom: no
Extra C object files:
Extra C options:
Extra dynamically-loaded libraries:
Unit name: Mc_parser
Interfaces imported:
	30d209d6ccecb36afd436e1096ff74c1	Why3
	49c6c492a189deeaed5bf77a6793e7fa	Unix
	6ae0781ff0acd08a308bc4a9842d91d9	Str

```

Using `ocamlopt` instead of `ocamlc` makes the code succeed. Using `Dynlink.loadfile` instead of `Dynlink.loadfile_private` also makes the code succeed. (This can be reproduced with a plain installation of Why3 obtained with `opam install why3`.)

Is `Dynlink.loadfile_private` broken in bytecode mode or is there something else going on?

---

<div class="post-metadata">

**Author:** ![vlaviron](https://avatars.discourse-cdn.com/v4/letter/v/ec9cab/32.png) [@vlaviron](https://discuss.ocaml.org/u/vlaviron)\
**Post date:** [March 13, 2024, 1:33pm UTC](https://discuss.ocaml.org/t/issue-with-dynlink-and-bytecode/14293/2 "2024-03-13T13:33:46Z")

</div>

I have not investigated yet, but I think there is a possibility that in native mode, the whole library is loaded at once, while in bytecode, all units are loaded one by one. And since they’re loaded in private mode, `Mc_parser` is loaded, then hidden, then the rest of the units cannot find it.  
You might have a bit more luck if you build packs instead of libraries for your plugins.
