* Problem calling a C function from Fortran
@ 1988-08-19 17:54 Nathan Okinaka
1988-08-21 16:35 ` Larry Jones
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Nathan Okinaka @ 1988-08-19 17:54 UTC (permalink / raw)
I'd appreciate if anybody out there could help me with a problem I'm having
calling a function written in C from a Fortran program on the VAX/VMS.
I'm passing two arguments to a C function called "test". The first one is
an integer*4 varaible and the second one is an external function. The first
argument gets passed ok but the external function "dbl" does not seem to get
passed ok.
The C function requires the address of where the function "dbl" starts so
I'm passing the value of the address of the function "dbl" to the C function
"test" but it does not seem to work. I'm wondering what Fortran does when you
try and get %loc(dbl). Is this really the address of where this function
starts? What am I doing wrong? Any help would be appreciatted. This is a
test program I'm trying out which I need to use in another bigger program.
Thanks,
Nate Okinaka (206)773-2687
The following is the error message I get when I run the program:
-----------------------------------------------------------------------------
[okinaka]> r ext
%loc(dbl) = 4848
%loc(dbl) in hex = 12F0
iangle = 90
iangle in hex = 5A
This is a test of external function
x = 90, f = 4848 (decial)
x = 5a, f = 12f0 (hex)
%SYSTEM-F-ACCVIO, access violation, reason mask=00, virtual address=0000005A,
PC=000012F9, PSL=03C00020
%TRACE-F-TRACEBACK, symbolic stack dump follows
module name routine name line rel PC abs PC
DBL DBL 5 00000009 000012F9
TEST test 9 00000039 000013AD
EXT$MAIN EXT$MAIN 17 000000AA 000012AA
-----------------------------------------------------------------------------
The following lists the Fortran program and the C function that I'm calling:
C------------------------------------------------------------------------
C Main Fortran Program
C------------------------------------------------------------------------
implicit none
external dbl
integer*4 func_adr
integer*4 test, temp
integer*4 dbl
integer*4 iangle, istat
iangle = 90
func_adr = %loc(dbl)
type *,'%loc(dbl) =',func_adr
write(5,100)func_adr
100 format(' %loc(dbl) in hex =',z8)
type *,'iangle =',iangle
write(5,110)iangle
110 format(' iangle in hex =',z8)
istat = test(%val(iangle), %val(func_adr))
type *,' '
type *,'istat=',istat
end
integer*4 function dbl(x)
implicit none
integer*4 x
dbl = (2 * x)
type *,' '
type *,'dbl> x=',x,' dbl=',dbl
type *,' '
return
end
/*---------------------------------------------------------------------------*/
/* C Funtion test */
/*---------------------------------------------------------------------------*/
int test(x,f)
int x;
int (*f)();
{
int y;
printf("\nThis is a test of external function\n");
printf("x = %d, f = %d (decial)\n",x,f);
printf("x = %x, f = %x (hex)\n",x,f);
y = (*f)(x);
printf("y = %d\n",y);
return(y);
}
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Problem calling a C function from Fortran
1988-08-19 17:54 Problem calling a C function from Fortran Nathan Okinaka
@ 1988-08-21 16:35 ` Larry Jones
1988-08-22 20:37 ` String passing (was :Re: " Rajiv Agrawal
1988-08-22 3:47 ` Gil Kloepfer Jr.
1988-08-22 20:55 ` Steven J Kassarjian
2 siblings, 1 reply; 6+ messages in thread
From: Larry Jones @ 1988-08-21 16:35 UTC (permalink / raw)
In article <1134@ssc-bee.ssc-vax.UUCP>, okinaka@ssc-vax.UUCP (Nathan Okinaka) writes:
< I'd appreciate if anybody out there could help me with a problem I'm having
< calling a function written in C from a Fortran program on the VAX/VMS.
Well, the problem is actually calling a FORTRAN subroutine from C, but read on.
< I'm passing two arguments to a C function called "test". The first one is
< an integer*4 varaible and the second one is an external function. The first
< argument gets passed ok but the external function "dbl" does not seem to get
< passed ok.
Sure it does, you just called it wrong.
< The C function requires the address of where the function "dbl" starts so
< I'm passing the value of the address of the function "dbl" to the C function
< "test" but it does not seem to work. I'm wondering what Fortran does when you
< try and get %loc(dbl). Is this really the address of where this function
< starts? What am I doing wrong? Any help would be appreciatted. This is a
< test program I'm trying out which I need to use in another bigger program.
<
< Thanks,
< Nate Okinaka (206)773-2687
<
< The following is the error message I get when I run the program:
< -----------------------------------------------------------------------------
< [okinaka]> r ext
< %loc(dbl) = 4848
< %loc(dbl) in hex = 12F0
< iangle = 90
< iangle in hex = 5A
< This is a test of external function
< x = 90, f = 4848 (decial)
< x = 5a, f = 12f0 (hex)
< %SYSTEM-F-ACCVIO, access violation, reason mask=00, virtual address=0000005A,
< PC=000012F9, PSL=03C00020
< %TRACE-F-TRACEBACK, symbolic stack dump follows
< module name routine name line rel PC abs PC
< DBL DBL 5 00000009 000012F9
< TEST test 9 00000039 000013AD
< EXT$MAIN EXT$MAIN 17 000000AA 000012AA
Well, the traceback shows that your FORTRAN main program did call your C
function which then called the FORTRAN subroutine so that's not the problem.
The problem is that when the C function calls the FORTRAN subroutine, it passes
the VALUE of the argument instead of the ADDRESS. When the FORTRAN subroutine
tried to access the variable, it used the value as an address and tried to
get the value stored there and that's what caused the access violation.
Just pass the address instead and everything will work fine.
----
Larry Jones UUCP: uunet!sdrc!scjones
SDRC scjones@sdrc
2000 Eastman Dr. BIX: ltl
Milford, OH 45150 AT&T: (513) 576-2070
Nancy Reagan on superconductivity: "Just say mho."
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Problem calling a C function from Fortran
1988-08-19 17:54 Problem calling a C function from Fortran Nathan Okinaka
1988-08-21 16:35 ` Larry Jones
@ 1988-08-22 3:47 ` Gil Kloepfer Jr.
1988-08-22 20:55 ` Steven J Kassarjian
2 siblings, 0 replies; 6+ messages in thread
From: Gil Kloepfer Jr. @ 1988-08-22 3:47 UTC (permalink / raw)
In article <1134@ssc-bee.ssc-vax.UUCP> okinaka@ssc-vax.UUCP (Nathan Okinaka) writes:
|>
|>I'd appreciate if anybody out there could help me with a problem I'm having
|>calling a function written in C from a Fortran program on the VAX/VMS.
|>
[Description deleted]
|>Thanks,
|>Nate Okinaka (206)773-2687
|>
|>The following is the error message I get when I run the program:
|>-----------------------------------------------------------------------------
|>%SYSTEM-F-ACCVIO, access violation, reason mask=00, virtual address=0000005A,
|>PC=000012F9, PSL=03C00020
|>%TRACE-F-TRACEBACK, symbolic stack dump follows
|>module name routine name line rel PC abs PC
|>DBL DBL 5 00000009 000012F9
|>TEST test 9 00000039 000013AD
|>EXT$MAIN EXT$MAIN 17 000000AA 000012AA
[Programs deleted]
Nate,
The fact that your program aborted in DBL shows that DBL *is* actually
called (and it is, from looking at your programs). The problem is in the
way you pass the integer*4 number to the C program. Here is a little code
segment to show how to properly pass integer variables between fortran and
C on VMS:
Fortran code:
PROGRAM TEST
INTEGER*4 FVAR
CALL CROUTINE(FVAR)
CALL EXIT
END
C code:
croutine(fvar)
long *fvar; /* note: fortran passes by reference */
{
long cvar;
cvar = *fvar + 10; /* correct referencing of fvar */
f77routine(&cvar); /* must pass cvar to f77 by reference */
/* which is a C pointer */
}
Fortran code:
SUBROUTINE F77ROUTINE(CVAR)
INTEGER*4 CVAR
WRITE(6,*) 'CVAR = ',CVAR
RETURN
END
Your passing of the function with %LOC was perfect, but the way you passed
your integer variables seemed a bit obscure. I have been using C and
FORTRAN this way with success, so this should help.
Hope it did help...
+------------------------------------+----------------------------------------+
| Gil Kloepfer, Jr. | Net-Address: |
| ICUS Software Systems | {boulder,talcott}!icus!limbic!gil |
| P.O. Box 1 | Voice-net: (516) 968-6860 |
| Islip Terrace, New York 11752 | Othernet: gil@limbic.UUCP |
+------------------------------------+----------------------------------------+
^ permalink raw reply [flat|nested] 6+ messages in thread
* String passing (was :Re: Problem calling a C function from Fortran
1988-08-21 16:35 ` Larry Jones
@ 1988-08-22 20:37 ` Rajiv Agrawal
1988-08-30 13:15 ` rbt
0 siblings, 1 reply; 6+ messages in thread
From: Rajiv Agrawal @ 1988-08-22 20:37 UTC (permalink / raw)
While on the subject of C and FORTRAN on VMS ... How does one pass
strings between the two compilers??? We have VAX FORTRAN and GnuC
compilers. I just cannot pass any strings between a C routine and a
FORTRAN routine.
Thanks in advance for any help.
Rajiv.
-=-
------------------------------------------------------------------------------
206 W 18th Ave INTERNET: rajiv@heron.cis.ohio-state.edu
Columbus, OH 43210 agrawal-r@eng.ohio-state.edu
(614) 292-9029 BITNET: ts5600@ohstvma CCnet: 6177::agrawal
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Problem calling a C function from Fortran
1988-08-19 17:54 Problem calling a C function from Fortran Nathan Okinaka
1988-08-21 16:35 ` Larry Jones
1988-08-22 3:47 ` Gil Kloepfer Jr.
@ 1988-08-22 20:55 ` Steven J Kassarjian
2 siblings, 0 replies; 6+ messages in thread
From: Steven J Kassarjian @ 1988-08-22 20:55 UTC (permalink / raw)
In general, what happens when the caller and the subroutine
disagree as to the argument list of the subroutine.
Specifically, the main body of the program called the
subroutine like this
...
CALL CONFUS (A, B, C)
...
while the subroutine was defined this way
SUBROUTINE CONFUS (A, B)
....
What happened was that C was always zero, though I think that
this is due to the compiler initializing C to zero.
I am interested in what would (and probably did) happen, with
all the nitty-gritty (and dirty) details. :-)
Also, what would happen in the reverse case (where the caller
doesn't supply an argument)?
Thanks (in advance) for the info.
steve.
p.s. how do you start a new discussion, with a new title, etc.?
steve kassarjian
+-----------------------------------------------------------------+
| All opinions are Eunichs: the operating |
| local and undefined system for |
| outside of unit. real men. |
+-----------------------------------------------------------------+
stejk@caen.engin.umich.edu
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: String passing (was :Re: Problem calling a C function from Fortran
1988-08-22 20:37 ` String passing (was :Re: " Rajiv Agrawal
@ 1988-08-30 13:15 ` rbt
0 siblings, 0 replies; 6+ messages in thread
From: rbt @ 1988-08-30 13:15 UTC (permalink / raw)
In article <20518@tut.cis.ohio-state.edu> rajiv@tuna.cis.ohio-state.edu (Rajiv Agrawal) writes:
>While on the subject of C and FORTRAN on VMS ... How does one pass
>strings between the two compilers??? We have VAX FORTRAN and GnuC
>compilers. I just cannot pass any strings between a C routine and a
>FORTRAN routine.
The problem is that FORTRAN programs use the CLASS_S descriptor mechanism while
C modules have zero terminated strings by default.
The best way to solve your problem is described in chapter "Using VAX C Features
on VMS" of "Guide to VAX C" manual. It describes how function parameters must
be declared to be conformant to the CLASS_S descriptor class.
The only problem in doing this is that ALL the modules calling the C routine
MUST have their parameters defined as CLASS_C descriptor, otherwise...
Roberto Divia`
=============
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~1988-08-30 13:15 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1988-08-19 17:54 Problem calling a C function from Fortran Nathan Okinaka
1988-08-21 16:35 ` Larry Jones
1988-08-22 20:37 ` String passing (was :Re: " Rajiv Agrawal
1988-08-30 13:15 ` rbt
1988-08-22 3:47 ` Gil Kloepfer Jr.
1988-08-22 20:55 ` Steven J Kassarjian
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox