Search code examples
vimocamlopam

Call OCaml from Vim: cannot load shared library dllbin_prot_stubs


I have an OCaml program that I'd like to call from Vim.

In my .vimrc, I have defined a command that should execute the OCaml bytecode:

command! MyCommand call FunctionCallingOCaml()
function! FunctionCallingOCaml()
  let scriptPath = $HOME . "/path/to/myOCamlProgram.byte"
  let ocamlCmd = "ocamlrun " . scriptPath
  execute "!" . ocamlCmd
endfunction

Unfortunately, this doesn't work; I get an error message:

Fatal error: cannot load shared library dllbin_prot_stubs Reason: dllbin_prot_stubs.so: cannot open shared object file: No such file or directory

I can execute myOCamlProgram.byte without a hitch from another terminal running bash.

Also, I can call shell built-ins like ls and programs like grep from Vim without a problem using execute.


My system:

uname -a:

Linux marathon 4.12.10-1-ARCH #1 SMP PREEMPT Wed Aug 30 12:18:42 CEST 2017 x86_64 GNU/Linux

opam --version: 1.2.2

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Sep 19 2017 13:59:47)


Solution

  • It turned out that OPAM wasn't properly initialized in the shell running Vim. The PATH didn't include the OCaml binaries which led to the error.

    As a fix, I called eval $(opam config env) to set environment variables and modify the PATH so my Linux installation will run OCaml programs from Vim:

    let ocamlCmd = "eval $(opam config env) && ocamlrun " . scriptPath
    execute "!" . ocamlCmd
    

    Better still, add this to your .bash_profile so your shell is initialzed with regards to OPAM every time you log in:

    . /home/mb/.opam/opam-init/init.sh
    

    The dot before the shell script sources it.