mardi 4 août 2015

Out of order print statements when using fortran program to call c subroutine

I am experiencing problems with displaying values on the console while invoking a C subroutine from a fortran subroutine. I have print statements immediately before and after the call to the C subroutine, as well as a print statement within the C subroutine. However, upon executing the program, the C statements are printed before the two Fortran statements, rather than between them. I’ve looked thoroughly within the code, and there are no calls to the C subroutine elsewhere, so there is no obvious reason why the c statement should be printed before the Fortran statement. I created a simpler Fortran-C program (below) to try to reproduce the problem, but the code executed the print statements in the expected order. I wonder if someone has any insights as to what could be the problem. Thank you.

Sample Fortran code:

  program test

     print *, 'Calling C program'

     call cprog()

     print *, 'Finished calling C program.'

   end program test

sample c code to be invoked from fortran:

void cprog()
{

  printf("THIS IS A TEST\n");

  return;
}

Output:

Calling C program

THIS IS A TEST

Finished calling C program.

Output similar to what I’m experiencing with more complex code:

THIS IS A TEST

Calling C program

Finished calling C program



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire