[Pyrex] #if

Francesc Alted falted at openlc.org
Fri Oct 17 19:32:09 CEST 2003


A Divendres 17 Octubre 2003 18:39, Bryan Weingarten va escriure:
> hmmm.... i should have know better regarding the #.  but what you
> suggest won't work for me since the _map_win32 function won't link since
> it doesn't exist on non-windows platforms.

You may want to use the next trick to get that. Imagine, as for one, that
your pyrex extension has to know if it can use a certain library (ucl) that
only exists on Windows. First, define in Pyrex a function that determines if
your function is accessible or not:

cdef int ucl_version
cdef extern from "myucl.h":
  int register_ucl()
  
# Initialize & register ucl
ucl_version = register_ucl()

then, in the myucl.c, you have the next definition of register_ucl:

#ifdef _windows
#   include "ucl/ucl.h"
#endif

int register_ucl(void) {
#ifdef _windows
  /* Init the ucl library */
  if (ucl_init()!=UCL_E_OK)
    printf("Problems initializing UCL library\n");
  return UCL_VERSION; /* lib is available */
#else
  return 0; /* lib is not available */
#endif /* _windows */

after that, in your pyrex extension, you can do something like:

if ucl_version:
    # Do whatever you want with the library
    ...
else:
    # define a fallback action
    ...

This works pretty well, at least for me.

-- 
Francesc Alted





More information about the Pyrex mailing list