CFD Online Logo CFD Online URL
Home > Forums > Main CFD Forum

Linking fortran and c

Register Blogs Members List Search Today's Posts Mark Forums Read

LinkBack Thread Tools Display Modes
Old   January 27, 2005, 21:17
Default Linking fortran and c
Posts: n/a

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

  Reply With Quote

Old   January 28, 2005, 01:44
Default Re: Linking fortran and c
Posts: n/a
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; }
  Reply With Quote

Old   January 29, 2005, 12:35
Default Re: Linking fortran and c
Posts: n/a

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
  Reply With Quote

Old   January 29, 2005, 13:38
Default Re: Linking fortran and c
Posts: n/a
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.
  Reply With Quote


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Fortran Compiler-CFX12.1 Araz CFX 13 March 27, 2017 05:37
Linking issues User Fortran Anil CFX 3 February 24, 2017 22:48
Intrinsic Procedure 'ISNAN' in GNU FORTRAN 77 hawk Main CFD Forum 1 April 12, 2005 22:13
visual fortran Monica Main CFD Forum 1 August 28, 2004 20:45
Why Favoring Fortran over C/C++? Zi-Wei Chiou Main CFD Forum 35 September 26, 2001 09:34

All times are GMT -4. The time now is 04:13.