|
[Sponsors] |
January 27, 2005, 21:17 |
Linking fortran and c
|
#1 |
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 |
|
January 28, 2005, 01:44 |
Re: Linking fortran and c
|
#2 |
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; } |
|
January 29, 2005, 12:35 |
Re: Linking fortran and c
|
#3 |
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 |
|
January 29, 2005, 13:38 |
Re: Linking fortran and c
|
#4 |
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.
|
|
|
|
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 |