Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Optional arguments in dlls

Chris_G_2
Beginner
1,389 Views

Are there any problems in using optional arguments when calling a dll?

I have a Fortran dll called from a program written in C++. I need to add some additional input dummy variables for new options in the Fortran dll, but for backwards compatibility I would like them to be optional. Will this cause problems?

0 Kudos
3 Replies
Steve_Lionel
Honored Contributor III
1,389 Views

The DLL aspect is not relevant - the same would apply for a static library.

The problem here is that there is no count of arguments passed, so if you add arguments to your Fortran routine, it will just see "garbage" in those positions when called from C++ where the caller didn't pass nulls for those arguments.

Fortran does let you make non-VALUE dummy arguments OPTIONAL for a routine with the BIND(C) attribute, but I don't think that will help you here. The solution is to create a new routine with the OPTIONAL arguments, and make the old one a jacket that calls the new one, omitting the optional arguments. You must use the PRESENT intrinsic to test whether an OPTIONAL argument was supplied, and must make an explicit interface visible to any Fortran caller.

If, however, the new arguments will either be always supplied or never supplied, you don't need to make them OPTIONAL. You will still need a new routine and a jacket for the old name, but the new arguments can be passed any way you want. Don't forget the explicit interface from the old to the new, though.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,389 Views

In Fortran follow Steve's advise for Optional arguments and on the C side, declare the function prototype with the optional arguments initialized to NULL:

extern "C" void foo(int N, float* A, int* OptionalArg1=NULL, float* OptionalArg2=NULL);

In this manner, you will minimize the edits to your C side interface. You could also declare a shell function with different signature:

extern "C" void foo(int N, float* A, int* OptionalArg1, float* OptionalArg2);
inline void foo(int N, float* A) { foo(N, A, NULL, NULL); } // supply the NULL

Jim Dempsey

0 Kudos
Chris_G_2
Beginner
1,389 Views

Thank you both. This answers my question.

0 Kudos
Reply