Certain Fortran compilers support automatic generation of C prototypes (see my open issue on compilers supporting generation of C function prototypes which unfortunately received no replies) for Fortran procedures (and interfaces) marked with bind(c)
. Presumably, this shouldn’t be too hard to implement in a compiler. I guess only few Fortran teams have reached the level of build automation (and influence) to lobby for this.
The other way round, from C functions to Fortran interfaces, is trickier. Personally I’d like to see it solved by the means of C++11 and soon C23 generalized attribute sequences. Using the attribute sequences the compiler could emit the correct Fortran interfaces (including implicit none
) when passed the right compiler flag (-fc-prototypes
).
The Rcpp package for seamless integration of C++ and R already uses attribute sequences to export C++ functions to R. Here’s one example:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double meanC(NumericVector x) {
int n = x.size();
double total = 0;
for(int i = 0; i < n; ++i) {
total += x[i];
}
return total / n;
}
Having this built in to the compiler seems like a more robust approach than what third-party tools including SWIG-Fortran and Shroud offer. I will note that these tools go one step further than just interoperable C interface, but are also able to wrap C++ classes and other non-interoperable constructs.
Unfortunately, I’m under the impressions that the Fortran community is too detached from the C and C++ communities to make this a reality. Perhaps my impression is wrong and there are people actively working on this, which would be of great comfort to me. In a few discussions with fellow C++ colleagues, they were pessimistic about this idea receiving any official C++ support (either symbolic from the committee or practical from vendors). Many programming language communities (this Fortran Discourse included) tend to adopt language-centric and exceptionalistic views. There have been dark periods of human history where language-speakers have been prejudiced, banned, and even eliminated. Perhaps the title of this thread would be served better by the word deprecate than eliminate. Interestingly, the use of deprecate to describe obsolescent technology is quite recent.