[Rd] .Call with C and Fortran together (PR#8122)

rabrantes82@gmail.com rabrantes82 at gmail.com
Sun Sep 4 21:54:44 CEST 2005


Full_Name: Ricardo Luiz de Andrade Abrantes
Version: 2.1.1
OS: Debian Linux, kernel 2.6.8
Submission from: (NULL) (201.6.83.153)


The problem can be well explained with the following example:
Suppose I made a program in fortran, and a C interfacece to it. Now I want to
use this C interface in R to call my fortran program. Then I modified my C file
to deal with SEXPs and compile it as a shared lib. Look at the files:

File: prog.f
-------------
      subroutine cg()
      write(*,*) 'Just a simple test'
      end

      subroutine program()
      write(*,*) 'Calling the function...'
      call cg()
      end

File test.h
------------
#include "cfortran.h"

PROTOCCALLSFSUB0(PROGRAM,program)
#define program() CCALLSFSUB0(PROGRAM,program)


File test.c
------------
#include <R.h>
#include <Rdefines.h>
#include <stdio.h>

SEXP simple_program(){
  program();
  return R_NilValue;
}


I compile the C and Fortran souces into a shared lib, open R, do a
dyn.load("lib's name") and then a .Call("simple_program").
What I got?
Calling the function...
Segmentation fault

What if I change the cg function's name to pp? My Fortran code is now:

File: prog.f
-------------
      subroutine pp()
      write(*,*) 'Just a simple test'
      end

      subroutine program()
      write(*,*) 'Calling the function...'
      call pp()
      end

And the output from R is:

 Calling the function...
 Just a simple test
NULL

In some machines I don't get the segmentation fault problem, but I don't get the
message "Just a simple test" either (when using "cg" as the subroutine's name).
I believe this is bug in R because if I change my C interface again to return a
0 instead of a R_NilValue, and then use it with another C program wich loads the
dynamic library amd call the function simple_program(), everything work
perfectly.


   Thanks,


         Ricardo



More information about the R-devel mailing list