Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Fortran > Re: calling C f...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 9 Topic 8540 of 8908
Post > Topic >>

Re: calling C from fortran

by fj <francois.jacq@[EMAIL PROTECTED] > Jul 23, 2008 at 02:02 AM

On 23 juil, 07:59, rudra <bnrj.ru...@[EMAIL PROTECTED]
> wrote:
> dear friend, i have a C++ routine(badly written):
>
> main()
> {
>   ofstream myfile;
> /********* PART 1: Checking machine bit *********/
>
> FILE *stream;
> char sys[6],*sysptr;
> int bit;
> sysptr = &sys[0];
> sys[6] = 0x0;
> stream=popen("/bin/uname -m","r");
> fread(sysptr,1,6,stream);
> pclose(stream);
> //printf("%s\n",sysptr);
>
> if ((strncmp(sysptr, "i686",4)==0)||
>    (strncmp(sysptr, "i386",4)==0))
>     {
>         bit=32;
>      }
>      else {
>      bit=64;
>           }
>     myfile.open ("machine");
>     myfile << bit<<"\n";
>   myfile.close();
>         printf("This is a %d bit machine \n",bit);
>
> }
>
> wose output is written in "machine" file. a f90 program is supposed to
> read it as
>
> open(2,file="machine",status="old")
> read(2,*) bit
> close(2)
>
> I want to use the C code as a function of the f90 program.
> can anybody tell me how can i do that?

Transform your C++ code into a C code first : you don't need C++ for
such routine. In addition, you just want to get the value of "bit".
Therefore, you don't need to use the "machine" file :


File operating_system.c :

#include <stdlib.h>
#include <stdio.h>

int operating_system(void){

FILE *stream;
char sys[6],*sysptr;
int bit;
sysptr = &sys[0];
sys[6] = 0x0;
stream=popen("/bin/uname -m","r");
fread(sysptr,1,6,stream);
pclose(stream);

bit=64;

if ((strncmp(sysptr, "i686",4)==0)||
   (strncmp(sysptr, "i386",4)==0))
        bit=32;

return bit;
}

In the FORTRAN program, you just need to declare an interface to that
routine :

PROGRAM test

  INTERFACE operating_system
     FUNCTION operating_system() RESULT(r)
BIND(C,name="operating_system")
       USE iso_c_binding
       INTEGER(C_INT) :: r
     END FUNCTION
  END INTERFACE

  INTEGER :: bit

  bit=operating_system()
  WRITE(*,*) bit

END PROGRAM

Shell commands :

$$$ cc -c operating_system.c
$$$ gfortran test.f90 operating_system.o
$$$ ./a.out
  32

Caution : this is a F2003 technique. To use it, you need a recent
FORTRAN compiler like ifort-10.x, gfortran-4.3, g95 ...
 




 9 Posts in Topic:
calling C from fortran
rudra <bnrj.rudra@[EMA  2008-07-22 22:59:49 
Re: calling C from fortran
=?iso-8859-1?q?J=F6rgen_T  2008-07-23 08:57:16 
Re: calling C from fortran
fj <francois.jacq@[EMA  2008-07-23 02:02:16 
Re: calling C from fortran
rudra <bnrj.rudra@[EMA  2008-07-23 07:06:41 
Re: calling C from fortran
Dave Seaman <dseaman@[  2008-07-23 14:25:56 
Re: calling C from fortran
"James Van Buskirk&q  2008-07-23 09:40:52 
Re: calling C from fortran
Dave Seaman <dseaman@[  2008-07-23 19:57:43 
Re: calling C from fortran
fj <francois.jacq@[EMA  2008-07-23 09:03:42 
Re: calling C from fortran
Ron Ford <ron@[EMAIL P  2008-07-25 01:34:47 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Nov 19 9:31:04 CST 2008.