how to call a Fortran90 function included in a module in c++ code?

前端 未结 2 1111
北海茫月
北海茫月 2020-12-15 12:59

I m including a fortran90 program that is not mine in my C++ project .

In the first stept I try to call the function by their name_() and i get the error \"undefin

2条回答
  •  半阙折子戏
    2020-12-15 13:39

    I am assuming you are using the g++, gfortran, mpif90 toolchain. If you have a module

    module rocker
    contains
    subroutine bye_baby
    ...
    end subroutine
    

    The external C declaration for it in C++ is

    extern "C"
    {
        //          ,-- 2 Leading underscores to start
        //          | ,-- then the module name
        //          | |     ,-- then _MOD_
        //          | |     |    ,-- then the subroutine name
        //          V V     V    V
        extern void __rocker_MOD_bye_baby();
    }
    

    You may also need to add attribute((stdcall)) after the extern. By default, C assumes cdecl which stacks the parameters differently.

    If you do not want the __rocker_MOD part, then the subroutine/function should not be declared in a module.

提交回复
热议问题