ocaml-rs: Undefined reference to "caml_startup" when trying to run `dune utop`

In an ocaml-rs project, for example ocaml-rust-starter, running dune utop gives the following error (while linking):

/usr/bin/ld: src/libocaml_rust_starter.a(ocaml_interop-4a398e80f3ca6ceb.ocaml_interop.8eq22uyz-cgu.12.rcgu.o): in function `_ZN3std4sync4once4Once9call_once28_$u7b$$u7b$closure$u7d$$u7d$17h7171c1b9a6fc7898E.llvm.3140953836603515050':
ocaml_interop.8eq22uyz-cgu.12:(.text._ZN3std4sync4once4Once9call_once28_$u7b$$u7b$closure$u7d$$u7d$17h7171c1b9a6fc7898E.llvm.3140953836603515050+0x3b): undefined reference to `caml_startup'
/usr/bin/ld: src/libocaml_rust_starter.a(ocaml_interop-4a398e80f3ca6ceb.ocaml_interop.8eq22uyz-cgu.12.rcgu.o): in function `_ZN4core3ops8function6FnOnce40call_once$u7b$$u7b$vtable.shim$u7d$$u7d$17h59471d095cabe6c5E.llvm.3140953836603515050':
ocaml_interop.8eq22uyz-cgu.12:(.text._ZN4core3ops8function6FnOnce40call_once$u7b$$u7b$vtable.shim$u7d$$u7d$17h59471d095cabe6c5E.llvm.3140953836603515050+0x3b): undefined reference to `caml_startup'
collect2: error: ld returned 1 exit status

image

Adding this function somewhere where the linker sees it, for example using the following code, “fixes” the issue and utop starts fine:

#[ocaml::func]
pub fn caml_startup() {}

OCaml version: 4.11.1 Rust version: nightly GCC version: 10.2.0

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 37 (29 by maintainers)

Commits related to this issue

Most upvoted comments

Yes, sure, that works too and is actually the easier one to implement (I was experimenting with this pure-dune + stub solution, but turns out it is not that easy to make the dependency on the C stub optional).

re: runtime crate split, I will open a new issue later so that the idea doesn’t get lost. It is a bigger change and requires more thought and work. From the point of view of things being different when the program is Rust-driven from when the program is OCaml-driven, the split makes sense, because the dependencies and requirements are not the same. It is just that I don’t think it is as simple as just splitting off caml_startup.

I will release ocaml-interop 0.8.5 with this new feature flag tomorrow (or maybe even later today) and open a PR with the relevant changes here and on the ocaml-rust-starter repo (cargo and dune files + changes to README mentioning that).

Anybody has found a work around for this?

There is partial one is the description of the issue which work for me.