I have run across a problem building xplor-nih with the g95
compiler from www.g95.org from which I understand gfortran is
derived. Xplor-nih is a mix of c, c++ and fortran code. The
main calling program a c shell which call the fortran subroutines.
These fortran subroutines in turn can call the c++ code through
a c interface call. The problem I am having is that when one
redirects the output from the xplor-nih program to a file, the
buffers from the different langauges purge at different times
so that output doesn't occur sequentially as it does to a terminal.
For example, I can demonstrate this with the following code...
hello.f
----------------------------------------------------------------------
SUBROUTINE HELLO
INTEGER CPLUS_OUTPUT
EXTERNAL CPLUS_OUTPUT
INTEGER SHELL
WRITE(6,'(A)') ' before output from c++ code'
SHELL=CPLUS_OUTPUT()
WRITE(6,'(A)') ' after output from c++ code'
RETURN
END
------------------------------------------------------------------------
cplus_output.cc
------------------------------------------------------------------------
#include <iostream.h>
#define FORTRAN(x) (x ## _)
extern "C" void
FORTRAN(cplus_output)()
{
for (int i=0 ; i<10000 ; i++)
cout << "Ouput from c++ of line number: " << i << "\n";
}
-------------------------------------------------------------------------
main.c
-------------------------------------------------------------------------
#include <unistd.h>
#define FORTRAN(x) (x ## _)
void g95_runtime_stop(void);
void FORTRAN(hello)();
main()
{
FORTRAN(hello)();
g95_runtime_stop();
return 0;
}
--------------------------------------------------------------------------
If I build the above...
g95 -o hello.o -fno-second-underscore -c hello.f
g++ -o cplus_output.o -c cplus_output.cc
gcc main.c hello.o cplus_output.o ./libf95.a libf95.a
and then execute it...
./a.out > outputfile
I find that instead of having the c++ output in front and after the
fortran output, the c++ output randomly appears in the fortran output.
Andy Vaught tells me...
> If you're outputting from both the fortran and c++ side, the result is
>not determinate. It will depend on when the fortran and C libraries
>decide to flush output. That would very easily account for interleaved
>lines.
However xplor-nih has been built on at least seven different operating
systems with a number of different compilers (gnu, Intel, IBM, Portland,
SGI and Sun) and none of these have suffered this lack of synchronization
in the mixed language i/o. Can anyone clarify what the standard should
really be and if g95's i/o buffering is simply broken at the moment?
Jack