Crops
Crops

Reputation: 5162

Unable to load C shared library in R

I am trying to call a C code double_metaphone.c from R in windows. I am familiar with R , but have not tried C yet.

I have compiled the code and created a shared library as follows in windows using Cygwin gcc including the header file double_metaphone.h

gcc -c double_metaphone.c
gcc -shared -o double_metaphone.dll double_metaphone.o

I have used dyn.load to load the dll file as follows

dyn.load("C:/R/double_metaphone.dll")

getLoadedDLLs() lists double_metaphone.dll, however

is.loaded(double_metaphone.dll)

gives the error

Error in is.loaded(double_metaphone.dll) : 
  object 'double_metaphone.dll' not found

When I try to use .C() or .Call(), I get

Error in .C("double_metaphone") : 
  C symbol name "double_metaphone" not in load table

I understand this is a problem with C++ code as in Link1, Link, but why can't I access the shared library for calling C code from R? Where am I going wrong?

Upvotes: 0

Views: 683

Answers (2)

Mike Jiang
Mike Jiang

Reputation: 209

You need compile and link against R for the shared object to be loadable. e.g.

$ gcc -I/YOUR_R_HOME_DIR/include -DNDEBUG -fpic -c double_metaphone.c -o double_metaphone.o

$ gcc -shared -o double_metaphone.dll double_metaphone.o -L/YOUR_R_HOME_DIR/lib -lR

Or simply

$ R CMD SHLIB double_metaphone.c

As Dirk said, R-exts gives you more details.

And you want to consider Rcpp for writing compiled code in R, which benefits you further down the road.

Upvotes: 1

Icarus3
Icarus3

Reputation: 2350

As per my understanding "is.loaded" checks for the loaded symbols. As per your header, you can try:

is.loaded("DoubleMetaphone")

To call the method. try:

.C("DoubleMetaphone", <your>, <arguments>)

Upvotes: 1

Related Questions