Lisa,

One can do what you do, but it is fraught with some difficulties (as you
experienced) and even more so once you try to do this portably. Helper
packages exists: the `inline` package is the oldest of this class and still
supports the .C() interface you used here. And which for a few years now has
generally been recommended against.  But if you insist, you can; and `inline`
will take care of compiling, linking and loading for you.

But you already use C++ as evidenced by the iostream use. So why not use just
a wee bit more of it?  It makes you program simpler (one less argument on the
signature) and opens the door to more tooling--in the snippet below we use
Rcpp without using any of its data types. It even runs the R example for us.

Code first:
-----------------------------------------------------------------------------
#include <Rcpp.h>

// [[Rcpp::export]]
void sum_c(int& p, std::vector<double> array) {
  int n = array.size();
  double res = 0;
  for (int i = 0; i < p * n; ++i) {
    res += array[i];
  }
  std::cout << "result : " << res << std::endl;
}

/*** R
myvec <- sqrt(1:10)
sum(77, myvec)
*/
-----------------------------------------------------------------------------

Usage demo:
-----------------------------------------------------------------------------
R> Rcpp::sourceCpp("/tmp/lisademo.cpp")

R> myvec <- sqrt(1:10)

R> sum(77, myvec)
[1] 99.4683
R> 
-----------------------------------------------------------------------------

There are other helper packages, and if you still want to do it by hand all
details are in _Writing R Extensions_.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

______________________________________________
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

Reply via email to