Calling C functions from fortran -


i'm trying call c functions using fortran (need project). first trying call non parametrized, void function via fortran.

kindly me resolve following errors in given code.

c code matrix multiplication:

#include <stdio.h>   extern "c"   { void __stdcall mat(); }   void mat() {  int m, n, p, q, c, d, k, sum = 0;    printf("enter number of rows , columns of first matrix\n");  scanf("%d%d", &m, &n);    int first[m][n];    printf("enter elements of first matrix\n");    (  c = 0 ; c < m ; c++ )    ( d = 0 ; d < n ; d++ )   scanf("%d", &first[c][d]);    printf("enter number of rows , columns of second matrix\n");   scanf("%d%d", &p, &q);    int second[p][q];   if ( n != p )    printf("matrices entered orders can't multiplied each other.\n");  else  {   printf("enter elements of second matrix\n");    ( c = 0 ; c < p ; c++ )    ( d = 0 ; d < q ; d++ )      scanf("%d", &second[c][d]);    int multiply[m][q];      ( c = 0 ; c < m ; c++ )  {    ( d = 0 ; d < q ; d++ )    {     ( k = 0 ; k < p ; k++ )     {       sum = sum + first[c][k]*second[k][d];     }      multiply[c][d] = sum;     sum = 0;   } }     printf("product of entered matrices:-\n");    ( c = 0 ; c < m ; c++ )   {     ( d = 0 ; d < q ; d++ )       printf("%d\t", multiply[c][d]);      printf("\n");   }  }   }    int main() {  mat(); return 0;  } 

also, code calling function mat fortran wrote :

  program mat_mult  !this main program.    call mat()   stop   end 

on executing c file following error:

matrix_mult.c:5: error: expected identifier or ‘(’ before string constant

on executing fortran file using f77 compiler, following error:

/tmp/ccqavekc.o: in function main__': matrix_mult.f:(.text+0x19): undefined reference tomat_' collect2: ld returned 1 exit status

kindly me in identifying error/correct code. thank you.

a few things first

1) i'm assuming you're using c99 compiler because c code you've written not work on c89 compiler.

2) extern "c" c++ programs: not c programs.

not sure compiler you're using. assuming you're using gcc , gfortran since looks you're on linux based system.

gcc adds leading _ symbols: mat becomes _mat. gfortran adds both leading , trailing _ symbols: mat becomes _mat _.

to make c , fortran talk

a) remove main function c code

b) remove extern "c" declaration. c++ declaration tells compiler routine mat should not have name mangling.

c) since don't have parameters, assume _cdecl , change void mat() void mat(). if have use stdcall, need compile --enable-stdcall-fixup. stdcall needed if fortran program has pass parameters c program different ball game. instead of _mat _, compiler generates mat@0

d) c routine like

#include <stdio.h> void mat_() {     ... }  /* no main */ 

e) since routine mat not declared in fortran code, compiler needs know external.

program mat_mult external mat call mat() stop end 

f) compile , link (say c program called mat.c , fortran program called matmul.f)

gcc -g -c mat.c gfortran -g matmul.f mat.o -o matmul 

there whole load of comments recommending use f2003 or f2008 if have been told have use f77, have use f77.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -