[Rcpp-devel] How to use external C++ libraries in R packages withRcpp.

guxiaobo1982 guxiaobo1982 at qq.com
Wed Sep 4 12:21:13 CEST 2013


Hi Dirk,
 
Thanks for you help, here is wha I got in a test package:
 
1.Rcpp.package.skeleton("cpptest")
2. Add the following line to Makevars
 
PKG_CPPFLAGS=-I/opt/boost_1_54_0/include -I../inst/include PKG_LIBS = -L/opt/boost_1_54_0/lib -lboost_regex `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"`

3. Change  rcpp_hello_world.cpp to this:
 
#include "rcpp_hello_world.h"
#include <boost/lambda/lambda.hpp>
#include <boost/regex.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
using namespace Rcpp;

int test(std::string line){
 boost::regex pat("^Subject: (Re: |Aw: )*(.*)" );
 boost::smatch matches;
 if (boost::regex_match(line, matches, pat))
  return 1;
 return 0;
}
SEXP rcpp_hello_world(){
 std::string line1 = "Subject: (Re: I am amber";
 std::string line2 = "Subject: (Aw: Who are you";
    
    NumericVector y   = NumericVector::create(test(line1), test(line2)) ;
    List z            = List::create( y ) ;
    
    return z ;
}

4: 
 
[root at cent64 dev]# R CMD INSTALL cpptest
* installing to library ‘/usr/lib64/R/library’
* installing *source* package ‘cpptest’ ...
** libs
make: Nothing to be done for `all'.
installing to /usr/lib64/R/library/cpptest/libs
** R
** preparing package for lazy loading
** help
Warning: /home/amber/R/dev/cpptest/man/cpptest-package.Rd:31: All text must be in a section
Warning: /home/amber/R/dev/cpptest/man/cpptest-package.Rd:32: All text must be in a section
*** installing help indices
  converting help for package ‘cpptest’
    finding HTML links ... done
    cpptest-package                         html  
    rcpp_hello_world                        html  
** building package indices
** testing if installed package can be loaded
Error in dyn.load(file, DLLpath = DLLpath, ...) : 
  unable to load shared object '/usr/lib64/R/library/cpptest/libs/cpptest.so':
  /usr/lib64/R/library/cpptest/libs/cpptest.so: undefined symbol: _ZTIN4Rcpp7RObjectE
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/usr/lib64/R/library/cpptest’
[root at cent64 dev]# exit

Actually, I am trying to use boost library with Rcpp, do I miss anything?
 
Regards,
 
Xiaobo Gu
 
 

 


------------------ Original ------------------
From:  "Dirk Eddelbuettel"<edd at debian.org>;
Date:  Sep 3, 2013
To:  "guxiaobo1982"<guxiaobo1982 at qq.com>; 
Cc:  "rcpp-devel"<rcpp-devel at lists.r-forge.r-project.org>; 
Subject:  Re: [Rcpp-devel] How to use external C++ libraries in R packages withRcpp.




Hi Xiaobo,

On 2 September 2013 at 18:33, guxiaobo1982 wrote:
| We are going to package an external C++ library into a R package, and I'd like
| to know Rcpp can help a lot,

Yes, Rcpp can help a lot in that case. 

Many existing packages provides examples, starting for example with RcppGSL
by Romain and myself and many other existing CRAN packages which connect to
external libraries using Rcpp.

We have an entire vignette devoted to using Rcpp for your package:
Rcpp-package.  And my Rcpp book has a chapter on this too.

| but we have two sinarioes:

[ You probably meant "scenarios" ]

| 1. We have only c++ header files, .lib dynamic link file and .dll dynamic
| runtime library.

".lib" and ".dll" sounds a lot like Windows-only.  Please understand that you
generally cannot link with Visual C++-compiled code (unless you do a pure C
layer in between, and even that is tricky.

But any "standard" library -- such as GSL, or anything else that is portable
and available on Windows, Linux and OS X can be used.

| 2. A whole C++ library with all the header and .cpp source file in a seperate
| directory.

That can be done too; you could build a static library first. The trick is to
avoid having to do a custom Makefile (as R prefers you do just use
src/Makevars or src/Makevars.win) so sometimes it is easier to just have all
the files in src/* --- I helped the folks with the phylobase package do just
that.
  
You can hardcode Windows path in src/Makevars.win, possibly using an
environment variable.  Here is the one from RcppGSL:


  ## This assumes that the LIB_GSL variable points to working GSL libraries
  PKG_CPPFLAGS=-I$(LIB_GSL)/include -I../inst/include
  PKG_LIBS=-L$(LIB_GSL)/lib -lgsl -lgslcblas $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "Rcpp:::LdFlags()")

For Unix one often uses configure but as a first approximation you can also
use environment variables on src/Makevars.

| In both sinarioes, how can I specify additional include pathes, add the
| additional sources, and link against external .lib files.

Please see the vignette, or the chapter in my book.

Dirk
  
| Regards,
|  
| Xiaobo Gu
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130904/b1700466/attachment.html>


More information about the Rcpp-devel mailing list