Hi, John, Making your code header-only is your best choice and it will make everything easier.
If header-only is impossible, this can happen, there is one trick you may want to try. As I remember the early version of Rcpp, maybe before 0.9, it builds a libRcpp.so when installing Rcpp. You can also do this by adding several lines in Makevars [1] and write helper functions to help system find the shared library you want to link to [2]. But I don't think this is good practice for Rcpp developing. Best wishes, KK [1] https://github.com/thirdwing/RcppMLPACK/blob/master/src/Makevars#L44-L61 [2] https://github.com/thirdwing/RcppMLPACK/blob/master/R/flags.R On Fri, Mar 6, 2015 at 5:03 PM, John Tipton <jtipto...@gmail.com> wrote: > Hey Rcpp developers, > > I am brand spanking new to the Rcpp community and I want to introduce > myself with what is likely a very simple question that I haven't found an > answer for yet. My question is on package development and linking c++ files > from one package directly to another package without having to create a > CCallable function . As a relatively experienced R programmer but novice > c++ programmer, I am still learning as I go so I created a minimal example > below that I believe better illustrates my question. > > I am building a package with RStudio and I want to make available to > another package the .h header files - (more explicitly the c++ subroutines > in those headers) without the loss of computation speed shown below. > > My question can best be summarized with two parts > > 1) Why when I directly source testPack.h is the function call so much > slower than when I define the function in the same .cpp file (i.e. why is > fun1.cpp so much slower (~40%) than fun2.cpp in test.R) > > 2) How do I build a library in an automated fashion (maybe there is a > CPPFLAGS variable?) so I can use the functionality of something like > #include “path/to/file/hello_world2.h” in fun2.cpp without using an > absolute path? I am building a large MCMC sampler so every bit of marginal > speed gain is important. In other words, how can I replicate the behavior > of fun2.cpp in a function that sources my library testPack > > > I have the following files (outside my package) that provide testing. > Thanks for any help. > > test.R > fun1.cpp <- includes <testPack.h> header, runs slow > fun2.cpp <- includes “hello_world2.h” header, runs faster > > I built a package skeleton in RStudio using Rcpp. Within the src directory > I have the files > hello_world2.h > cpp_hello_world.cpp > > > The files are as follows > ## > ## > ## > ———— begin test.R script ———— > > library(rbenchmark) > n <- 10000 > Rcpp::sourceCpp('~/test/fun1.cpp’) > Rcpp::sourceCpp('~/test/fun2.cpp’) > > benchmark(fun1(n), fun2(n), replications = 500) > > ———— end test.R script ———— > ## > ## > ## > ———— begin fun1.cpp ———— > > #include <RcppArmadillo.h> > #include <testPack.h> > // [[Rcpp::depends(testPack)]] > // [[Rcpp::depends(RcppArmadillo)]] > > //using namespace Rcpp; > > //[[Rcpp::export]] > void fun1(const int& n) { > for(int i = 0; i < n; i++){ > testPack::rcpp_hello_world(); > } > } > > ———— end fun1.cpp ———— > ## > ## > ## > ———— begin fun2.cpp ———— > > #include <RcppArmadillo.h> > #include <path/to/file/testPack/src/hello_world2.h> > > // [[Rcpp::depends(RcppArmadillo)]] > > //[[Rcpp::export]] > void fun2(const int& n) { > for(int i = 0; i < n; i++){ > rcpp_hello_world2(); > } > } > > ———— end fun2.cpp ———— > ## > ## > ## > ———— begin hello_world2.h ———— > > Rcpp::List rcpp_hello_world2() { > > Rcpp::CharacterVector x = Rcpp::CharacterVector::create("foo", "bar"); > Rcpp::NumericVector y = Rcpp::NumericVector::create(0.0, 1.0); > Rcpp::List z = Rcpp::List::create(x, y); > > return z ; > } > > ———— end hello_world2.h ———— > ## > ## > ## > ———— begin rcpp_hello_world.cpp ———— > > #include <RcppArmadillo.h> > > // [[Rcpp::depends(RcppArmadillo)]] > // [[Rcpp::interfaces(cpp)]] > > using namespace Rcpp; > > //[[Rcpp::export]] > List rcpp_hello_world() { > > CharacterVector x = CharacterVector::create( "foo", "bar" ) ; > NumericVector y = NumericVector::create( 0.0, 1.0 ) ; > List z = List::create( x, y ) ; > > return z ; > } > > ———— end rcpp_hello_world.cpp ———— > > _______________________________________________ > Rcpp-devel mailing list > Rcpp-devel@lists.r-forge.r-project.org > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel > -- Qiang Kou q...@umail.iu.edu School of Informatics and Computing, Indiana University
_______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel