rabrantes82@gmail.com
2005-Sep-04 19:54 UTC
[Rd] .Call with C and Fortran together (PR#8122)
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
> > 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. >I don't think it is an R bug. I think it is because there is already a Fortran function called cg in R. The fact that changing the name matters suggest that you have a linking problem, and this turns out to be the case. When I try running your code under gdb in R as Peter Dalgaard suggested (after editing it to use R's macros for calling fortran from C instead of "cfortran.h" which I don't have), I get> .Call("simple_program")Calling the function... Program received signal SIGSEGV, Segmentation fault. 0x081604e5 in cg_ (nm=0x9e5dda4, n=0xbfefccfc, ar=0xbfefcce8, ai=0x89a826, wr=0x9e5dda4, wi=0x9790cc0, matz=0x56090a0, zr=0x80992d4, zi=0x0, fv1=0x0, fv2=0x9e745f8, fv3=0x89a810, ierr=0x706d6973) at eigen.f:3416 3416 IERR = 10 * N Current language: auto; currently fortran That is, your program is calling the Fortran subroutine CG in eigen.f, rather than your CG. There should be some set of linker flags that makes sure your definition of CG is used, but I don't know what it would be (and it's probably very platform dependent) -thomas