Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran 2003 bindings to library in C: how to translate enums and #defines?

I am writing Fortran bindings for C library.

What is best practice on translating constants defined (in library headers) using enum, e.g.

typedef enum cufftType_t {
  CUFFT_R2C = 0x2a,     // Real to Complex (interleaved)
  CUFFT_C2R = 0x2c,     // Complex (interleaved) to Real
  CUFFT_C2C = 0x29,     // Complex to Complex, interleaved
  CUFFT_D2Z = 0x6a,     // Double to Double-Complex
  CUFFT_Z2D = 0x6c,     // Double-Complex to Double
  CUFFT_Z2Z = 0x69      // Double-Complex to Double-Complex
} cufftType;

and what on translating constants using #define, e.g.

#define CUFFT_FORWARD -1 // Forward FFT
#define CUFFT_INVERSE  1 // Inverse FFT

or combinations of those

typedef enum cufftCompatibility_t {
    CUFFT_COMPATIBILITY_NATIVE          = 0x00, 
    CUFFT_COMPATIBILITY_FFTW_PADDING    = 0x01,    // The default value
    CUFFT_COMPATIBILITY_FFTW_ASYMMETRIC = 0x02,
    CUFFT_COMPATIBILITY_FFTW_ALL        = 0x03
} cufftCompatibility;

#define CUFFT_COMPATIBILITY_DEFAULT   CUFFT_COMPATIBILITY_FFTW_PADDING

Should this information be in a separate file, INCLUDE-d, or simply in a USE-d module?

like image 477
Jakub Narębski Avatar asked Oct 09 '22 08:10

Jakub Narębski


1 Answers

The enums are actually supported by the ISO_C_Binding, see for example this documentation: http://docs.cray.com/books/S-3693-36/html-S-3693-36/z1018297010.html as for the literal constants defined in the preprocessing: you could run the C-Preprocessor on your Fortran files. Or you use an extra module, where you define all those constants (and running the C-preprocessor only over that one file). In any case I would use Modules to be used and not includes. Though that has the drawback, that you depend on module files, which are generated by the compiler and are compiler specific, this could be avoided when using includes, but would be less "Fortran-like".

like image 185
haraldkl Avatar answered Oct 13 '22 11:10

haraldkl