Skip to content

bytecode + c stubs not working as expected #108

Open

Description

@rgrinberg

See this repo:

https://github.com/rgrinberg/jbuilder-c-stubs

[rgrinberg:~/tmp/c-ocaml] 1 % jbuilder build hello_world.exe
[rgrinberg:~/tmp/c-ocaml] % _build/default/hello_world.exe
Hello
[rgrinberg:~/tmp/c-ocaml] % _build/default/hello_world.bc
Fatal error: cannot load shared library dllc_lib_stubs
Reason: dllc_lib_stubs.so: cannot open shared object file: No such file or directory

Am I doing something wrong here?

Activity

  1. rgrinberg commented on May 30, 2017

    @rgrinberg
    MemberAuthor

    Hm, but CAML_LD_LIBRARY_PATH seems correct to me:

    [rgrinberg:~/tmp/c-ocaml] % echo $CAML_LD_LIBRARY_PATH
    /home/rgrinberg/.opam/4.04.1/lib/stublibs:/home/rgrinberg/.opam/4.04.1/lib/ocaml/stublibs:/home/rgrinberg/.opam/4.04.1/lib/ocaml
    

    opam switch is supposed to set it right?

    But regardless, my executable isn't public, can I still somehow run it? I can make it public, but it seems annoying to have to make executables just to run them.

  2. rgrinberg commented on May 31, 2017

    @rgrinberg
    MemberAuthor

    Hmm, I'm still unable to run the bytecode binary this way. Even after setting CAML_LD_LIBRARY_PATH to what jbuilder exec would. See:

    [rgrinberg:~/tmp/c-ocaml] master(+2/-1)* 2 ± jbuilder exec -- env sh -c 'echo $CAML_LD_LIBRARY_PATH'
    /home/rgrinberg/tmp/c-ocaml/_build/install/default/lib/stublibs:/home/rgrinberg/.opam/4.04.1/lib/stublibs:/home/rgrinberg/.opam/4.04.1/lib/ocaml/stublibs:/home/rgrinberg/.opam/4.04.1/lib/ocaml
    [rgrinberg:~/tmp/c-ocaml] master(+2/-1)* ± jbuilder exec -- env _build/default/hello_world.bc
    Fatal error: cannot load shared library dllc_lib_stubs
    Reason: dllc_lib_stubs.so: cannot open shared object file: No such file or directory
    

    Seems like CAML_LD_LIBRARY_PATH is set correctly here but the bytecode isn't running.

  3. rgrinberg commented on May 31, 2017

    @rgrinberg
    MemberAuthor

    It didn't exist, thanks for the hint. My library was actually public, but i still ran into that error. Looks like I still needed to build the native target to run the bytecode executable. Note sure if that's expected.

    [rgrinberg:~/tmp/c-ocaml] master(+3/-1)* 2 ± rm -rf _build
    [rgrinberg:~/tmp/c-ocaml] master(+2/-1)* ± jbuilder build hello_world.bc
        ocamldep hello_world.depends.ocamldep-output
        ocamldep c_lib.depends.ocamldep-output
          ocamlc c_lib.{cmi,cmo,cmt}
          ocamlc c_lib__Hello_world.{cmi,cmo,cmt}
          ocamlc c_lib.cma
          ocamlc hello_world.{cmi,cmo,cmt}
          ocamlc hw.o
      ocamlmklib dllc_lib_stubs.so,libc_lib_stubs.a
          ocamlc hello_world.bc
    [rgrinberg:~/tmp/c-ocaml] master(+2/-1)* ± jbuilder exec -- env _build/default/hello_world.bc
    Fatal error: cannot load shared library dllc_lib_stubs
    Reason: dllc_lib_stubs.so: cannot open shared object file: No such file or directory
    [rgrinberg:~/tmp/c-ocaml] master(+2/-1)* 2 ± jbuilder build
        ocamlopt c_lib.{cmx,o}
        ocamlopt c_lib__Hello_world.{cmx,o}
        ocamlopt c_lib.{a,cmxa}
        ocamlopt c_lib.cmxs
    [rgrinberg:~/tmp/c-ocaml] master(+2/-1)* ± jbuilder exec -- env _build/default/hello_world.bc
    Hello
    
  4. rgrinberg commented on May 31, 2017

    @rgrinberg
    MemberAuthor

    OK, then I think leaving a note about the current situation is enough. This isn't an issue in practice anyway.

  5. ghost added a commit that references this issue on Jun 2, 2017
  6. didier-wenzek commented on Sep 12, 2017

    @didier-wenzek

    I experienced a related issue.

    I fail to load from utop a library defined with a c-stubs. Utop say "Cannot load required shared library. Cannot open shared object file: No such file or directory. ".

    Indeed, the shared library is installed by jbuilder/opam in a directory named stubslibs instead of stublibs.

    $ jbuilder install
    
    # opam-version    1.2.2
    # os              linux
    ...
    _build/install/default/lib/stublibs/dllkyoto_stubs.so => /home/didier/.opam2/4.04.2+flambda/lib/stubslibs/dllkyoto_stubs.so
    ...
    
  7. trefis commented on Sep 13, 2017

    @trefis
    Collaborator

    I ran into the same issue a few days ago.
    I believe this might be related to ocaml/opam@718d619 .

    Which version of opam/opam-installer are you using?

    (ping @AltGr)

  8. dra27 commented on Sep 13, 2017

    @dra27
    Member

    As @trefis notes, it's entirely to do with a very long-standing bug in opam-installer. You can fix it locally by updating to opam2 beta 4, but I think this makes a very strong case for having jbuilder install process the .install itself.

    Note that installs via opam should be unaffected - there is (intentional) code duplication between opam and opam-installer, so as long as your opam file doesn't incorrectly try to run jbuilder install then opam packages are fine.

  9. 17 remaining items

  10. vsiles commented on Mar 27, 2019

    @vsiles
    Contributor

    Hi ! I just had the same issue (I baked a toy example in https://github.com/vsiles/dune-debug-test)
    The two workarounds I got were:

    • update LD_LIBRARY_PATH on invocation, but that's not satisfying
    • use -custom when building with ocamlc (ocamlc_flags (:standard -custom)) which creates a viable .bc file
  11. thautwarm commented on May 18, 2020

    @thautwarm

    @vsiles Thank you! I searched everywhere for a thousand years and you saved my life!

  12. vsiles commented on May 19, 2020

    @vsiles
    Contributor

    Thanks for this info. Do you know in which version -custom has been deprecated (we are lagging quite a bit, so I'll have to update at some point :D)

  13. frejsoya commented on Sep 13, 2023

    @frejsoya
    Contributor

    I hit this issue using ctypes + mdx. The mdx stanza creates `mdx-gen.ml, but mdx-gen.bc fails to run. It dlopens the stub fine, but the dynamic linked library itself is not found (missing symbol).

    Mac OSX

  14. ELLIOTTCABLE commented on Dec 5, 2023

    @ELLIOTTCABLE
    Contributor

    We're still running into this (see @Khady's post above; we have a pretty pervasive hack involving CAML_LD_LIBRARY_PATH-setting in a bunch of our internal binaries ...)

    Unfortunately, that doesn't seem to be helpful in the case mentioned by @frejsoya — I can't feed CAML_LD_LIBRARY_PATH to the mdx_gen.bc invocation:

    $ export CAML_LD_LIBRARY_PATH="$scriptdir/../../_build/default/backend/ahrefskit:$CAML_LD_LIBRARY_PATH"
    
    $ dune runtest --verbose
    ... SNIP ...
    
    Running[1]: (cd _build/default/backend/COMPONENT/client && ./mdx_gen.bc using_ppx.mld) > _build/default/backend/COMPONENT/client/.mdx/using_ppx.mld.corrected
    File "backend/COMPONENT/client/dune", line 36, characters 0-67:
    36 | (mdx
    37 |  (files using_ppx.mld)
    38 |  (libraries COMPONENT.ppx_COMPONENT))
    
    Command [1] got signal ABRT:
     $ (cd _build/default/backend/COMPONENT/client && ./mdx_gen.bc using_ppx.mld) > _build/default/backend/COMPONENT/client/.mdx/using_ppx.mld.corrected
    Fatal error: cannot load shared library dllahrefskit_stubs_stubs
    Reason: dllahrefskit_stubs_stubs.so: cannot open shared object file: No such file or directory

    Unfortunately, the thing-under-test is a ppx; which doesn't accept (modes byte_complete); and (ocamlc_flags (-custom)) is no more helpful:

    $ dune runtest
    ... SNIP ...
    File "backend/COMPONENT/client/dune", line 36, characters 0-67:
    36 | (mdx
    37 |  (files using_ppx.mld)
    38 |  (libraries COMPONENT.ppx_COMPONENT))
    /usr/bin/ld: cannot find -lmysql_stubs
    /usr/bin/ld: cannot find -lmurmur3_stubs
    /usr/bin/ld: cannot find -lmariadb_stubs
    /usr/bin/ld: cannot find -lctypes_stubs
    ... SNIP ...
    /usr/bin/ld: cannot find -lcurl_stubs
    collect2: error: ld returned 1 exit status
    File "_none_", line 1:
    Error: Error while building custom runtime system

    Is there any workaround to this that will function in a ppx-rewriter binary, when using (mdx …)? /=

  15. frejsoya commented on Dec 5, 2023

    @frejsoya
    Contributor

    @ELLIOTTCABLE i made this pr #8784 some time ago. Which might be related. I should have followed up on it :)

    For non-installed shared libraries, at build time and + avoiding global env flags, it is possible to use relative paths to the binary with rpath (Runtime-paths). Reference for how CMAKE does this https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling

  16. added
    c-bindings
    on Jan 28, 2025
  17. joelreymont commented on Jun 13, 2025

    @joelreymont

    Is this still an unresolved issue?

  18. Alizter commented on Jun 13, 2025

    @Alizter
    Collaborator

    It seems at the moment we give an error message like this:

    Reproduction case for https://github.com/ocaml/dune/issues/108
    
      $ cat > dune-project <<EOF
      > (lang dune 3.19)
      > (package (name foo))
      > EOF
    
      $ cat > dune <<EOF
      > (executable
      >  (public_name hello_world)
      >  (modes byte)
      >  (foreign_stubs
      >   (language c)
      >   (names hw)))
      > EOF
    
      $ cat > hello_world.ml <<EOF
      > external print_hello : unit -> unit = "caml_print_hello"
      > 
      > let () = print_hello ()
      > EOF
    
      $ cat > hw.c <<EOF
      > #include <stdio.h>
      > #include <caml/mlvalues.h>
      > 
      > CAMLprim value caml_print_hello(value unit)
      > {
      >   printf("Hello\n");
      >   return Val_unit;
      > }
      > EOF
    
      $ dune exec -- ./hello_world.bc
      File "dune", lines 1-6, characters 0-98:
      1 | (executable
      2 |  (public_name hello_world)
      3 |  (modes byte)
      4 |  (foreign_stubs
      5 |   (language c)
      6 |   (names hw)))
      Error: Pure bytecode executables cannot contain foreign stubs.
      Hint: If you only need to build a native executable use "(modes exe)".
      [1]
    

    I suppose we could close this issue as its not something we explicitly support. If we were to support such a feature (how, I have no idea) it would be a new feature request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    c-bindingssuspended

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions