Calling a C function from Fortran where the C function name was originally passed in from C

后端 未结 2 960
谎友^
谎友^ 2021-01-22 14:42

For reasons that are not relevant, I need to pass a C/C++ function name into a Fortran subroutine, which, in turn, calls that C function. What I have found is that I can succesf

2条回答
  •  时光取名叫无心
    2021-01-22 15:37

    I would change the interface definition to read

    interface
        subroutine calledfromFortran(status) bind (c)
            use iso_c_binding
            integer(kind = c_int), VALUE :: status
        end subroutine calledfromFortran
    end interface
    

    I am not sure what intent(in) was, but that bit of the code does not look right. Apart for that, the rest looks (at first pass) reasonable and right.

    Note. The ISO C bindings were only added in the 2003 releae of the FORTRAN language, so if you are using an older version it might be worth checking the compiler documentation for details. Some compilers allow ISO C bindings, but maybe called slightly differently than that I have displayed above.


    Edit. Having looked into the intent keyword, you might try using intent(in) in conjunction with the following type declaration, that follows the interface definition

    integer (c_int), parameter :: one = 2
    

    I hope this helps.

提交回复
热议问题