Hi,
I am having trouble with some code that I am dyn.loading. I am
writing an interface to ARPACK. I compile my interface (dssimp.cc), and
link it against the ARPACK library (libarpack_SUN4.a):
g++ -shared -static -fPIC dssimp.cc -o dssimp.so -larpack_SUN4 -lg2c -lm
I can dyn.load the code and it appears OK. However, when I call my
function, the call to the function in the ARPACK library returns an
error.
I can print the arguments to my function when I call it from R, so I
can see that I am dyn.load'd, and the arguments are passed correctly.
Moreover, I can load and call the shared object (dssimp.so) using
dlopen and dlsym from a stand alone C++ program. So I can see that the
shared object is OK.
In summary, I have a shared object that works correctly outside of R,
but when dyn.load'd into R, does not work. Short of calling errors
(which is not the case here) I wonder how that might happen, and how I
might fix it?
Matt
You are missing some extern "C" declarations? R needs C linkage style.
Basically a lot of:
extern "C" {
}
around your c++ code.
Matt Calder wrote:> Hi,
> I am having trouble with some code that I am dyn.loading. I am
> writing an interface to ARPACK. I compile my interface (dssimp.cc), and
> link it against the ARPACK library (libarpack_SUN4.a):
>
> g++ -shared -static -fPIC dssimp.cc -o dssimp.so -larpack_SUN4 -lg2c -lm
>
> I can dyn.load the code and it appears OK. However, when I call my
> function, the call to the function in the ARPACK library returns an
> error.
> I can print the arguments to my function when I call it from R, so I
> can see that I am dyn.load'd, and the arguments are passed correctly.
> Moreover, I can load and call the shared object (dssimp.so) using
> dlopen and dlsym from a stand alone C++ program. So I can see that the
> shared object is OK.
> In summary, I have a shared object that works correctly outside of R,
> but when dyn.load'd into R, does not work. Short of calling errors
> (which is not the case here) I wonder how that might happen, and how I
> might fix it?
>
> Matt
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
All,
I am still having trouble dyn.load'ing some code into R. I have
isolated the problem, I wonder if someone could explain what I am
seeing.
I think the problem is that a symbol defined in my compiled code
clashes with one already defined in R. The result is that the function
in my code is not called. Here is an example
// lnktst.cc
extern "C"
{
void func(double *out1, double *out2);
void dnrm2_(double *out);
void dnrm3_(double *out);
}
void func(double *out1, double *out2)
{
dnrm2_(out1);
dnrm3_(out2);
}
void dnrm2_(double *out)
{
*out = 1234.5;
}
void dnrm3_(double *out)
{
*out = 6789.0;
}
// End of lnktst.cc
When I compile:
g++ -shared -static -o lnktst.so lnktst.cc
and then in R I call "func"
> dyn.load("lnktst.so")
> .C('func', double(1), double(1))
[[1]]
[1] 0
[[2]]
[1] 6789
So, as you can see, the function "dnrm2_" is not called whereas
"dnrm3_"
is, even though both functions are identical in form. Now, I believe
dnrm2_ is a BLAS function, and so it is likely R already has a copy
floating around. However, it surprises me that the "-static" option
does
not force the call in my code to "dnrm2_" to be linked to the function
defined in my code.
I have been writing C code for Splus for quite a while and don't recall
ever running across this issue. However, I am new to R, so I wonder, am
I missing something obvious?
I am running this on Ubuntu Linux, the output of uname -a is:
Linux calder-linux 2.6.22-14-generic #1 SMP Sun Oct 14 23:05:12 GMT 2007 i686
GNU/Linux
Thanks for any help,
Matt