gpt4 book ai didi

c - Fortran 模块与在 C 中访问的过程相结合,反之亦然

转载 作者:行者123 更新时间:2023-12-01 13:49:49 25 4
gpt4 key购买 nike

我正在尝试使用模块和过程来测试 Fortran/C 混合语言。我使用了此链接中的基本示例案例:http://cftcc.iccas.ac.cn/upload/doc/ifc/f_ug1/pgwusmod.htm但是当我尝试修改代码时,我开始收到类似

的错误
"_initfo_", reference from: _MAIN__ in main.o
ld: symbol(s) not found for architecture x86_64.

这是我的代码:

new.F >>
MODULE EXAMP
use iso_c_binding
REAL, bind(C) :: A(3)
INTEGER I1, I2
CHARACTER(80) LINE
TYPE MYDATA
SEQUENCE
INTEGER N
CHARACTER(30) INFO
END TYPE MYDATA
END MODULE EXAMP
cnew.c >>
/* C code accessing module data */
extern float a[3];
extern int examp_mp_i1, examp_mp_i2;
extern char examp_mp_line[80];
//extern void usemodule();
extern struct {
int n;
char info[30];
} examp_mp_mydata;

void pythagoras (float *c){
*c = (float) sqrt(a[0]*a[0] + a[1]*a[1]);
}

void initfo(float *aa){
*aa = a[0]+a[1]+a[2];
}
main.F >>
! Fortran 95/90 Module including procedure
MODULE CPROC
INTERFACE
SUBROUTINE PYTHAGORAS ( res)
!DEC$ ATTRIBUTES C :: PYTHAGORAS
!DEC$ ATTRIBUTES REFERENCE :: res
! res is passed by REFERENCE because its individual attribute
!: overrides the subroutine's C attribute
REAL res
! a and b have the VALUE attribute by default because
! the subroutine has the C attribute
END SUBROUTINE
END INTERFACE
END MODULE

! Fortran 95/90 Module including procedure
MODULE CCPROC
INTERFACE
SUBROUTINE INITFO (aa)
REAL aa
END SUBROUTINE
END INTERFACE
END MODULE

PROGRAM MAIN
USE EXAMP
! Fortran 95/90 Module including procedure
USE CPROC
USE CCPROC
A(1)=1.0
A(2)=2.0
A(3)=3.0
WRITE(*,*) A(1)
CALL PYTHAGORAS ( X)
WRITE(*,*) X

CALL INITFO(Y)
WRITE(*,*) Y
END PROGRAM MAIN

我正在使用英特尔编译器。这是我编译时所做的:

icc -c cnew.c
ifort -c new.f
ifort -o test main.f new.o cnew.o

我对 Fortran 很陌生。我真的希望有人能指出我正确的方向。

谢谢,

最佳答案

在 Fortran 2003 中,您可以这样声明接口(interface):

  INTERFACE
SUBROUTINE PYTHAGORAS (res) bind(c, name='pythagoras')
use iso_c_binding
real(kind=c_float) :: res
END SUBROUTINE

SUBROUTINE initfo (aa) bind(c, name='initfo')
use iso_c_binding
real(kind=c_float) :: aa
END SUBROUTINE
END INTERFACE

这里的要点是添加到子程序声明中的名称属性,它告诉编译器实际使用哪个符号。否则名称重整会给你找不到的实体。

总共得到:new.f90

MODULE EXAMP
use iso_c_binding

REAL(kind=c_float), bind(c) :: A(3)
INTEGER :: I1, I2
CHARACTER(80) :: LINE

TYPE MYDATA
SEQUENCE
INTEGER :: N
CHARACTER(len=30) :: INFO
END TYPE MYDATA

END MODULE EXAMP

main.f90:

MODULE CPROC
use iso_c_binding
INTERFACE
SUBROUTINE PYTHAGORAS (res) bind(c, name='pythagoras')
use iso_c_binding
real(kind=c_float) :: res
END SUBROUTINE

SUBROUTINE initfo (aa) bind(c, name='initfo')
use iso_c_binding
real(kind=c_float) :: aa
END SUBROUTINE
END INTERFACE
END MODULE

PROGRAM MAIN
USE EXAMP
! Fortran 95/90 Module including procedure
USE CPROC

A(1)=1.0
A(2)=2.0
A(3)=3.0
WRITE(*,*) A(1)
CALL PYTHAGORAS(X)
WRITE(*,*) X

CALL INITFO(Y)
WRITE(*,*) Y
END PROGRAM MAIN

C 代码不变。我强烈建议您不要使用像您的 a 这样的全局变量。

关于c - Fortran 模块与在 C 中访问的过程相结合,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32793723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com