[Rcpp-devel] [RcppExamples] -- would someone share the makefile?
Romain Francois
romain at r-enthusiasts.com
Sun Aug 22 14:16:32 CEST 2010
Hi,
This is precisely because getting these bits is somewhat involved that
we provide Rcpp.package.skeleton. Once the package is generated by it,
you just have to modify the .cpp and .h files to do whatever you want to
do, as long as you respect the requirements of the .Call interface.
The Rcpp-package vignette explains what Rcpp.package.skeleton does and
why. There are essentially two important things :
- your package must find Rcpp headers, this is achieved by adding
LinkingTo: Rcpp in your DESCRIPTION. This will help R CMD INSTALL by
adding automatically the -I flag so that you can use #include <>
- your package must link against the Rcpp library (this is a C++ library
provided by Rcpp. This is accomodated by the PKG_LIBS line in the
Makevars file
Something else you might find interesting is the inline package, it lets
you write C++ code directly from within the R console (provided you have
all the required tools).
For example, with the code that is on page 7 of the Rcpp-introduction
vignette :
fx <- cxxfunction( signature( a = "numeric", b = "numeric" ), '
Rcpp::NumericVector xa(a);
Rcpp::NumericVector xb(b);
int n_xa = xa.size() ;
int n_xb = xb.size() ;
Rcpp::NumericVector xab(n_xa + n_xb - 1);
double* pa = xa.begin() ;
double* pb = xb.begin() ;
double* pab = xab.begin() ;
int i,j=0;
for (i=0;i<n_xa;i++)
for(j=0;j<n_xb;j++)
pab[i + j] += pa[i] * pb[j];
return xab ;
', plugin = "Rcpp" )
x <- rnorm(10)
y <- rnorm(10)
fx( x, y )
You can even do this to make a skeleton package out of this function :
package.skeleton( "wonderland", fx )
Romain
Le 21/08/10 22:30, Johannes Egner a écrit :
> Dirk, Romain,
>
> thanks for your replies.
>
> I suspect I should have been more verbose. Here's what I was trying to do:
>
> (1) The convolve function is the standard example for using .Call or .C;
> and it is easy (using MinGW or even cl.exe in MSVC) to produce a DLL
> that one can call from R (possibly modulo name mangling): 5 minutes,
> tops. Most importantly: up to the point where R loads the DLL, I know
> precisely what was going on.
>
> (2) This is a reassuring result, and I want to achieve the same thing
> with Rcpp. Ok, let's read your article "Rcpp: Seamless R and C++
> integration". Page 2 has the code. I didn't expect it to work, but
> nonetheless tried MinGW (because you were pretty clear on MSVC...) on
> this simple example. (Let's forget about a header file that may be
> necessary to expose the function, or any trap related to calling
> conventions for the moment.)
>
> (3) MinGW needs the header files from Rcpp (0.8.5, WinBinary -- I don't
> think 0.7.1 is any different, although I should have used this). Rcpp.h
> is needed, which I find in Rcpp/include and place it in the root of the
> "project folder" (a folder just containing the cpp-file). Ok, this isn't
> working -- it needs to be #include "Rcpp.h", and not #include <Rcpp.h>,
> because I have no environment variables set for MinGW, and the compiler
> apparently does not look at the root of the folder (which MSVC would do).
>
> (4) Having changed that, MinGW wants to see RcppCommon.h (ok, my mistake
> -- I should have had a look into Rcpp.h!). Same bracket vs quote thing.
> Next are various header files from Rcpp/include/Rcpp and Rcpp/classic --
> each time with the same bracket/quote issue.
>
> (5) I must be on the wrong track -- changing the header files cannot be
> the intented solution. I now suspect a makefile could be the solution --
> maybe it defines where to look for all those header files. (Hoping it
> also might give me some indication how I can later compile code from
> many different files.) Because I don't know (yet) how to make such a
> makefile, I have looked into RcppExamples, and then written my first
> mail to the list.
>
> Now using Rcpp.package.skeleton is indeed a step forward -- it passes R
> CMD, becomes a tarball, and I can build a zip file with the DLL in it.
> (Which correctly exposes the function rcpp_hello_world). It seems R CMD
> build miraculously knows how to feed the right arguments to the compiler
> (and lets it know where all the headers are). As convenient as this is,
> it is a bit too opaque for me. I suspect I'd struggle to develop
> "realistic code" (several files, links to static/dynamic libraries,...)
> just relying on R's package building mechanism.
>
> Long story short: something as in (1), i.e. a simple, but %working%
> piece of code where all "mechanics" (dependencies, compiler arguments,
> etc) are transparent is what I'm after. I would imagine that this could
> be done by explaining what happens behind the stages when R CMD build is
> called, and that these mechanics are best scaled to code projects of
> realistic sizes by the mentioned makefiles -- but I might of course be
> wrong.
>
> Best wishes, Jo
--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2
More information about the Rcpp-devel
mailing list