[Rcpp-devel] Building/linking trouble with cxxfunction()

Dirk Eddelbuettel edd at debian.org
Wed Feb 16 23:00:28 CET 2011


Ken,

Here is an updated version of the 'secondExample' from that aforementioned
example file in Rcpp.  It uses the plugin generator facility to override LIBS
which makes it work on my box. There may be a buglet in the RcppGSL package
and its definition of the default LIBS, or it maybe on my box. Dunno.


secondExample <- function() {

    ## now use Rcpp to pass down a parameter for the seed
    gslrng <- '
    int seed = Rcpp::as<int>(par) ;

    gsl_rng *r;
    gsl_rng_env_setup();
    double v;

    r = gsl_rng_alloc (gsl_rng_default);

    gsl_rng_set (r, (unsigned long) seed);
    v = gsl_rng_get (r);

    #ifndef BeSilent
    printf("generator type: %s\\n", gsl_rng_name (r));
    printf("seed = %d\\n", seed);
    printf("first value = %.0f\\n", v);
    #endif

    gsl_rng_free(r);
    return Rcpp::wrap(v) ;
    '

    plug <- Rcpp:::Rcpp.plugin.maker(include.before = "#include <gsl/gsl_rng.h>",
                                     libs = paste("-L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp",
                                                  "-Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib",
                                                  "-L/usr/lib -lgsl -lgslcblas -lm"))
    registerPlugin("gslDemo", plug )

    funx <- cxxfunction(signature(par="numeric"), gslrng, plugin="gslDemo")
    print(funx(0))

    plug <- Rcpp:::Rcpp.plugin.maker(include.before = "#include <gsl/gsl_rng.h>",
                                     include.after = "#define BeSilent 1",
                                     libs = paste("-L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp",
                                                  "-Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib",
                                                  "-L/usr/lib -lgsl -lgslcblas -lm"))
    registerPlugin("gslDemo", plug )

    funx <- cxxfunction(signature(par="numeric"), gslrng, plugin="gslDemo")
    print(funx(0))


    invisible(NULL)
}


When I run this I get the desired behaviour of running without and with a
DEFINEd silencer:

R> secondExample <- function() {
+ 
+     ## now use Rcpp to pass down a parameter for the seed
+     gslrng <- '
+     int seed = Rcpp::as<int>(par) ;
+ 
+     gsl_rng *r;
+     gsl_rng_env_setup();
+     double v;
+ 
+     r = gsl_rng_alloc (gsl_rng_default);
+ 
+     gsl_rng_set (r, (unsigned long) seed);
+     v = gsl_rng_get (r);
+ 
+     #ifndef BeSilent
+     printf("generator type: %s\\n", gsl_rng_name (r));
+     printf("seed = %d\\n", seed);
+     printf("first value = %.0f\\n", v);
+     #endif
+ 
+     gsl_rng_free(r);
+     return Rcpp::wrap(v) ;
+     '
+ 
+     plug <- Rcpp:::Rcpp.plugin.maker(include.before = "#include <gsl/gsl_rng.h>",
+                                      libs = paste("-L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp",
+                                                   "-Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib",
+                                                   "-L/usr/lib -lgsl -lgslcblas -lm"))
+     registerPlugin("gslDemo", plug )
+ 
+     funx <- cxxfunction(signature(par="numeric"), gslrng, plugin="gslDemo")
+     funx(0)
+ 
+     plug <- Rcpp:::Rcpp.plugin.maker(include.before = "#include <gsl/gsl_rng.h>",
+                                      include.after = "#define BeSilent 1",
+                                      libs = paste("-L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp",
+                                                   "-Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib",
+                                                   "-L/usr/lib -lgsl -lgslcblas -lm"))
+     registerPlugin("gslDemo", plug )
+ 
+     funx <- cxxfunction(signature(par="numeric"), gslrng, plugin="gslDemo")
+     print(funx(0))
+ 
+ 
+     invisible(NULL)
+ }
R> secondExample()
generator type: mt19937
seed = 0
first value = 4293858116
[1] 4293858116
R>

Long story short, the plugin mechanism is very powerful and useful for us as
we can access it from RcppArmadillo etc and let these packages use inline.

For quick hacks, you can also try cfunction() as per the earlier example.

Hth, Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list