I need to call a fortran function from C, I have created a fortran.o file for the object call.
I defined the fortran function in C as extern double function();
I use a makefile to compile a bunch of files stored in various directories.
This makefile creates .o files for individual files in their directories and then updates a .a file.
Let’s say my fortran file is in Xyz directory. This directory has xyz.c, xyz1.c and fortran.o files.
Is there a way I can create the fortran.o file and link to the .c file and then update the .a file?
Thank you
Btw: I think page 248 in the users manual has a typo. it should be $ pgcc -c cmain.c instead of cmain.f
Is this fortran file will be a part of library? If so, you can add it to your make file. It should update .a file automatically. When you link this library to your program, if C is a main program, you may need to link with either -pgf77libs or -pgf90libs (depends on which compiler you use).
For more info on how to set up to archive library in makefile on linux type:
prompt% info make
and look for Using ‘make’ to Update Archive Files section.
Thank you for your input on the user manual.
Hongyon
So, I tried doing what you suggested (I think)
My make file calls the following make_all file in the directory. This make_all file updates the archive file with the files in the directory
Is it on win32, win64, or linux? What C compiler do you use to compile your C codes? I ask because there might be some naming or calling convention issue.
Below is an example for linux:
% pgf90 -c f.f
% pgcc main.c f.o -pgf90libs
What is it you are trying to do with adh and grad? As you know, the data layout in Fortran and C are not the same(column-wise vs. row-wise). It may not work if you try to do some fancy references. Please look at our PGI Fortran Language Reference for more information.
main () {
a.n=7;
a.f=2.3;
a.d=1.901;
printme();
}
PGI$ cat f.f
subroutine printme()
!DEC$ ATTRIBUTES ALIAS:‘_a’::a
common /a/n,f,d
integer n
real4 f
real8 d
print *, n,f,d
end
Ok, so the compile works and messages are passed to Fortran. However, when I try to pass 2 D arrays fortran does not get the values. Are 2D arrays passed differently from 1D arrays?
In C I have the arrays defined as float **bp and in fortran they are
dimension bp(4,10)
and the fortran subroutine is
double precision function casm( ) various vars inside the brackets
What is it you are trying to do in Fortran routine with float**? As you know, the data layout in Fortran and C are not the same(column-wise vs. row-wise). It may not work if you try to do references.
Thanks for all your help, I have linked my forrtan and C codes and they are performing as expected.
I have a question though, how do I clear the static variable declared in Fortran from memory? The reason this is important is b/c the fortran code is called almost a million times sometimes greater and the memory gobbled up becomes immense.
Or,
Is there a way I can tell the fortran code to declare the variables only once and not declare them everytime the code is called.