User Function declaration in native languages
This section describes how to declare a user function in C, Fortran and so on. The general shape of the declaration is shown. Not all the possible arguments will necessarily be used by any particular function, and the actual arguments required will depend on the way the function is declared to Xpress NonLinear.
User function declaration in C
The XPRS_CC calling convention (equivalent to __stdcall under Windows) must be used for the function. For example:
type XPRS_CC MyFunc(double *InputValues, int *FunctionInfo, char *InputNames, char *ReturnNames double *Deltas, double *ReturnArray);
where type is double or double* depending on the nature of the function.
In C++, the function should be declared as having a standard C-style linkage. For example, with Microsoft C++ under Windows:
extern "C" type _declspec(dllexport) XPRS_CC MyFunc(double *InputValues, int *FunctionInfo, char *InputNames, char *ReturnNames double *Deltas, double *ReturnArray);
If the function is placed in a library, the function name may need to be externalized. If the compiler adds "decoration" to the name of the function, the function may also need to be given an alias which is the original name. For example, with the Microsoft compiler, a definition file can be used, containing the following items:
EXPORTS MyFunc=_MyFunc@12
where the name after the equals sign is the original function name preceded by an underscore and followed by the @ sign and the number of bytes in the arguments. As all arguments in Xpress NonLinear external function calls are pointers, each argument represents 4 bytes on a 32-bit platform, and 8 bytes on a 64-bit platform.
A user function can be included in the executable program which calls Xpress NonLinear. In such a case, the user function is declared as usual, but the address of the program is provided using XSLPchguserfuncaddress or XSLPsetuserfuncaddress. The same technique can also be used when the function has been loaded by the main program and, again, its address is already known.
The InputNames and ReturnNames arrays, if used, contain a sequence of character strings which are the names, each terminated by a null character.
Any argument omitted from the declaration in Xpress NonLinear will be omitted from the function call.
Any argument declared in Xpress NonLinear as of type NULL will generally be passed as a null pointer to the program.