3

The premise: I'm writing a plug-in DLL which conforms to an industry standard interface / function signature. This will be used in at least two different software packages used internally at my company, both of which have some example skeleton code or empty shells of this particular interface. One vendor authors their example in C/C++, the other in Fortran.

Ideally I'd like to just have to write and maintain this library code in one language and not duplicate it (especially as I'm only just now getting some comfort level in various flavors of C, but haven't touched Fortran).

I've emailed off to both our vendors to see if there's anything specific their solvers need when they import this DLL, but this has made me curious at a more fundamental level. If I compile a DLL with an exposed method void foo(int bar) in both C and Fortran... by the time it's down to x86 machine instructions - does it make any difference in how that method is called by program "X"? I've gathered so far that if I were to do C++ I'd need the extern "C" bit to avoid "mangling" - there anything else I should be aware of?

Tom S
  • 135
  • 7

3 Answers3

3

It matters. The exported function must use a specific calling convention, there are several incompatible ones in common use in 32-bit code. The calling convention dictates where the function arguments are stored, in what order they are passed and how they are removed again. As well as how the function return value is passed back.

And the name of the function matters, exported function names are often decorated with extra characters. Which is what extern "C" is all about, it suppresses the name mangling that a C++ compiler uses to prevent overloaded functions from having the same exported name. So the name is one that the linker for a C compiler can recognize.

The way a C compiler makes function calls is pretty much the standard if you interop with code written in other languages. Any modern Fortran compiler will support declarations to make them compatible with a C program. And surely this is something that's already used by whatever software vendor you are working with that provides an add-on that was written in Fortran. And the other way around, as long as you provide functions that can be used by a C compiler then the Fortran programmer has a good chance at being able to call it.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Very useful information, thank you. Not having much formal CS background, this stuff at the "guts" level is easy for me to overlook and be unaware of! – Tom S Jan 11 '14 at 16:19
  • As a supplement to this, I am now seeing that different calling conventions can be specified in C++ by using __stdcall, __cdecl, etc (the former being appropriate for use with Fortran). – Tom S Jan 13 '14 at 15:13
  • 1
    You'll find more (generic) details in [this answer](http://stackoverflow.com/a/15664100/17034). – Hans Passant Jan 13 '14 at 15:18
2

Yes it has been discussed here many many times. Study answers and questions in this tag https://stackoverflow.com/questions/tagged/fortran-iso-c-binding .

The equivalent of extern "C" in fortran is bind(C). The equivalency of the datatypes is done using the intrinsic module iso_c_binding.

Also be sure to use the same calling conventions. If you do not specify anything manually, the default is usually the same for both. On Linux this is non-issue.

Community
  • 1
  • 1
0

extern "C" is used in C++ code. So if you DLL is written in C++, you mustn't pass any C++ objects (classes).

If you stick with C types, you need to make sure the function passes parameters in a single way e.g. use C's default of _cdecl. Not sure what Fortran uses.

egur
  • 7,830
  • 2
  • 27
  • 47