Is it time to stop using CAMLprim, CAMLexport, and CAMLextern?

I was curious why we have these macros, when both CAMLprim and CAMLexport are empty definitions and CAMLextern is simply extern. The comment above these defines in misc.h says something about Windows DLLs, but it’s unclear what that could possibly mean.

I traced the history of misc.h and finally found this commit from Alain Frisch:

It seems that these macros did have some significance before OCaml 3.11, as they would emit Microsoft-specific attribute syntax causing the definitions to be visible from a DLL. Since the linked diff, these macros are vestigial.

I understand that the OCaml runtime headers need to continue to define these macros for backwards compatibility, but I’ve also noticed that new C code continues to use these macros. The OCaml documentation for Interfacing with C includes CAMLprim in its examples.

Is it by design that C stub authors should continue to use these macros?


I have found that they still have some good value as documentation (especially CAMLprim).

I believe all 3 of these macros are implementation detail of OCaml runtime and are not needed to be used by stubs authors at all. In particular CAMLprim is used to get the list of primitives for bytecode runtime (with some custom post-processing during ocaml build), which has no meaning for external stubs. Looks like some sort of cargo cult that these spread into many bindings. It is unfortunate that examples in manual mention CAMLprim (as examples of code but without explaining), I think it is an oversight and should be cleaned up.


I believe these macros used to be needed a long time ago to correctly expose the primitives for dynlinking (at least on Windows, if memory serves), but indeed they are no longer needed for user code.

1 Like