C-Fortran混合编程
我正在开发一个必须在 C 语言中使用 Fortran 库的项目。在 Fortran 库中,有一个包含复杂*16、4x4 数组的公共块。现在,在 C 中,复数变量只是一个包含两个元素的结构体,由于它是复数*16,因此元素应该是 long double,这是 Fortran 中相应的 C 数据类型。所以我有一个带有两个长双精度的结构。
我要访问这个数组的元素。好消息是,我已经可以与库的所有其他常见变量一起做到这一点。问题是我从数组导入的元素是,
1)不按应有的顺序,“即使考虑到 C 和 Fotran 数组结构的差异”。
2)虽然大多数要素都是正确的,但有两个与应有的情况有很大不同。
3)只有当我使用 double 而不是 long double 时,我才能得到正确的元素(除了两个)。当我使用 long double (以及正确的字符转换)时,我得到了完全不同的东西,这清楚地表明转换存在问题。
我已经用尽了所有的解释,但没有任何效果。我在 C 中打印数组的代码如下:
for (j=0;j<=3;j++){
printf("%s", "\n");
for(k=0;k<=3;k++){
printf("%s %d %s %d %s %s %LE %s %LE %s",
"(", k+1, "," ,j+1, ")", "{",
(long double)mssmmixing_.neunmx[k][j].dr,
" ",
(long double)mssmmixing_.neunmx[k][j].di,
"}\n");
}
}
附加信息:由于我必须混合 Fortran 对象文件,因此我使用 gfortran 来编译 C 文件。如果我使用 GNU C 编译器,它会抛出无法识别 gfortran 例程的错误。这也可能是问题的根源,可能是 gfortran 无法识别 C 中的长双精度数。
任何帮助都会有用。
I am working on a project where I have to use a Fortran library in C. In the Fortran library there is a common block containing a complex*16, 4x4 array. Now in C, a complex variable is simply a struct containing two elements and since it is complex*16, the elements should be long double, which is the corresponding C data type in Fortran. So I have a struct with two long doubles.
I am to access the elements of this array. Good thing is, I can already do that along with all other common variables of the library. The problem is that elements that I import from the array are,
1) Not in the order as the should be, "even after taking into account the difference in the array structure of C and Fotran".
2) While most elements are right, two are very different from what they should be.
3) I get the right elements (except for the two) only if I use double instead of long double. When I use long double (and the correct character conversions) I get something entirely different which clearly points to a problem with conversions.
I have exhausted every explaination I had but nothing works. My code for priting arrays in C is the following:
for (j=0;j<=3;j++){
printf("%s", "\n");
for(k=0;k<=3;k++){
printf("%s %d %s %d %s %s %LE %s %LE %s",
"(", k+1, "," ,j+1, ")", "{",
(long double)mssmmixing_.neunmx[k][j].dr,
" ",
(long double)mssmmixing_.neunmx[k][j].di,
"}\n");
}
}
Additional Info: Since I have to mix Fortran Object files, I am using gfortran to compile the C files. If I use GNU C compiler instead, it throws errors about not recognizing gfortran routines. This might also be a source of problem, may be the gfortran does not recognize long doubles in C.
Any help will be useful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于混合 Fortran 和 C,我建议使用 ISO_C_Binding。它甚至有一个与 C 类型 long double _Complex 相匹配的 Fortran 类型 C_LONG_DOUBLE_COMPLEX - 请参阅 http://gcc.gnu.org/onlinedocs/gfortran/ISO_005fC_005fBINDING.html。作为 Fortran 语言标准的一部分,这些类型保证匹配(当您使用兼容的编译器时)。很可能C_LONG_DOUBLE_COMPLEX实际上与complex*16相同,但您可以尝试在Fortran中在两种类型之间进行复制,以防内存布局不同。您应该使用 gfortran 编译 Fortran 源文件,使用 gcc 编译 C 源文件。与 gfortran 链接是最简单的。 (或使用其他“品牌”编译器。)
For mixing Fortran and C, I recommend the use of the ISO_C_Binding. It even has a Fortran type C_LONG_DOUBLE_COMPLEX that matches the C type long double _Complex -- see http://gcc.gnu.org/onlinedocs/gfortran/ISO_005fC_005fBINDING.html. As part of the Fortran language standard these types are guaranteed to match (when you use compatible compilers). Very likely C_LONG_DOUBLE_COMPLEX is actually the same as complex*16 but you could try, in Fortran, copying between the two types in case the memory layout is different. You should compile the Fortran source files with gfortran and the C with gcc. It is easiest to link with gfortran. (Or use other "brand" compilers.)