# Fail to dynamically link cmxs file when using Dynlink

**URL:** <https://discuss.ocaml.org/t/fail-to-dynamically-link-cmxs-file-when-using-dynlink/655>\
**Category:** Learning\
**Created:** [August 1, 2017, 4:08pm UTC](https://discuss.ocaml.org/t/fail-to-dynamically-link-cmxs-file-when-using-dynlink/655 "2017-08-01T16:08:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yuren-Zhong](https://avatars.discourse-cdn.com/v4/letter/y/7cd45c/32.png) [@Yuren-Zhong](https://discuss.ocaml.org/u/Yuren-Zhong)\
**Post date:** [August 1, 2017, 4:08pm UTC](https://discuss.ocaml.org/t/fail-to-dynamically-link-cmxs-file-when-using-dynlink/655/1 "2017-08-01T16:08:05Z")

</div>

I try to dynamically load a shared object file (a cmxs file) to my main application. This shared object file uses the library `Graphic`.  
Thus, I try to dynamically load `graphics.cmxs` before loading my shared object file. But an exception is raised with on error message.  
Does anybody knows why or how to fix this problem?  
Thanks!

---

<div class="post-metadata">

**Author:** ![dra27](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dra27/32/5294_2.png) [@dra27](https://discuss.ocaml.org/u/dra27)\
**Post date:** [August 1, 2017, 4:53pm UTC](https://discuss.ocaml.org/t/fail-to-dynamically-link-cmxs-file-when-using-dynlink/655/2 "2017-08-01T16:53:03Z")

</div>

What’s the error message - have you also called `Dynlink.allow_unsafe_modules true`?

---

<div class="post-metadata">

**Author:** ![Yuren-Zhong](https://avatars.discourse-cdn.com/v4/letter/y/7cd45c/32.png) [@Yuren-Zhong](https://discuss.ocaml.org/u/Yuren-Zhong)\
**Post date:** [August 1, 2017, 6:35pm UTC](https://discuss.ocaml.org/t/fail-to-dynamically-link-cmxs-file-when-using-dynlink/655/4 "2017-08-01T18:35:26Z")

</div>

there is no message. Only: Dynlink.Error(\_)  
I called Dynlink.allow\_unsafe\_modules true, though it seems unnecessary in native code

---

<div class="post-metadata">

**Author:** ![dra27](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dra27/32/5294_2.png) [@dra27](https://discuss.ocaml.org/u/dra27)\
**Post date:** [August 1, 2017, 6:38pm UTC](https://discuss.ocaml.org/t/fail-to-dynamically-link-cmxs-file-when-using-dynlink/655/5 "2017-08-01T18:38:22Z")

</div>

Oh, indeed - I started in bytecode when checking it and forgot that there’s no requirement for `allow_unsafe_modules` in native.

You can get the error by catching `Dynlink.Error` and using `Dynlink.error_message` to print the error message.

---

<div class="post-metadata">

**Author:** ![Yuren-Zhong](https://avatars.discourse-cdn.com/v4/letter/y/7cd45c/32.png) [@Yuren-Zhong](https://discuss.ocaml.org/u/Yuren-Zhong)\
**Post date:** [August 2, 2017, 11:28am UTC](https://discuss.ocaml.org/t/fail-to-dynamically-link-cmxs-file-when-using-dynlink/655/6 "2017-08-02T11:28:53Z")

</div>

I don’t know how to convert the exception caught from type exn to type Dynlink.error.  
I looked into the source code of Dynlink, the exceptions raised by the Dynlink.loadfile may not be of Dynlink.Error

---

<div class="post-metadata">

**Author:** ![dra27](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dra27/32/5294_2.png) [@dra27](https://discuss.ocaml.org/u/dra27)\
**Post date:** [August 2, 2017, 12:38pm UTC](https://discuss.ocaml.org/t/fail-to-dynamically-link-cmxs-file-when-using-dynlink/655/7 "2017-08-02T12:38:31Z")

</div>

In general, digging into the OCaml sources is a sledgehammer approach to debugging! Indeed, certain file I/O problems may cause `Dynlink.loadfile` to raise exceptions other than `Dynlink.Error`, but that doesn’t matter here - you already know that you’re getting a `Dynlink.Error` exception.

You want to wrap your call to `Dynlink.loadfile` with something like:

```ocaml
try
  Dynlink.loadfile "graphics.cmxs"
with Dynlink.Error e -> Printf.eprintf "Dynlink error: %s\n%!" (Dynlink.error_message e)

```

---

<div class="post-metadata">

**Author:** ![Yuren-Zhong](https://avatars.discourse-cdn.com/v4/letter/y/7cd45c/32.png) [@Yuren-Zhong](https://discuss.ocaml.org/u/Yuren-Zhong)\
**Post date:** [August 3, 2017, 1:46pm UTC](https://discuss.ocaml.org/t/fail-to-dynamically-link-cmxs-file-when-using-dynlink/655/9 "2017-08-03T13:46:55Z")

</div>

the error message is :  
error loading shared library: /home/harvey/.opam/4.04.0/lib/ocaml/graphics.cmxs: undefined symbol: camlHashtbl

---

<div class="post-metadata">

**Author:** ![dra27](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dra27/32/5294_2.png) [@dra27](https://discuss.ocaml.org/u/dra27)\
**Post date:** [August 3, 2017, 1:54pm UTC](https://discuss.ocaml.org/t/fail-to-dynamically-link-cmxs-file-when-using-dynlink/655/10 "2017-08-03T13:54:10Z")

</div>

You need to link the program with `-linkall`

---

<div class="post-metadata">

**Author:** ![Yuren-Zhong](https://avatars.discourse-cdn.com/v4/letter/y/7cd45c/32.png) [@Yuren-Zhong](https://discuss.ocaml.org/u/Yuren-Zhong)\
**Post date:** [August 4, 2017, 1:41pm UTC](https://discuss.ocaml.org/t/fail-to-dynamically-link-cmxs-file-when-using-dynlink/655/12 "2017-08-04T13:41:10Z")

</div>

It works.

But a new problem emerges. Since I wish to use the `websocket` and the `websocket.cohttp` libraries, the -linkall FLAG creates an error:  
`Files /home/harvey/.opam/4.04.0/lib/websocket/websocket_cohttp_lwt.cmxa and /home/harvey/.opam/4.04.0/lib/websocket/websocket.cmxa both define a module named Rng`

---

<div class="post-metadata">

**Author:** ![dra27](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dra27/32/5294_2.png) [@dra27](https://discuss.ocaml.org/u/dra27)\
**Post date:** [August 4, 2017, 1:55pm UTC](https://discuss.ocaml.org/t/fail-to-dynamically-link-cmxs-file-when-using-dynlink/655/13 "2017-08-04T13:55:04Z")

</div>

Ouch! In which case, you’ll need to make sure that modules `Hashtbl`, `Callback` and `Printexc` are all pulled in so that you can build without `-linkall`. It’s ugly, but you can do this with things like:

```ocaml
let _ = Hashtbl.find in let _ = Callback.register in let _ = Printexc.to_string in
Dynlink.loadfile ...

```

---

<div class="post-metadata">

**Author:** ![Yuren-Zhong](https://avatars.discourse-cdn.com/v4/letter/y/7cd45c/32.png) [@Yuren-Zhong](https://discuss.ocaml.org/u/Yuren-Zhong)\
**Post date:** [August 4, 2017, 2:18pm UTC](https://discuss.ocaml.org/t/fail-to-dynamically-link-cmxs-file-when-using-dynlink/655/15 "2017-08-04T14:18:51Z")

</div>

I am not sure if I get it correctly:

Generally, if I want to dynamically load a plugin, I have to make sure all modules the plugin uses are pulled in unless the module is in a standard library.

---

<div class="post-metadata">

**Author:** ![dra27](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dra27/32/5294_2.png) [@dra27](https://discuss.ocaml.org/u/dra27)\
**Post date:** [August 4, 2017, 2:42pm UTC](https://discuss.ocaml.org/t/fail-to-dynamically-link-cmxs-file-when-using-dynlink/655/16 "2017-08-04T14:42:25Z")

</div>

That’s right - all the modules required by that plugin must be linked into your program (this includes standard library ones - you don’t link the entire stdlib by default). You do this either with `-linkall` or by having dummy references to modules you don’t actually use.
