Fortran 2003 与 C 库的绑定:如何翻译枚举和#defines?
我正在为 C 库编写 Fortran 绑定。
使用 enum 翻译(在库头中)定义的常量的最佳实践是什么,例如
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;
使用 #define
< /strong>,例如
#define CUFFT_FORWARD -1 // Forward FFT
#define CUFFT_INVERSE 1 // Inverse FFT
或这些信息的组合
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
此信息应该位于单独的文件、INCLUDE-d 中,还是简单地位于 USE-d 模块中?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些枚举实际上受 ISO_C_Binding 支持,例如,请参阅此文档: http://docs.cray.com/books/S-3693-36/html-S-3693-36/z1018297010.html
至于预处理中定义的文字常量:您可以在 Fortran 文件上运行 C 预处理器。或者您使用一个额外的模块,在其中定义所有这些常量(并且仅在该文件上运行 C 预处理器)。
无论如何,我会使用要使用的模块,而不是包含的模块。尽管这有一个缺点,即您依赖于由编译器生成且特定于编译器的模块文件,但在使用 include 时可以避免这种情况,但会不太“像 Fortran”。
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".