在C ++中使用Fortran模块时出现单元标识符问题

时间:2017-05-24 19:00:30

标签: c++ fortran interop

我的主程序是C ++,我需要使用现有Fortran程序的模块。我能用g ++链接到它。 问题是,fortran代码中的所有子程序都是由Fortran主程序调用的。在全球范围内,后者中的全局变量的初始化是通过在命令行上引导输入文件来完成的:

main_fortran_prog < inputFile.ext

给定的子程序“foubar”需要设置这些变量。为此,它从单元标识符5中读取,根据我阅读的文档,它是从带有READ语句的键盘输入INPUT。

现在,我想在我的C ++ main中调用子程序“foubar”,它将在子程序操作之前初始化这些变量“real_fou”和“bool_flag”。知道“foubar”会从我想要避免的单位标识符5中查看名单中的real_fou和bool_flag的值,对我来说最好的选择是什么?

总之:我想从Fortran main迁移到C ++ main。我正在尝试尽可能避免从文件中读取名单

我目录中的文件列表:

inputFile.ext

! this file is used by fortran main program to initialize global variable
&justANamelist
    real_fou = 0.3490834
    bool_flag = .true.
/

afortmodule.f90

module afortmodule
implicit none
integer :: ios
logical, save :: bool_flag
real, save :: real_fou

contains

subroutine foubar() bind(c, name='foubar')
    use iso_c_binding
    call parseInput
    write(*,*) "real_fou x 2 = " , real_fou * 2
end subroutine

subroutine parseInput
    ! Define the namelist
    NAMELIST /justANamelist/ real_fou, bool_flag
    ! How to bypass this in cpp call ?
    read(5,nml=justANamelist, IOSTAT=ios)
end subroutine

end module afortmodule

main_fortran.f90

! require < inputFile.ext
program mainfortranprog
use afortmodule
! call foubar to display real_fou x 2
call foubar
end program mainfortranprog

main_cpp.cpp

// This want to call the subroutine foubar in fortran module afort_module
// like main_fortran did but avoiding the commandline input like "<  inputFile.ext"
#include <iostream>

using namespace std;

void call_foubar(void);
extern "C"{
    void foubar (void);
    bool bool_flag;
    double real_fou;
}

int main (void){
    real_fou = 3.13215163;
    bool_flag = true;

    //How can I use these values in namelist justANamelist 
    //in manner that I could be able to do:
    call_foubar();

    return 0;
}

void call_foubar(void){
    // some codes here
    foubar();
}

0 个答案:

没有答案