<div dir="ltr"><br clear="all"><div><div>Hi, </div><div> I am a beginner in Rcpp and I am very much enjoying the amazing Rcpp and RcppArmadillo. It has really boosted my speed.</div><div>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(). </div>
<div> 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. </div>
<div>Obviously, there is a multiple definition problem (from the message) but I could not solve it. Any help will be highly appreciated. </div><div>Thanks in advance</div><div><br></div><div> Here is an example of the code I am using </div>
<div><br></div><div>//sca2.cpp</div><div> </div><div>#include <RcppArmadilloExtensions/sample.h></div><div>#include "sim2.h" //(my custom header file in)</div><div>//[[Rcpp::depends(RcppArmadillo)]]</div><div>
<br></div><div>using namespace Rcpp;</div><div>using namespace std;</div><div><br></div><div>// [[Rcpp::export]]</div><div><br></div><div> NumericVector test_sca2(NumericVector vec, NumericVector probVec){</div><div> </div>
<div>RNGScope scope;</div><div><br></div><div>//Environment base("package:base");</div><div>//Function r_sample = base["sample"];</div><div><br></div><div>  NumericVector sample_out = RcppArmadillo::sample(vec, 1, false, probVec);</div>
<div>  </div><div>//  NumericVector sample_out = r_sample(vec, 1, false, probMat(0, _));</div><div> </div><div> return(sample_out);</div><div> </div><div> }</div><div> </div><div> </div><div> //sca1.cpp</div><div> </div><div>
 #include <RcppArmadilloExtensions/sample.h></div><div> # include "sim2.h"</div><div> //[[Rcpp::depends(RcppArmadillo)]]</div><div><br></div><div>using namespace Rcpp;</div><div>using namespace std;</div><div>
<br></div><div>// [[Rcpp::export]]</div><div> </div><div> NumericVector test_sca2(NumericVector vec, NumericVector probVec){</div><div>  </div><div>  RNGScope scope;</div><div>  </div><div> NumericVector sample_out = RcppArmadillo::sample(vec, 1, false, probVec);</div>
<div> </div><div> </div><div> return(sample_out);</div><div> </div><div> }</div><div> 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</div>
<div> </div><div> Here are Rstudio build command results </div><div> </div><div> * installing to library 'C:/Users/Subodh/Documents/R/win-library/2.15'</div><div>* installing *source* package 'sampleTest' ...</div>
<div>** libs</div><div>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</div>
<div>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</div>
<div>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</div>
<div>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</div>
<div>sca2.o:sca2.cpp:(.text+0x170): multiple definition of `Rcpp::RcppArmadillo::SampleReplace(Rcpp::Vector<13>&, int, int)'</div><div>sca1.o:sca1.cpp:(.text+0x170): first defined here</div><div>sca2.o:sca2.cpp:(.text+0x1d0): multiple definition of `Rcpp::RcppArmadillo::SampleNoReplace(Rcpp::Vector<13>&, int, int)'</div>
<div>sca1.o:sca1.cpp:(.text+0x1d0): first defined here</div><div>sca2.o:sca2.cpp:(.text+0x320): multiple definition of `Rcpp::RcppArmadillo::FixProb(Rcpp::Vector<14>&, int, bool)'</div><div>sca1.o:sca1.cpp:(.text+0x320): first defined here</div>
<div>sca2.o:sca2.cpp:(.text+0x8f0): multiple definition of `Rcpp::RcppArmadillo::ProbSampleNoReplace(Rcpp::Vector<13>&, int, int, arma::Col<double>&)'</div><div>sca1.o:sca1.cpp:(.text+0x8f0): first defined here</div>
<div>sca2.o:sca2.cpp:(.text+0x1400): multiple definition of `Rcpp::RcppArmadillo::ProbSampleReplace(Rcpp::Vector<13>&, int, int, arma::Col<double>&)'</div><div>sca1.o:sca1.cpp:(.text+0x1400): first defined here</div>
<div>collect2: ld returned 1 exit status</div><div>ERROR: compilation failed for package 'sampleTest'</div><div>* removing 'C:/Users/Subodh/Documents/R/win-library/2.15/sampleTest'</div><div>* restoring previous 'C:/Users/Subodh/Documents/R/win-library/2.15/sampleTest'</div>
<div><br></div><div>Exited with status 1.</div><div><br></div></div><br>
</div>