Custom Functions > Using Fortran > To Use Fortran in Your Custom Functions
To Use Fortran in Your Custom Functions
Create the Fortran Library
1. Create a new Fortran Static Library project in Compaq Visual Fortran 6.6B, and add your files to the project.
2. Under the Build menu, click Set Active Configuration, and select Release.
3. Compile and build. The Release subfolder of your Fortran project now contains [myfortranlib].lib.
Modify the C code for the Custom Function
When you have the Fortran library, you must add in your C code:
extern void __stdcall FORTRANFUNCTION(const double *array1, const double *scalar1 [, etc.]);
// Since FORTRAN typically expects arguments by reference, arguments are passed
// as pointers. To pass values, the FORTRAN code must contain a compiler directive
// telling the function to expect a value rather than an address.
LRESULT mcadfunction(LPCOMPLEXARRAY array1, LPCCOMPLEXSCALAR scalar1, etc.)

// this defines the function before FUNCTIONINFO, using the same variable names
// called by the external FORTRAN function.
// some error checking goes here, followed by the
// actual call to the FORTRAN function. For example,
FORTRANFUNCTION(&array1->hReal[0][0], &scalar1->real [, etc.]);

// Either the function call must be in UPPERCASE, or you will have to set
// Settings->FORTRAN->External procedures->External name implementation
// to "Upper case" in your FORTRAN compiler. Any other C functionality follows...
return 0;

Link the Library
1. In an existing C project in MS Visual C++ or Visual Studio, select the Linker tab and then Input from the Category menu. Enter [myfortranlib].lib in the Object/library modules textbox at the end of the list.
2. Enter libc.lib in the Ignore libraries text box.
3. Enter the path of the [myfortranlib].lib file in the Additional Library Path text box, for example, C:/temp.
4. Compile and build the DLL.
Additional Information
The Fortran code should be run as a separate thread, rather than directly as a function. This way, if the process is terminated, for example, by pressing Esc, the whole thread can be terminated without checking which Fortran subroutine is currently active.
It is useful to replace all STOP statements in the Fortran code with a special function returning an error code to the calling C which will terminate the thread gracefully.
If the Fortran code contains PRINT to console statements, these can be converted by reassigning console to a file, so output goes to a file instead of the screen. If the Fortran routine terminates with an error, the C++ front-end can be programmed to read this log file and display it as a window. For example:
c this subro which redirects FORTRAN output to file
use dfwin
integer res

c CALL close_stdout

c PRINT *, 'Print a character to initialize '
PRINT *,' '
If you wish to distribute a Fortran-based DLL, the computer which runs it must have the appropriate Fortran runtime libraries installed. You can use the following Fortran runtime libraries:
If these DLLs are missing, errors will result. The DLLs are packaged with your Fortran compiler. You must install them in the Windows/system32/ or WINNT/system32/ directory.
Was this helpful?