Skip to content
Prev 39920 / 63424 Next

[Rcpp-devel] Question on 5.6 Interfacing C++ code

smcguffee wrote:
When I first started out extending R with compiled code, I used R CMD SHLIB
as well. Don't know why exactly, it was probably the first thing I stumbled
across in the manual. Once I learned about making packages and that putting
C, C++ or Fortran code in the `src` directory of the package magically
caused a library to be built, I quit using R CMD SHLIB---probably haven't
touched it in years.

I think R CMD SHLIB may be intended more for compiling external programs
that want to hook into the R libraries rather than things intended to be
loaded by R it's self.
smcguffee wrote:
Well, the best answers to this question lie inside the "Writing R
Extensions" manual---specifically Section 1.2 "Configure and Cleanup".  The
short version is:

 - If the code in your package needs custom compiler flags, add a 
`src/Makevars` file that contains them.

 - If the code in your package dependes on external libraries, add a
Configure script, written using GNU autotools, that will produce
`src/Makevars` from a template `src/Makevars.in` that contains the `-L` and
`-I` flags required to link your code against the external library.

However, I will again suggest taking this one step at a time:

  - Build a toy package that includes C or C++ code that needs to be
compiled. Observe how `R CMD INSTALL` compiles the code for you and how to
use `.onLoad` or `.First.Lib` to `dyn.load` the resulting library when a
user runs `library` on your package.  Bonus points for reading enough of
"Writing R Extensions" to know if having an R NAMESPACE in your package has
any effect on this process.

  - Extend your toy package to include C++ code that needs custom compiler
flags. See how you can achieve this with `src/Makevars`.

  - Extend your package again with an external dependency that requires a
`configure` script. A good example of such a package is `rgdal`---it has to
find both the GDAL and PROJ4 libraries in order to compile operational code.

If you run into any trouble along the way, stop and read "Writing R
Extensions". If you really get stuck, you can then ask the mailing list a
very focused question along with an example that shows what is going wrong
for you. Then you have a good change of getting helpful answers.  Right now
your questions are spanning the entire spectrum from beginning to advanced
package authoring and so the most likely answer you will get from the list
is "slow down and read the manual".
smcguffee wrote:
Quite a brain dump there!  Some things that you may want to look into in the
future:

  - The original mailing list you posted to, Rcpp, is for an R package that
wraps the C API of R into C++ classes.  I would bet it also provides methods
for calling R code and C++ without having to write as many R functions.  I
have not had the pleasure of using Rcpp yet---Fortran was my first compiled
language and I am still moving my way up the food chain :)

  - The inline package may be of interest to you---It allows C, C++ and
Fortran programs to be stored as text strings at the R level and then
dynamically compiled, loaded and interfaced.  Could be along the lines of
what you were thinking about with "building dynamic compilation and
execution options".

  - Also, it is always fun to drop by the Omegahat project
(www.omegahat.org) and see what Duncan Temple Lang has been cooking up. He
has a couple of packages for interfacing R with compiled code via LibFFI
(rather than the built in pointer method you observed) and one package that
has the beginnings of some LLVM bindings.


-Charlie
On 4/21/11 10:02 PM, "Sharpie" <chuck at sharpsteen.net> wrote:

            
-----
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University
--
View this message in context: http://r.789695.n4.nabble.com/FW-Rcpp-devel-Question-on-5-6-Interfacing-C-code-tp3465257p3468640.html
Sent from the R devel mailing list archive at Nabble.com.