[Rcpp-devel] Sharing Cpp code between packages

Dirk Eddelbuettel edd at debian.org
Tue May 20 17:41:26 CEST 2014


On 20 May 2014 at 17:26, Alessandro Mammana wrote:
| Dear All,
| first of all thanks again for Rcpp, I am becoming more and more
| dependent on it and I am filling my directories with .cpp scripts...
| 
| I have a Rcpp package P using some header file F.hpp in its src/
| directory. This file F.hpp contains templated functions and structs.
| Is there a way for a new Rcpp package Q to include file F.hpp?

Yes. 

But you need to move it from src/F.hpp to inst/include/F.hpp which, once
installed, will become include/F.hpp.

The big trick then is that by just saying "LinkingTo: P" you get R to do the
right thing via the appropriate -I.... flags.

This is essence all that the package BH does to give everybody Boost headers.
 
| I read already Writing R Extensions "Linking to native routines in
| other packages" as well as Rcpp-attriibutes, "Providing a C++
| Interface". It looks like there are two mechanisms for sharing c++
| code.
| 1. The "interface" mechanism: the real code is in the src/ directory
| but it is referred to from some headers in the inst/include directory.
| Additionally, these headers need to be wrapped with some (boilerplate
| and obscure to me) C code. This works only with functions. The
| boilerplate code can be nicely generated automatically with Rcpp
| attributes.

Yes. That is probably the best route __when you need object code__.  I am
using that in package RcppXts from (non-Rcpp) package xts, and in the new
RcppRedis from RApiSerialize.

The exporting package also needs to do some work.  See for example
RApiSerialize -- all it does is register two functions.  The mechanism is
pure R mechanics and not Rcpp specific but can be used by Rcpp if you want
to. 

| 2. The "direct" mechanism: just put the code in inst/include and tweak
| the Makevars so that the compiler knows where to look for those
| headers. This should work also with structs and templated cpp code.

See above. No tweaks to Makevars needed (!!) but works only for template headers.
 
| I think I need the "direct" mechanism.
| In package P, I moved my hpp files in inst/include and tweaked the
| Makevars, the package compiled fine.
| Now in package Q I am trying to use some of the stuff, and I am
| including the files I need. I also did the following:
| 1. put import(P) in Q's namespace file
| 2. put Import P and LinkingTo P in Q's description file
| 
| however during compilation I got an error like:
| undefined symbol: _Z16rcpp_hello_worldv

It is a linking error.  
 
| do you know what could be wrong?

Are you expecting object code? Then you need to do more.

You are doing the right thing by working it out with small examples.

Dirk

| Thanks a lot and sorry for bothering.
| 
| Ale
| 
| 
| -- 
| Alessandro Mammana, PhD Student
| Max Planck Institute for Molecular Genetics
| Ihnestraße 63-73
| D-14195 Berlin, Germany
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list