Interaction of the variational function of C and Fortran

Is there a way to declare a C variable and call it from Fortran? I need to call this function to compute some point products between vectors marked as a string. My idea was to declare something like the following, where the variable argument list contains string literals. If the variable argument list is empty, I would search among the standard labels and do the calculations. If the user specified two labels, I would extract these two vectors and get their point product:

extern "C" void compute_dot_product(double * dot_product, ...) { va_list args; va_start(args, NULL); char * label1 = va_arg(args, char *); if (!label1) { // Do standard label lookup and compute dot product } else { // Compute dot product between the vectors with the specified labels char * label2 = va_arg(args, char *); } va_end(args); } 

The only problem is that I can compile my C library and link it to the Fortran executable, but I get a runtime error when I try to access the list of variable arguments. Any idea if what I'm trying to do is possible? A possible solution would be to divide into two functions: one that performs a standard label search (argument 0 arguments), the other handles a non-standard label search (2 arguments). However, I would rather avoid this solution.

+6
source share
1 answer

It is not possible to call a variable function in a standard matching (i.e. portable) way.

You can make the definition of the C function with only two parameters (therefore, it is no longer a variable - existing references to the function must be changed), and the second parameter in the C function is a pointer, either NULL, or indicate that no additional things are passed in or pointed to an array of pointers (possibly NULL) or something else, with any additional things. In F201X, the interface body for this function can use the OPTIONAL attribute for the second argument.

+3
source

Source: https://habr.com/ru/post/956997/


All Articles