CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

Linking fortran and c

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 27, 2005, 21:17
Default Linking fortran and c
  #1
vasanth
Guest
 
Posts: n/a
Hi,

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

vasanth
  Reply With Quote

Old   January 28, 2005, 01:44
Default Re: Linking fortran and c
  #2
xueying
Guest
 
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
  #3
vasanth
Guest
 
Posts: n/a
Hi

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
  #4
andy
Guest
 
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

Reply


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 Off
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 06: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 23:13
visual fortran Monica Main CFD Forum 1 August 28, 2004 21:45
Why Favoring Fortran over C/C++? Zi-Wei Chiou Main CFD Forum 35 September 26, 2001 10:34


All times are GMT -4. The time now is 22:31.