在混合C/Fortran应用程序的上下文中,有没有办法检查编译器是否知道"iso_c_binding"(例如GCC 4.1.2不知道它,而4.3.4确实如此),就像预处理指令一样或者其他的东西?我不能简单地检查GCC的版本,因为我可能会使用其他编译器.
谢谢
我将值分配给状态数组,如下所示:
status [i] + = 1;
我想从fortran访问这个数组
如何访问这个数组?
例如,我想从fortran更改STAT的值,如下所示:
STAT(2)= 3
这可能吗?
c来源
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/stat.h>
void call_fc_ (int *key, int *addr, int *size, int *status)
{
int i;
int shmid;
void* shared_addr;
//printf("first ptr = %p\n", *addr);
shmid = shmget (*key, *size, IPC_CREAT | IPC_EXCL | 0666);
if (shmid == -1)
{
printf("shmget is failed!\n");
exit(0);
}
shared_addr = (void*) shmat(shmid, 0, 0);
status = (int*)shared_addr;
//printf("status ptr = %p\n", status);
int data_size = …Run Code Online (Sandbox Code Playgroud) 是否可以将fortran 77函数作为回调函数指针传递给C/C++?如果是这样,怎么样?
我在网上找到的信息与fortran 90及以上相关,但我遗留的代码库是77.
非常感谢
我认为头衔说我需要什么.我知道我们可以使用"asd"函数来执行此操作,但由于某些原因,我需要在Fortran中进行分配(即在子例程"asd_"中).这是C代码:
#include <stdio.h>
void asd(float **c) {
*c = (float *) malloc (2*sizeof(float));
**c =123;
*(*c+1)=1234;
}
void asd_(float **c);
main () {
float *c;
asd_(&c);
// asd(&c); would do the job perfectly
printf("%f %f \n",c[0],c[1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是Fortran代码:
subroutine asd(c)
implicit none
real, pointer, allocatable ::c(:)
print *, associated(c)
if(.not. associated(c)) allocate(c(2))
end subroutine
Run Code Online (Sandbox Code Playgroud)
这随机给出了分段错误.任何帮助,将不胜感激.
我最近遇到过一种情况,我想从Fortran调用一个C函数,因为它有一段很有用的C代码.为了方便数组操作,我希望能够使用数组参数调用此函数,但它只接受标量参数.
在Fortran中,当然可以简单地声明一个elemental实现此目的的过程,并且可以声明一个C过程的接口bind(C).但是,由于C没有元素过程的概念,Fortran(2008)标准排除了这种组合:
C1246元素过程不应具有BIND属性.
那么,这个功能可以在Fortran中实现吗?
我刚开始学习 Fortran,遇到了这个问题。考虑下面的简单代码。
PROGRAM random
INTEGER, DIMENSION(12):: array
PRINT *, array
END PROGRAM random
Run Code Online (Sandbox Code Playgroud)
将array未分配的值,但可以打印,它似乎有一些随机的因素和一些零元素。但是,如果我考虑更短的数组,请说我声明
INTEGER, DIMENSION(5):: array
Run Code Online (Sandbox Code Playgroud)
那么打印的数组的所有元素都为 0。我想知道这里发生了什么?
我正在尝试使用iso_c_bindings模块将Fortran 2003绑定写入CUFFT库,但我遇到cufftPlanMany子程序问题(类似于sfftw_plan_many_dftFFTW库).
绑定本身如下所示:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! cufftResult cufftPlanMany(cufftHandle *plan, int rank, int *n,
! int *inembed, int istride, int idist,
! int *onembed, int ostride, int odist,
! cufftType type, int batch)
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
interface cufftPlanMany
subroutine cufftPlanMany(plan, rank, n, &
inembed, istride, idist, &
onembed, ostride, odist, &
type, batch) &
& bind(C,name='cufftPlanMany')
use iso_c_binding
integer(c_int):: plan
integer(c_int),value:: rank, type, batch
integer(c_int):: n(*)
integer(c_int),value:: istride, idist, ostride, odist
integer(c_int):: inembed(*), onembed(*)
end subroutine cufftPlanMany
end …Run Code Online (Sandbox Code Playgroud) fortran compiler-errors gfortran fortran90 fortran-iso-c-binding
嘿那里,如果env var"XYZ"设置为编译时,比我想要的部分:
write (STDOUT,*) "Compiled with XYZ"
here one more function call bla()
Run Code Online (Sandbox Code Playgroud)
要编译成二进制文件.如果不是,不是.有办法吗?非常感谢!
我正在使用 C++ 和 Fortran 代码。C++ 代码需要调用 Fortran 子例程。Fortran 子例程的一个参数具有 Fortran 类型complex。
C++ 代码位于一个文件中,Fortran 代码子例程位于另一文件中。我正在64位系统上使用gccand 。gfortranGNU/Linux
下面是显示 Fortran 子例程声明的片段(以及一些附加行):
SUBROUTINE minp (AMP,L,L2,FMINP,PHI)
REAL*4 AMP( L ),FMINP( L )
COMPLEX PHI( L )
Run Code Online (Sandbox Code Playgroud)
在 C++ 文件中,我知道传递给 Fortran 代码的参数需要通过引用传递,而不是通过值传递。Fortran 子例程使用关键字在 C++ 代码(位于源代码文件的顶部)中声明为函数extern。
extern "C"
{
minp_ (float *amp, int &L, int &L2, float *fminp, complex *phi);
}
Run Code Online (Sandbox Code Playgroud)
然而,该函数的最后一个参数是一个复杂的 C 数组。怎么可能:
phi参数传入?我正在尝试使用C++应用程序中的Fortran代码.具体来说,我正在尝试与SLATEC的drc3jj.f进行交互.但是,Fortran子例程返回一个数组,该数组的大小取决于传递给函数的参数.
如果数组的大小为1,则我打印的C++数组包含适当的值.但是,如果此大小大于1,则C++数组包含应存在输出值的NaN.
以下是我使用的代码.这只是将Fortran子例程链接到C++应用程序.
#ifndef FORTRANLINKAGE_H
#define FORTRANLINKAGE_H
extern "C"
{
extern void drc3jj_(double*,double*,double*,double*,double*,
double*,double [],int*,int*);
}
#endif // FORTRANLINKAGE_H
Run Code Online (Sandbox Code Playgroud)
肉在这里.我们实际上从C++调用Fortran子例程并打印输出:
#include "fortranLinkage.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
void wigner3j(double l2, double l3, double m2, double m3, double coeff [])
{
double l1min,l1max;
int ierr,size(3);
drc3jj_(&l2,&l3,&m2,&m3,&l1min,&l1max,coeff,&size,&ierr);
cout << "Min: " << l1min << "\t Max: " << l1max << "\t Err: " << ierr << endl;
}
int main(int argc, char const *argv[])
{
int l1(atoi(argv[1])),l2(atoi(argv[2])),m2(atoi(argv[3])),m3(atoi(argv[4]));
double …Run Code Online (Sandbox Code Playgroud) fortran ×10
c ×5
c++ ×3
binary ×1
environment ×1
fortran2003 ×1
fortran77 ×1
fortran90 ×1
gfortran ×1
hardcode ×1
interop ×1
preprocessor ×1
variables ×1