CFD Online Discussion Forums

CFD Online Discussion Forums (
-   Main CFD Forum (
-   -   Linking fortran and c (

vasanth January 27, 2005 21:17

Linking fortran and c

I have a fortran code and I am trying to call a C

subroutine from the fortran code.Can anybody help me

with all the steps that I have to follow to achieve

this and all the factors I need to consider.

Thanks in advance


xueying January 28, 2005 01:44

Re: Linking fortran and c
In C subroutine, put underscore _ after the subroutine name; compile C subroutine by C compiler; compile Fortran code by Fortran compiler; and then link .o files by Fortran compiler.

Code example:

In Fortran code, there's one line calling C subroutine as following:

call ewdcwrite(1,xyz,3*NODES,8)

And the C subroutine is shown as following:

#include <malloc.h> #include <stdio.h> #include <errno.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <time.h> void ewdcwrite_(int *fd, char *array, int *nelem, int *elsize) {

int nbytes, ntest,gener;

register int size, sizem, i, j;

char *bytea, *byteb, *arrayswab;

if (*fd == 0) return; nbytes = (*nelem) * (*elsize);

arrayswab = malloc(nbytes);

size = *elsize; sizem = size - 1;

bytea = malloc(size); byteb = malloc(size);

for (i = 0; i < *nelem; i++) {

memcpy((void *)bytea, (void *)(array+i*size), size);

for (j = 0; j < size; j++) byteb[j] = bytea[sizem - j];

memcpy((void *)(arrayswab+i*size), (void *)byteb, size); }

gener = *nelem* (*elsize);

for (i = 0; i < gener;i++) array[i] = arrayswab[i];

return; }

vasanth January 29, 2005 12:35

Re: Linking fortran and c

Thank you very much.But I have one problem.If the alphabets in the name of the c routine are in upper case then fortran is unable to detect the routine. I dont know what to do for this, because my fortran application uses a subroutine in C has a name which has upper as well a lower case alphabets. Can anybody help me with this.

Thanks in advance vasanth

andy January 29, 2005 13:38

Re: Linking fortran and c
Your problem would appear to be that you have not declared the C function to be an external C function in your Fortran code. Read the Fortran manual to find out how to do this because it varies from compiler to compiler.

All times are GMT -4. The time now is 19:39.