[Rcpp-devel] multiple definition problem

Steve Jaffe sjaffe at riskspan.com
Thu Aug 29 18:59:45 CEST 2013


On 24 Aug 2013 at 09:04, romain at r-enthusiasts.com wrote:
> 
> > On 23 August 2013 at 19:33, Subodh Acharya wrote:
> > But whenever I call sample() in more than 1 file, I get the error 
> > during package compilation. 
> 
> 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

Another alternative is simply to declare such a function as "inline." 

This does not mean that it will be physically inlined -- it may or may not
be, at the discretion of the compiler; it only means that the compiler/linker
are responsible for eliminating multiple definitions, ie it simply makes it 
legal for the function to be defined in multiple compilation units, "as if" it 
were inline.

Include guards won't prevent multiple definition errors when the same header 
is included by multiple compilation units. Any function defined in a header file, 
which is not in a class definition, and is not a template, should be 
declared as inline.

Steve Jaffe
steve_jaffe at yahoo.com


More information about the Rcpp-devel mailing list