Fortran programs not writing stdout on windows

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Fortran programs not writing stdout on windows

Jeroen Ooms-2
A user has reported an issue that appears when a fortran executable is
called via R on Windows. I am unsure if this expected behavior or a
bug in Fortran or in how R calls Windows executables.

The problem is that when the fortran program is called from R, stdout
gets written to a file "fort.6" instead of stdout. When the same
executable is called from the terminal it works fine and prints to
stdout. This unexpected behavior is unfortunate for R wrappers that
rely on captured output.

A minimal example is available from github [1] and can be installed with

   devtools::install_github("jeroen/ftest")
   ftest::hello()

When running ftest::hello() on linux, R will properly capture output.
However on Windows it will return an empty string, and a file 'fort.6'
gets created in the working directory instead.

The executables can be found in: system.file("bin", package = "ftest")

Interestingly if we open a command line terminal and run the same
executable it prints output to stdout. So perhaps it has to do with
the way R invokes CreateProcess() on Windows?

[1] https://github.com/jeroen/ftest

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Fortran programs not writing stdout on windows

Tomas Kalibera
For reference, this has been fixed in R-devel, 74168. The problem only
exists on Windows with RGui.

As a workaround for older versions of R, one can unset environment
variables "GFORTRAN_STDOUT_UNIT" and "GFORTRAN_STDERR_UNIT" for the
duration of system/system2 calls that invoke external Fortran programs.

Please note that while external code, which is run via system/system2,
can be implemented in Fortran and use Fortran I/O to write to standard
error and standard output, code that is linked against R is not allowed
to do that. Mixing C and Fortran I/O is dangerous due to potentially
incompatible runtimes. On Windows, with RGui, there is an additional
problem that the output needs to go to the RGui console. So all output,
at the lowest level, has to go through C functions Rprintf/REprintf.
More details are in Writing R Extensions (Printing, Printing from FORTRAN).

Tomas

On 06/20/2017 12:52 PM, Jeroen Ooms wrote:

> A user has reported an issue that appears when a fortran executable is
> called via R on Windows. I am unsure if this expected behavior or a
> bug in Fortran or in how R calls Windows executables.
>
> The problem is that when the fortran program is called from R, stdout
> gets written to a file "fort.6" instead of stdout. When the same
> executable is called from the terminal it works fine and prints to
> stdout. This unexpected behavior is unfortunate for R wrappers that
> rely on captured output.
>
> A minimal example is available from github [1] and can be installed with
>
>     devtools::install_github("jeroen/ftest")
>     ftest::hello()
>
> When running ftest::hello() on linux, R will properly capture output.
> However on Windows it will return an empty string, and a file 'fort.6'
> gets created in the working directory instead.
>
> The executables can be found in: system.file("bin", package = "ftest")
>
> Interestingly if we open a command line terminal and run the same
> executable it prints output to stdout. So perhaps it has to do with
> the way R invokes CreateProcess() on Windows?
>
> [1] https://github.com/jeroen/ftest
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel