- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you both. This answers my question.
![](/skins/images/7FC17B7B85029576C25F1E43CE255B51/responsive_peak/images/icon_anonymous_message.png)
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page