Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling .c from fortran ( exceptional)

Status
Not open for further replies.

Da1cj

Programmer
Sep 30, 2003
4
DE
I basically want to call the following struct in .c from fortran as follows
...
struct AZNames{
int Number;
char *Name;
char *Unit;
int Typ;
};

static struct AZName AZLNames[] = {
{1, "Interaction G(%d)" , "Sa", 1}
{2, "Object H(%d)" , "Da", 3}
{3, "Action G(%d)" , "Fa", 2}
}

... what I really want to achieve is a state in fortran in which if I give in a randomlly selected number i.e 1, 2 or 3 ..., I recieve the corresponding Name, unit and Typ as stated in the struct.


... I have tried out a couple of methods calling this struct from fortran but still pretty much not close to my aim.

:)
 
I don't understand what you mean by "call the structure in Fortran". Do you mean you want to call a Fortran routine and pass the structure as an argument? I think it would be much easier to extract the data from the structure in a C routine either before you pass it to the Fortran routine or in another C routine that can be called from Fortran.

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
1) Which version of Fortran? F77 doesn't support structures. F90 and F95 support structures but their storage may be different: especially for char*.
2) What platform are you running this on?
 
Yes that's exactly what I mean, as in I need the data present in .c (in form of a struct) for use in fortran

...Such that when implemented I have the corrsponding values (Name Unit and Type ) to each Number I give in.

I work with a Visual F90, the implemented data should be tranfered to a Matlab format which isn't the basic challenge.


CaKiwi I'll be glad if you ellaborate more onb your tip.
 
The big problem here is calling conventions. In Fortran, the number of parameters is known so the called routine does the unstacking of the parameters on return. In C, the number of parameters is variable so the caller does the unstacking. Microsoft C used to have a keyword "Pascal" which did Fortran style unstacking but I don't know if you have access to the C code or not.

Also, in C, everything is called by value. In Fortran, they are called by location (similar to reference but not quite the same thing). C has a simulated call by location by passing the pointer to the value.

You'd probably have to write some noddy programs and disassemble them to see how the stacking is done, then write an assembler interface to hook both of them up.
 
Here's a test program which shows a possible solution

struct AZNames{
int Number;
char *Name;
char *Unit;
int Typ;
};

static struct AZNames AZLNames[] = {
{1, "Interaction G(%d)" , "Sa", 1},
{2, "Object H(%d)" , "Da", 3},
{3, "Action G(%d)" , "Fa", 2}
};
main()
{
f1 (AZLNames);
return;
}

void extract (struct AZNames* a, int* ix, int* Number, char* Name, char* Unit, i
nt* Typ)
{
*Number = a[*ix-1].Number;
strcpy(Name, a[*ix-1].Name);
strcpy(Unit, a[*ix-1].Unit);
*Typ = a[*ix-1].Typ;
}
C ------ Fortan subroutine ------
subroutine f1(a)
character*80 a

character*80 name, un
integer*4 number, typ, ix

ix = 2
call extract(a, ix, number, name, un, typ)
print *, number, name, un, typ
return
end

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
I found the tip from Cakiwi very helpfull and that from xwb was a very good point;

However I kept recieving the statements ...

unresolved external _MAIN
unresolved external _EXTRACT@36

when I tried to build an .exe

The fortran Programm I wrote was however without Errors after compiling.
 
You are linking with only the fortran object file. You must link both the fortran and C object files together. You probably need to include both files in a VF90 project do a build.

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
If you are using a VF90 project, do the following:

click project -> settings
click fortran tab
under catergory, select external procedures
under arg passing, select C by reference
under external name, select lower case

You may also need to change the string length argument passing to "after all args"

Also change the fortran declaration of the the argument a to integer instead of character*80


CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
Thanks for the Tip, Cakiwi ... Got to get rid of the first error, however I have not been able to get rid of the error EXTRACT@36 .

I basically tired to use an INTERFACE statement to kind of get rid of this error, however I guess there's something wrong with the statement
!DEC$ ATTRIBUTES C, ALIAS: 'My SUB' :: MY SUB

I'll keep working to get rid of this, I don't think I'm too far away from the desired result.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top