2

I am writing a static library by C++, expecting it to be used by either Fortran or C. Since Fortran has all its index starting from 1, I have to do some index modification inside my library when called by Fortran. Because I am passing an array of indices to the library and it is important for further computation.

Of course an intuitive way to solve this problem is to set a argument at interface to let user tell me what language they are using, but I don't think it is a cool way to do this.

So I wonder if there is anyway to detect in my library if it is called by Fortran or C?

Thanks!

Blue_Black
  • 307
  • 1
  • 3
  • 11
  • 2
    You certainly don't *have* to be that nice to Fortran users. When you call a Fortran library from C, the library doesn't do any index modifications for you. – n. m. could be an AI Apr 11 '13 at 21:13
  • There is really no reason to do such a think. Array indexing is just a map to offsets in memory. It doesn't matter at all if the other language maps this differently. In Fortran you can have different routines to index the same array differently and it is no problem at all. (**Fortran is not strictly 1 based**!) – Vladimir F Героям слава Apr 12 '13 at 08:25

3 Answers3

1

If you are just passing arrays and their lengths, there shouldn't be any issue. The problem is only if you pass an index, then you need to know what that index is relative to. (Which in Fortran could be any value, if the array is explicitly declared to start with an index other than one). If you have this case, my suggestion is to write glue routines for one of the languages that will convert the index values, then call the regular library routines. The problem with this solution is that it obligates the user of the "special" language to call the special glue routines; calling the regular routines is a mistake.

M. S. B.
  • 28,968
  • 2
  • 46
  • 73
  • M. S. B., thanks for your answer. Yes, you are right I'm passing an array of index and I have edited the question to make it clear. But I think making users call a special routine is almost the same with letting them put one more parameter in the routine to tell me what language they are using. Thanks again! – Blue_Black Apr 12 '13 at 22:22
  • Another approach, instead of passing a parameter stating the language, is to pass the starting index value. This would also handle the case of a non-default starting index in Fortran. – M. S. B. Apr 13 '13 at 00:27
0

Applications and libraries in any language will be built to target the same ABI. The ABI defines calling conventions and other details that make it possible for two functions built by different compilers (possibly for different languages) to call each other. There shouldn't be anything obviously different in the calls because great effort has gone into avoiding those differences.

You could look for out-of-band information like symbols provided by the FORTRAN compiler (or pulled in from its utility libraries). You would declare some symbol with the weak attribute and if it became valid you would know that there was some FORTRAN somewhere in the current executable image. However, you could not know if it was calling you directly or simply due to some other library pulled in.

The right solution appears to be using explicit wrappers to call C from FORTRAN: Calling a FORTRAN subroutine from C

Community
  • 1
  • 1
Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
0

It would be better if you set a start index. Fortran arrays do not necessarily have to start at 1. They can start at any number. The array may have been declared as

float, dimension(-20:20):: neg
float, dimension(4:99) pos

So passing an index of 5 for neg would mean the 26th element and passing an index of 5 for pos would mean the 2nd element.

cup
  • 7,589
  • 4
  • 19
  • 42