Vineeth Mohan
Vineeth Mohan

Reputation: 19253

Not able to link dependent header files in cpp file for Rcpp

Based on this tutorial - http://www.r-bloggers.com/using-r-callling-c-code-with-rcpp/ I was trying to call a C function from R. The C code have following dependencies and it works perfectly after compiling the C code

#include <json/json.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <time.h>
#include <math.h>
#include <Rcpp.h>

Now when i am trying to load the so file , i am seeing the below error.

dyn.load("storage.so")
Error in dyn.load("storage.so") : 
unable to load shared object '/home/algotree/Rcode/storage.so':
/home/algotree/Rcode/storage.so: undefined symbol: json_object_array_length

Seems R is not able to link the rest of header files.

How can I fix it?

Upvotes: 0

Views: 510

Answers (2)

Vineeth Mohan
Vineeth Mohan

Reputation: 19253

Here is how i solved the issue , for running the code i had to add -lcurl and -ljson as command link arguement. So the command R CMD SHLIB should have executed is the below commands

g++ -I/usr/share/R/include -DNDEBUG -I/usr/local/lib/R/site-library/Rcpp/include -I/usr/include/ -fpic -O3 -pipe -g -c storage.cpp -o storage.o g++ -shared -o storage.so storage.o -L/usr/lib -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -L/usr/lib64/R/lib -lcurl -ljson -lR

This can be done by editing the PKG_LIBS flags.

Upvotes: 0

Dirk is no longer here
Dirk is no longer here

Reputation: 368201

This has nothing to do with Rcpp (for which we also provide ample documentation regarding use on its own, in package, via inline, ...).

You seem to use JSON-parsing functionality, but apparently have not linked to a JSON-parser library corresponding to the header json/json.h you included.

Apart from this question being incomplete in its code example and hence not reproducible, I see two issues here:

  1. learn the ropes about C/C++ program using libraries, and

  2. apply this to the R context.

As you using JSON and Curl based on your headers, you could (and probably should) study the corresponding packages like RJSONIO and RCurl.

If you know what is going there and understand the mechanics, you can then use Rcpp to provide the new functionality you are seeking. But just by throwing Rcpp in the mix, these issues do not address themselves. You need to understand how in include headers and link libraries.

Upvotes: 3

Related Questions