I’m not sure what you’re trying to achieve with -shared
… but I’d just like to point out I got success building mixed ocaml/C/C++ on Alpine (from a docker container), where musl is the default libc. There’s nothing special to do other than passing -ccopt -static
to the ocaml compiler. In particular:
- no need to select a special opam switch
- finding the C++ standard library worked out of the box (unlike on ubuntu, which is where I gave up when I tried)
See for example how it’s done for dune-deps, whose CI build runs on a docker image created with ocaml-layer. You can just run docker run -it mjambon/ocaml:alpine
and cat /Dockerfile
to see the setup. Demo:
$ docker run -it mjambon/ocaml:alpine
Running "eval $(opam env)", which initializes PATH and other variables.
In scripts and dockerfiles, dont forget to run your commands as
opam exec -- COMMAND
or equivalently as
eval $(opam env) && COMMAND
bash-5.0$ opam switch
# switch compiler description
-> 4.10.0 ocaml-base-compiler.4.10.0 4.10.0
default ocaml-system.4.08.1 default
bash-5.0$ cat > toto.ml
Printf.printf "%f\n" (Unix.gettimeofday ())
bash-5.0$ ocamlopt -o toto -ccopt -static unix.cmxa toto.ml
bash-5.0$ ldd toto
/lib/ld-musl-x86_64.so.1 (0x7f12214ae000)
bash-5.0$ file toto
toto: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), statically linked, with debug_info, not stripped
bash-5.0$ ./toto
1612212056.085955
If you don’t want a position-independent executable (PIE) static binary (not sure when this is needed), on Alpine you have to pass -ccopt -no-pie
:
bash-5.0$ ocamlopt -o toto-no-pie -ccopt -static -ccopt -no-pie unix.cmxa toto.ml
bash-5.0$ ldd toto-no-pie
/lib/ld-musl-x86_64.so.1: toto-no-pie: Not a valid dynamic program
bash-5.0$ file toto-no-pie
toto-no-pie: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, with debug_info, not stripped
There are no issues with installing the packages you mentioned:
bash-5.0$ opam install ppx_deriving ocaml-protoc-yojson
...
<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><>
-> installed easy-format.1.3.2
-> installed biniou.1.2.1
-> installed ocaml-compiler-libs.v0.12.3
-> installed ocaml-migrate-parsetree.2.1.0
-> installed ppx_derivers.1.2.1
-> installed sexplib0.v0.14.0
-> installed stdlib-shims.0.1.0
-> installed ppxlib.0.21.0
-> installed ppx_deriving.5.2
-> installed yojson.1.7.0
-> installed ocaml-protoc-yojson.0.2.0
Done.