Different C compiler invocation when compiling C stubs with `ocamlc` and `ocamlopt`

Is it expected that compiling C stubs with ocamlc and ocamlopt yields two different C invocations ?

> ocamlopt '-verbose' '-c' '-g' src/zstd/bytesrw_zstd_stubs.c
+ gcc -O2 -fno-strict-aliasing -fwrapv  -pthread   -D_FILE_OFFSET_BITS=64    -c -g   -I'/home/debian/.opam/default/lib/ocaml' 'src/zstd/bytesrw_zstd_stubs.c'

> ocamlc '-verbose' '-c' '-g' src/zstd/bytesrw_zstd_stubs.c
+ gcc -O2 -fno-strict-aliasing -fwrapv -fPIC -pthread   -D_FILE_OFFSET_BITS=64    -c -g   -I'/home/debian/.opam/default/lib/ocaml' 'src/zstd/bytesrw_zstd_stubs.c'

The lack of -fPIC in the first case makes ocamlmklib fail.

> ocaml --version
The OCaml toplevel, version 5.3.0

> gcc --version
gcc (Debian 12.2.0-14) 12.2.0

From memory, it has been like this for a long time. Maybe the idea is that if you compile a C file with ocamlopt you are aiming to link it statically with the final executable. If you are going to pass it to ocamlmklib you should always compile it with ocamlc instead (this is what Dune does, incidentally).

Cheers,
Nicolas

Sigh. Yet another toolchain wart.

This is a regression (I’m wondering if there’s been a slip-up in some of the PRs around Don't use configured CFLAGS & CPPFLAGS to compile third-party C sources by shindere · Pull Request #12589 · ocaml/ocaml · GitHub) - 5.2.1 has the same flags for each.

1 Like

This is tracked upstream (by the now ill–named) Document properly that C stubs for libraries to be given with `ocamlmklib` must be compiled with `ocamlc` and not `ocamlopt` · Issue #13798 · ocaml/ocaml · GitHub