Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

calling c subprogram from fortran

roddur
Beginner
557 Views
I want to use the c subprogram
[cpp]/***********************************************
 This code returns the machine bit
 to the program bitinit
***********************************************/
#include 
#include 
#include 
int operating_system(void){
FILE *stream;
char sys[8],*sysptr;
int bit;
sysptr = &sys[0];
sys[6] = 0x0;
stream=popen("/bin/uname -m","r");
//stream=popen("file /sbin/init|awk '{print $3}'|cut -c1-2","r");
//printf("bit = %sn",stream);
fread(sysptr,1,2,stream);
pclose(stream);

printf ("%sn",sysptr);
/*bit=64;
if ((strncmp(sysptr, "i686",2)==0)||
   (strncmp(sysptr, "i386",4)==0))
        bit=32;*/
return bit;
} [/cpp]
for my code in f90. I have interfaced it to fortran as
[cpp]	interface operating_system
	function operating_system()result &
		bind(C,name="operating_system")
	use iso_c_binding
       	integer(C_INT) :: r
     	end function
  	end interface 
[/cpp]
but the problem is if i want the c-code to return the value of uname, its not doing....what should i use instead of "void"? can you plz tell me so that i can print the output of uname in my fortran code? i am c-illeterate, and facing a lot of troubles.
regards
0 Kudos
2 Replies
Yuan_C_Intel
Employee
557 Views


Roddur,
Youcould pass Stringinstead of intas yourfunction result, e.g.: In f90, define the interface as:

interface operating_system
function operating_system() result(string) &
bind(C,name="operating_system")
use iso_c_binding
CHARACTOER *8 :: string
end function
end interface

...
CHARACTER*8 unames
unames = operating_system();
write(*,*) unames


In c subprogram, define:
voidoperating_system(char * result, int * length)
{
...
// Copy output to result allocated at fortran
for (i=0; i{
result = sysptr;
}
...
}

Note that the 2 parameters at Cfunction do not appear in the call statement; they are added by the compiler.
You can also refer to the intel compiler documentation onsection: "Programming Mixed Language - Handling Character Strings" for your reference.

0 Kudos
roddur
Beginner
557 Views
Thanks Chen!
I have done this
0 Kudos
Reply