0

My objectif is to rename a fortran subroutine inside a module to be easily callable by C code (i.e. without the __<modulename>_MOD_ prefix), and using GCC-6.3.0. I cannot use bind(c,name='') even if it works great. I have read that I should use interface, but without any success. Here is the MWE:

module testmodule
    interface
       subroutine func02
       !GCC$ ATTRIBUTES CDECL :: func01
       end subroutine
    end interface

    contains

    subroutine func01
      print*,"func01"
    end subroutine
end module 

I compile using command gfortran -c test.f90 and then I check if the subroutine is correctly renamed using nm test.o, but there is no sign of func02. Any help appreciated.

4
  • Have you tried with the DLLEXPORT attribute? Commented Jun 22, 2018 at 14:15
  • 1
    Why can't you use bind(c)? Whatever stops you using that may also stop you using any other suggestion, so it would be useful for you to clarify. Commented Jun 22, 2018 at 14:20
  • 1
    @RodrigoRodrigues DLLEXPORT did not work either. Also I am working on Linux. @francescalus It is more than 10000 lines of code with high complexity, using bind(c) changes many thing starting with c-structure that are transmitted from C to Fortran very differently (with a void *). I need this specific MWE to work. Thanks both for your interest in the question. Commented Jun 22, 2018 at 14:30
  • If there is some important change that BIND(C) makes to the nature of your procedure, then your example needs to include that so that we can advise you appropriately. If you have existing C code calling an existing Fortran procedure, but with a long funny MOD name, then it should be reasonably straight forward to construct a BIND(C) wrapper (you don't have to change the original Fortran - you can add another procedure as an intermediate) for that existing procedure that maintains binary compatibility, bar the name. Commented Jun 23, 2018 at 7:33

1 Answer 1

1

You can use BIND(C) to rename the subroutine. Whatever you read about INTERFACE seems to be a red herring.

module testmodule
   contains
      subroutine func01() bind(c, name='func2')
         print*,"func01"
      end subroutine
end module

With the simple command 'gfortran -c a.f90', I see the following results

nm a.o
     U _gfortran_st_write
     U _gfortran_st_write_done
     U _gfortran_transfer_character_write
00000000 T func2
Sign up to request clarification or add additional context in comments.

6 Comments

sorry but bind(c) is forbidden for me like I explained. I agree you code works, but it will not for me.
@ThomasGuenet, What to you mean forbidden?
I cannot use it because of complexe fortran code structure implying changing too many code. Hence bind(c) is a solution for anyone starting a new project, but I asked for another solution : it is the purpose of this question.
You can use bind(c) in a single place to generate a correct binding name and not use it anywhere else. I don't understand how bind(c) would cause any issue to the rest of your code base.
@evets, It's not impossible. ThomasGuenet, you can use gfortran to generate an assembly file. Feed that file through sed to strip out the prefix. Then use gfortran to compile the sed-edited assembly. Good luck, or use BIND(C).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.