fortran and c mixing

fortran and c mixing

hello

  I created a c static library with this code:

fileopen.h:

extern FILE *fp;

r.h:

#ifdef HAVE_F77_UNDERSCORE
# define F77_CALL(x)    x ## _
#else
# define F77_CALL(x)    x
#endif
#define F77_NAME(x)    F77_CALL(x)
#define F77_SUB(x)     F77_CALL(x)
#define F77_COM(x)     F77_CALL(x)
#define F77_COMDECL(x) F77_CALL(x)

ex.c:

#include "fileopen.h"
#include "r.h"

static    int jacsng = -1;
static  int jacupd = -1;
static  double jacond = 0.0;

/*
 * output for a single incorrect jacobian entry
 */
 
 void F77_SUB(fileopenclose)()
 {
   fp=fopen("nleqslv.err", "w+");
 }
 
void F77_SUB(nwckot)(int *i, int *j, double *aij, double *wi)
{   
    fprintf(fp,"Chkjac  possible error in jacobian[%d,%d] = %20.13e\n"
            "                         Estimated[%d,%d] = %20.13e\n", *i, *j, *aij, *i, *j, *wi);

}

the library is called test.lib and is linked with this fortran code:

 MODULE INTER

     interface
     SUBROUTINE nwckot(i,j,a,b) BIND(C,name='nwckot')
     use, intrinsic :: ISO_C_BINDING     
     integer (C_INT), value :: i
     integer (C_INT) :: j
     REAL(C_DOUBLE) :: a
     REAL(C_DOUBLE) :: b
     end SUBROUTINE nwckot
     end interface

     interface
     subroutine fileopenclose() BIND(C,name='fileopenclose')
     use, intrinsic :: ISO_C_BINDING
     END subroutine fileopenclose
     end interface

     END MODULE INTER

SUBROUTINE user
USE INTER
INTEGER i,j
DOUBLE PRECISION a,b
CALL fileopenclose()
CALL nwckot(1,2,1.d0,1.d0)
END SUBROUTINE USER

This last sub is called in the main code that I don't show here.

when I run the app I get an access violation error(157) and I know that it is reated with the fprintf code. But why do I get that error?

I am just starting to mixing languages and this is just me trying to learn, step by step, because I have to build a huge app in fortran that uses a lot of c files that I have to turn into a static library to be called inside my code.

 

thanks in advance

Peter

 

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Your fortran declaration for the subroutine is:

 SUBROUTINE nwckot(i,j,a,b) BIND(C,name='nwckot')

	     use, intrinsic :: ISO_C_BINDING     

	     integer (C_INT), value :: i
...

Note the value attribute on the i argument.

Your C declaration is

void F77_SUB(nwckot)(int *i, int *j, double *aij, double *wi)

Note the pointer declaration on the i parameter.

Those two declarations are inconsistent. 

Either delete the VALUE attribute from the Fortran or (not and) delete the * that designates a pointer in the function's parameter list for that parameter and subsequently in the body of the function itself.

(I'd do the latter - you really don't need the i thing to be a pointer, but I'd also make the other attributes VALUE and delete the * for them too, because that is more natural style for a C function.  But you may have other requirements.)

 

   Thanks

       I removed the FORTRAN declaration of the i,j index. The double part remained as it was and did not produce any runtime error. Why does the double variables can be declared in that way without any runtime error if they are also pointers?

 

thanks in advance

They can't.

Post your modified code if you want to check or clarify further.

Login to leave a comment.