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

Subodh Acharya shoebodh at gmail.com
Sat Aug 24 01:33:24 CEST 2013


Hi,
 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.
Thanks in advance

 Here is an example of the code I am using

//sca2.cpp

#include <RcppArmadilloExtensions/sample.h>
#include "sim2.h" //(my custom header file in)
//[[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;
using namespace std;

// [[Rcpp::export]]

 NumericVector test_sca2(NumericVector vec, NumericVector probVec){

RNGScope scope;

//Environment base("package:base");
//Function r_sample = base["sample"];

  NumericVector sample_out = RcppArmadillo::sample(vec, 1, false, probVec);

//  NumericVector sample_out = r_sample(vec, 1, false, probMat(0, _));

 return(sample_out);

 }


 //sca1.cpp

 #include <RcppArmadilloExtensions/sample.h>
 # include "sim2.h"
 //[[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;
using namespace std;

// [[Rcpp::export]]

 NumericVector test_sca2(NumericVector vec, NumericVector probVec){

  RNGScope scope;

 NumericVector sample_out = RcppArmadillo::sample(vec, 1, false, probVec);


 return(sample_out);

 }
 Note that when I comment out the RcppArmadillo::sample() code in sca_test1
and uncomment the previous 2 lines, the package is compiled built fine

 Here are Rstudio build command results

 * installing to library 'C:/Users/Subodh/Documents/R/win-library/2.15'
* installing *source* package 'sampleTest' ...
** libs
g++  -I"C:/PROGRA~1/R/R-215~1.2/include" -DNDEBUG
 -I"C:/Users/Subodh/Documents/R/win-library/2.15/Rcpp/include"
-I"C:/Users/Subodh/Documents/R/win-library/2.15/RcppArmadillo/include"
 -O2 -Wall  -mtune=core2 -c RcppExports.cpp -o RcppExports.o
g++  -I"C:/PROGRA~1/R/R-215~1.2/include" -DNDEBUG
 -I"C:/Users/Subodh/Documents/R/win-library/2.15/Rcpp/include"
-I"C:/Users/Subodh/Documents/R/win-library/2.15/RcppArmadillo/include"
 -O2 -Wall  -mtune=core2 -c sca1.cpp -o sca1.o
g++  -I"C:/PROGRA~1/R/R-215~1.2/include" -DNDEBUG
 -I"C:/Users/Subodh/Documents/R/win-library/2.15/Rcpp/include"
-I"C:/Users/Subodh/Documents/R/win-library/2.15/RcppArmadillo/include"
 -O2 -Wall  -mtune=core2 -c sca2.cpp -o sca2.o
g++ -shared -s -static-libgcc -o sampleTest.dll tmp.def RcppExports.o
rcpp_hello_world.o sca1.o sca2.o
C:/Users/Subodh/Documents/R/win-library/2.15/Rcpp/lib/i386/libRcpp.a
-LC:/PROGRA~1/R/R-215~1.2/bin/i386 -lR
sca2.o:sca2.cpp:(.text+0x170): multiple definition of
`Rcpp::RcppArmadillo::SampleReplace(Rcpp::Vector<13>&, int, int)'
sca1.o:sca1.cpp:(.text+0x170): first defined here
sca2.o:sca2.cpp:(.text+0x1d0): multiple definition of
`Rcpp::RcppArmadillo::SampleNoReplace(Rcpp::Vector<13>&, int, int)'
sca1.o:sca1.cpp:(.text+0x1d0): first defined here
sca2.o:sca2.cpp:(.text+0x320): multiple definition of
`Rcpp::RcppArmadillo::FixProb(Rcpp::Vector<14>&, int, bool)'
sca1.o:sca1.cpp:(.text+0x320): first defined here
sca2.o:sca2.cpp:(.text+0x8f0): multiple definition of
`Rcpp::RcppArmadillo::ProbSampleNoReplace(Rcpp::Vector<13>&, int, int,
arma::Col<double>&)'
sca1.o:sca1.cpp:(.text+0x8f0): first defined here
sca2.o:sca2.cpp:(.text+0x1400): multiple definition of
`Rcpp::RcppArmadillo::ProbSampleReplace(Rcpp::Vector<13>&, int, int,
arma::Col<double>&)'
sca1.o:sca1.cpp:(.text+0x1400): first defined here
collect2: ld returned 1 exit status
ERROR: compilation failed for package 'sampleTest'
* removing 'C:/Users/Subodh/Documents/R/win-library/2.15/sampleTest'
* restoring previous
'C:/Users/Subodh/Documents/R/win-library/2.15/sampleTest'

Exited with status 1.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130823/6bcd7015/attachment.html>


More information about the Rcpp-devel mailing list