Using g95 with Scilab
Loading dlls with 'link()'
Scilab (www.scilab.org) is a free numerical computation package similar to Matlab. This page explains how to access Fortran procedures in Scilab 4.0.
In Scilab Help, under the 'fort' keyword, the following Fortran subroutine is provided as an example:
The working environment for the following commands is a Msys shell in Windows. Save the code as foof.f90, and compile it with g95:
$ g95 -c foof.f90
To see how the subroutine name has been "decorated", type:
$ dlltool -z foof.def --export-all-symbols foof.o
;c:\mingw\bin\dlltool.exe -z foof.def --export-all-symbols foof.o
Note that a trailing underscore was added to the subroutine name. The subroutine entry point is "foof_". Scilab expects external procedure names to have a trailing underscore, and g95 adds these by default to non-module procedure names.
In Linux, Scilab 'link()' can handle object files, but on Windows systems, the object files need to be compiled as dlls in order to be used in Scilab. That is done with:
$ g95 -shared -mrtd -o foof.dll foof.o foof.def
Using a .def file is optional, but it was used here to illustrate how Fortran subroutines work in Scilab. The dll should be in the Scilab working directory.
Results in Scilab
When module procedures are compiled with g95, trailing underscores are not added to procedure names. Some method of attaching trailing underscores to procedure names is required to get module procedures to work with g95. Several ways to do it and access module procedures in Scilab are outlined below.
With bind(c), we can assign any name to a Fortran procedure. This provides an elegant, and standard conforming way to attach trailing underscores to the names of procedures that are loaded by Scilab.
With the bind(c) line added to the code, a module procedure can be loaded in Scilab and used in the same way as a non-module subroutine. The Scilab results for this example are the same as above.
Save this code as 'foofm.f90'.
Compile the module:
$ g95 -c foofm.f90
Next we use dlltool to create a .def file:
$ dlltool -z foofm.def --export-all-symbols foofm.o
In the .def file we can control how symbols will be identified in a dll. Open the .def file in a text editor or do:
$ cat foofm.def
Notice that g95 has added the prefix "foo_MP_" to the procedure name.
Now open the file foofm.def in a text editor and edit as follows to create an alias for the module procedure name, with a trailing underscore:
When compiling the dll, include the new, edited version of foofm.def in the command line:
$ g95 -shared -mrtd -o foofm.dll foofm.o foofm.def
Defining an alias to a module procedure name in a .def file, the alias having a trailing underscore, and then compiling as a dll with g95, allows module procedures to be loaded in Scilab.
Another option is to add trailing undercores to procedure names in the Fortran source code. Compile the code to a dll, and call the procedure in Scilab with a name constructed from <module-name>_MP_<procedure-name>, minus the trailing underscore. The example code would be:
Compile the module as before and access the subroutine in Scilab using the name "foo_MP_foof". This is another, perhaps less elegant way to access module procedures compiled with g95 from within Scilab.
Interfacing with Fortran in Scilab
In the example below, another subroutine has been added to module 'foo', which is compiled as a dll with g95 and loaded in Scilab. The second subroutine outputs the real array c, (c1 in Scilab), along with the arguments 's' and 'ch'. The 's' argument is a real scalar and 'ch' has type character(len=4).
Save the above code as 'foo.f90'. Compile with:
g95 -c foo.f90