[Rcpp-devel] Multiple definition problem with RcppArmadillo:sample() function during package compilation

romain at r-enthusiasts.com romain at r-enthusiasts.com
Sat Aug 24 09:04:21 CEST 2013


Le 2013-08-24 02:47, Dirk Eddelbuettel a écrit :
> Hi,
>
> On 23 August 2013 at 19:33, Subodh Acharya wrote:
> |  I am a beginner in Rcpp and I am very much enjoying the amazing 
> Rcpp and
> | RcppArmadillo. It has really boosted my speed.
> | I am making an R package that links to the RcppArmadillo to call
> its new sample
> | () function. I have several different functions ( defined in 
> separate .cpp
> | files) that call the RcppArmadillo::sample(). 
> |  But whenever I call sample() in more than 1 file, I get the error 
> during
> | package compilation. However, if I use the RcppArmadillo: sample()
> in only one
> | place and use R's sample() function, the package is compiled fine. 
> | Obviously, there is a multiple definition problem (from the 
> message) but I
> | could not solve it. Any help will be highly appreciated. 
>
> That may be a problem at our end in the design of the contributed 
> sample.h
> file.  Someone recently noticed the same issue with sparse matrix 
> conversion
> I had added in a similar fashsion; Romain has since cleaned this up 
> and moved
> it into RcppArmadillo proper.
>
> For now, your best (only?) bet may be to organize your code in such a 
> way
> that sample.h gets included only once.
>
> Sorry, Dirk

This is now fixed in svn.
The file did not have include guards and it was defining functions that 
were too long to be inlined.

Since I don't want the user to have to link against an RcppArmadillo 
library (like we do in Rcpp), I made these functions (SampleReplace, etc 
...) templates.
This should allow people to include this file more than once.

The alternative would be to only have declarations of the 
SampleReplace, etc ... functions in sample.h and definitions in a .cpp 
file in RcppArmadillo, but then we would need users to mess with their 
PKG_LIBS, and we would need to store a library we can link against. This 
is already a lot of trouble to do this with Rcpp, so I'm not doing that 
for RcppArmadillo.

The other alternative is to not host sample in RcppArmadillo but in 
another package. I don't have strong opinion on this, but sample is 
somewhat of an outlier in RcppArmadillo. RcppArmadillo is just supposed 
to make armadillo available. sample is an example use case.

Romain



More information about the Rcpp-devel mailing list