« Return to Thread: "ocaml_beginners"::[] Conditional compilation

Re: "ocaml_beginners"::[] Conditional compilation

by Richard Jones-4 :: Rate this Message:

Reply to Author | View in Thread

On Mon, Oct 29, 2007 at 12:35:20PM +0100, Joerg van den Hoff wrote:

> On Sun, Oct 28, 2007 at 08:07:46PM -0000, cultural_sublimation wrote:
> > Fellow Camels,
> >
> > How does one handle conditional compilation in OCaml?  I want certain
> > pieces of debugging code to be included only if a certain environment
> > variable is set.  This sort of thing is really easy to do in the C
> > preprocessor with #ifdefs.
> >
> > And yes, I know I can use cpp also with OCaml, but I was wondering
> > if there was a more "bactrian" way of doing this.
>
> is  using  `Sys.getenv'  to  simple  minded  and  out of the
> question?  this is not conditional compilation, sure, but if
> you      use      a     reasonable     "unprobable"     name
> ("MYPROGRAM_DEBUGGING_OUTPUT"  ...)  for   the   environment
> variable, it would do the job, essentially.

Conditional code works for simple debugging case, but in other cases
you often need C-like macros, impotent as they may be compared to real
macros.

For example:

  let package = "@PACKAGE@"
  let version = "@VERSION@"

where @PACKAGE@ and @VERSION@ are substituted in your Makefile or
configure script.  To do this, just isolate these definitions into a
separate module and use autoconf.

Another example:

  let dialog =
  #if LABLGTK <= 2.0
    GDialog.dialog ~main_title:title ()
  #else
    GDialog.dialog ~title ()
  #endif

[This isn't an exact example, but lablgtk does change the types of its
API calls often enough that compiling against several versions
requires C macros and cannot be done using ordinary if ... then ...
clauses].

Rich.

--
Richard Jones
Red Hat

 « Return to Thread: "ocaml_beginners"::[] Conditional compilation