Gaius
Gaius

Reputation: 2595

Detect OCaml version when compiling C

I am working on some code that mixes OCaml and C, the functions caml_release_runtime_system() and caml_acquire_runtime_system() were introduced in OCaml 3.12 (they were called something else in earlier versions) but I would like to be compatible back to 3.10 if possible, is there an #ifdef I can use for that? I have had a look through the headers (in /usr/lib/ocaml/caml on my Debian system) and can't find anything that looks like it might. Thanks!

UPDATE: this is what I did

This is what I did:

#if OCAML_VERSION_MINOR >= 12
#include <caml/threads.h>
#else
#include <caml/signals.h>
#endif 

#ifndef caml_acquire_runtime_system
#define caml_acquire_runtime_system caml_leave_blocking_section
#define caml_release_runtime_system caml_enter_blocking_section
#endif

Upvotes: 4

Views: 237

Answers (2)

ygrek
ygrek

Reputation: 6697

caml/threads.h:

#define caml_acquire_runtime_system caml_leave_blocking_section
#define caml_release_runtime_system caml_enter_blocking_section

So simply add that same lines to your C code (guarded with #ifndef caml_acquire_runtime_system) and spare your build system (and users) from depending on external utilities and version numbers.

Upvotes: 3

mu is too short
mu is too short

Reputation: 434665

The ocamlopt and ocamlc binaries supports -vnum and -version switches for getting the version number:

-vnum or -version Print the version number of the compiler in short form (e.g. 3.11.0), then exit.

This switch is supported in 3.12.0 and the example text in the documentation suggests that 3.11.0 also supports it. I don't have 3.10.0 handy but nlucaroni (whose OCaml Fu looks stronger than mine) indicates in the comments that 3.10.0 does have ocamplopt -version.

So you could add something like this to your Makefile:

OCAML_VERSION_MAJOR = `ocamlopt -version | cut -f1 -d.`
OCAML_VERSION_MINOR = `ocamlopt -version | cut -f2 -d.`
OCAML_VERSION_POINT = `ocamlopt -version | cut -f3 -d.`

And then pass those to your compiler using -DOCAML_VERSION_MAJOR=$(OCAML_VERSION_MAJOR), -DOCAML_VERSION_MINOR=$(OCAML_VERSION_MINOR), ...

Upvotes: 3

Related Questions