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

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
- Add `no-caml-startup` feature flag that avoids the `caml_startup` symbol. This will make `OCamlRuntime::init_persistent()` a noop. The main motivation for this feature flag is to make code that uses... — committed to tizoc/ocaml-interop by tizoc 3 years ago
- Add `no-caml-startup` feature flag that avoids the `caml_startup` symbol. This will make `OCamlRuntime::init_persistent()` a noop. The main motivation for this feature flag is to make code that uses... — committed to tizoc/ocaml-interop by tizoc 3 years ago
- Add `no-caml-startup` feature flag that avoids the `caml_startup` symbol. This will make `OCamlRuntime::init_persistent()` a noop. The main motivation for this feature flag is to make code that uses... — committed to tizoc/ocaml-interop by tizoc 3 years ago
- Add `no-caml-startup` feature flag that avoids the `caml_startup` symbol. This will make `OCamlRuntime::init_persistent()` a noop. The main motivation for this feature flag is to make code that uses... — committed to tizoc/ocaml-interop by tizoc 3 years ago
- Add `no-caml-startup` feature flag that avoids the `caml_startup` symbol. Will also be enabled if the `OCAML_INTEROP_NO_CAML_STARTUP` environment variable is set. This will make `OCamlRuntime::init_... — committed to tizoc/ocaml-interop by tizoc 3 years ago
- Add `no-caml-startup` feature flag that avoids the `caml_startup` symbol. Will also be enabled if the `OCAML_INTEROP_NO_CAML_STARTUP` environment variable is set. This will make `OCamlRuntime::init_... — committed to tizoc/ocaml-interop by tizoc 3 years ago
- Add `no-caml-startup` feature flag that avoids the `caml_startup` symbol. Will also be enabled if the `OCAML_INTEROP_NO_CAML_STARTUP` environment variable is set. This will make `OCamlRuntime::init_... — committed to tizoc/ocaml-interop by tizoc 3 years ago
- Add `no-caml-startup` feature flag that avoids the `caml_startup` symbol. Will also be enabled if the `OCAML_INTEROP_NO_CAML_STARTUP` environment variable is set. This will make `OCamlRuntime::init_... — committed to tizoc/ocaml-interop by tizoc 3 years ago
- Add `no-caml-startup` feature flag that avoids the `caml_startup` symbol. Will also be enabled if the `OCAML_INTEROP_NO_CAML_STARTUP` environment variable is set. This will make `OCamlRuntime::init_... — committed to tizoc/ocaml-interop by tizoc 3 years ago
- Add `no-caml-startup` feature flag that avoids the `caml_startup` symbol. Will also be enabled if the `OCAML_INTEROP_NO_CAML_STARTUP` environment variable is set. This will make `OCamlRuntime::init_... — committed to tizoc/ocaml-interop by tizoc 3 years ago
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).
There is partial one is the description of the issue which work for me.