[Rcpp-devel] multiple definition problem

Romain Francois romain at r-enthusiasts.com
Fri Aug 30 10:19:51 CEST 2013


Le 29/08/13 18:59, Steve Jaffe a écrit :
> 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."

I don't think that works and I've had examples to verify this belief.

Quite recently, implementations of wrap and as for sparse matrices 
classes in RcppArmadillo were huge and yet declared as inline. And we 
still had the multiple definitions problem. There were other issues with 
them, but that's beyond the scope of this thread and this has been fixed 
anyway.

> 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.

Sure. This was not the problem, but it was a bit scary to see a header 
file without include guards.

> Any function defined in a header file,
> which is not in a class definition, and is not a template, should be
> declared as inline.

If this were true we just would have to declare everything as inline.

I don't mean to create a debate here, but I don't think just using the 
inline keyword would have fixed this issue.

Romain

> Steve Jaffe
> steve_jaffe at yahoo.com


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com

blog:            http://blog.r-enthusiasts.com
|- http://bit.ly/13SrjxO : highlight 0.4.2
`- http://bit.ly/10X94UM : Mobile version of the graph gallery



More information about the Rcpp-devel mailing list