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

Sean Robert McGuffee sean.mcguffee at gmail.com
Thu Apr 21 17:38:42 CEST 2011


You are right, I looked and I did find the R source code. However, it's
largely written in R! I mean, I don't know how to trace the R code where
INSTALL is recognized and follow it to a c or c++ level command. For example
these are hits in .R files, not c files, and I don't know how to connect
what they do to c or c++ commands that I might understand:
R-2.13.0/src/:$grep '"INSTALL"' */*/*/*
library/tools/R/check.R:            args <- c("INSTALL", "-l",
shQuote(libdir), INSTALL_opts,
library/tools/R/install.R:                     "INSTALL", "--no-multiarch")
library/tools/R/install.R:                         "INSTALL",
"--no-multiarch")
library/utils/R/packages2.R:        if(!file.exists(file.path(R.home("bin"),
"INSTALL")))
in a c file that does recognize install, it runs a separate command, and I
have no idea how to trace that command to a c or c++ source code:
if (!strcmp(argv[cmdarg], "INSTALL")) {
    snprintf(cmd, CMD_LEN,
         "%s/%s/Rterm.exe -e tools:::.install_packages() R_DEFAULT_PACKAGES=
LC_COLLATE=C --no-restore --slave --args ",
         getRHOME(3), BINDIR);
    PROCESS_CMD("nextArg");
    }
If you could point me to the functions that are called a c or c++ level, I'd
love to see what R is doing for myself.
Thanks!
Sean


On 4/21/11 11:21 AM, "Joshua Ulrich" <josh.m.ulrich at gmail.com> wrote:

> Please, please, please read the documentation before sending more
> questions to the list.  You also have the source code, so you can look
> at what "R CMD build" and "R CMD INSTALL" are doing.
> --
> Joshua Ulrich  |  FOSS Trading: www.fosstrading.com
> 
> 
> 
> On Thu, Apr 21, 2011 at 10:16 AM, Sean Robert McGuffee
> <sean.mcguffee at gmail.com> wrote:
>> 
>> So, how is the package turning it's name into those commands?
>> Does the installation automatically list the src directory and iteratively
>> run a loop over each file and call 'R CMD SHLIB objectOfIterator' ?
>> The reason this is so important is because itšs easy to get things to work
>> via a terminal command 'R CMD SHLIB someSourceFile.cppš, so I want to be
>> able to take things that work that way and put them inside a package. It
>> seems peculiar to me that R needs to access functions through a C wrapper
>> too. Išm not sure why it canšt access C++ functions directly. What R is
>> doing is over my head because it is calling functions that were not compiled
>> into it. I mean, if I want to call a C or C++ function from C++ code, I have
>> to convince my compiler that I have a header and all definitions behind the
>> declarations in my source files to compile my own programs. I donšt know how
>> R works when the program is compiled way in advance and is then somehow
>> calling on declarations made later in another place. I think objective C/C++
>> allows for this type of thing where you can write code to call something
>> that is declared but not yet defined. However, Išm not sure what R is doing?
>> Is R doing the same thing a compiler would do and creating itšs own binary
>> instructions for the launch of a function, or is it creating a new
>> executable and launching that as itšs own application and then somehow
>> communicating with it?
>> Sean
>> 
>> 
>> On 4/21/11 7:52 AM, "Dirk Eddelbuettel" <edd at debian.org> wrote:
>> 
>>> 
>>> On 21 April 2011 at 07:16, Duncan Murdoch wrote:
>>> | On 11-04-20 11:33 AM, Sean Robert McGuffee wrote:
>>> | > Hi, apparently I sent my question about using R and C++ to the wrong
>>> list,
>>> | > ironically seeing as that list was called Rcpp. Anyway, I was directed
>>> to
>>> | > post my question here. To summarize my current question, I have found
>>> two
>>> | > commands that I want to be able to put into a package. The commands are
>>> 'R
>>> | > CMD SHLIB X.cc X_main.cc' and
>>> | > 'dyn.load(paste("X",.Platform$dynlib.ext,sep="")),' which I would like
>>> to
>>> | > run when my package is installed and maybe have the second command run
>>> again
>>> | > when my package is to be used. I've been trying to figure out the
>>> | > documentation and learn through examples, but I'm just not getting it
>>> and
>>> | > have been trying for weeks.
>>> | > Does anyone on this site have any suggestions for me?
>>> |
>>> | Assuming those lines work on their own, just do the following:
>>> |
>>> | 1.  Put those *.cc files into the src directory of your package.  (You
>>> | may need to create it.)
>>> |
>>> | 2.  Put useDynLib(foo) into the NAMESPACE file of your foo package.
>>> |
>>> | 3.  Call those functions using .C("X", args, PACKAGE="foo").
>>> |
>>> | That's it.
>>> 
>>> We told Sean this twice or three times already over in this thread
>>> 
>>>   http://thread.gmane.org/gmane.comp.lang.r.rcpp/1808
>>> 
>>> but the message does not seem to sink in.  He keeps asking where to put 'R
>>> CMD SHLIB' and doesn't seem to hear when we say there is none in a
>>> package...
>>> 
>>> Dirk
>>> 
>>> | Duncan Murdoch
>>> |
>>> | > Thanks, Sean
>>> | >
>>> | > |On 20 April 2011 at 10:20, Sean Robert McGuffee wrote:
>>> | > |
>>> | > |
>>> | > | Hi, thanks!
>>> | > |
>>> | > |>On 4/20/11 10:03 AM, "Steve Lianoglou"<mailinglist.honeypot at gmail.com>
>>> | > wrote:
>>> | > |>  Hi,
>>> | > |>
>>> | > |>  On Wed, Apr 20, 2011 at 9:49 AM, Sean Robert McGuffee
>>> | > |>  <sean.mcguffee at gmail.com>  wrote:
>>> | > |>>  Hi, I have a quick couple of questions about some of the
>>> documentation
>>> | > on
>>> | > |>>  the web page:
>>> | > |>>
>>> | >
>>> http://cran.r-project.org/doc/manuals/R-exts.html#Linking-GUIs-and-other-fro
>>> | > n
>>> | > |>>  t_002dends-to-R
>>> | > |>>  under the heading:
>>> | > |>>  5.6 Interfacing C++ code
>>> | > |>>
>>> | > |>>  Question 1:
>>> | > |>>  If Išm at a terminal, I can type the instructions they suggest:
>>> | > |>>  R CMD SHLIB X.cc X_main.cc
>>> | > |>>  If I wanted a package to do this, how would I tell the package to
>>> do
>>> | > that
>>> | > |>>  same thing?
>>> | > |>
>>> | > |>  Just to make sure we're all on the same page, you want an R package
>>> to
>>> | > |>  compile some source code into a shared library/dll from inside R?
>>> | > |>
>>> | > |>  Not sure if there's a "baked in" way for that to happen, but maybe
>>> you
>>> | > |>  can invoke `R CMD WHATEVER` from inside R using the `system`
>>> function:
>>> | > |>
>>> | > |>  R>  ?system
>>> | > |>
>>> | > |
>>> | > | ok, so where in the package would I put the system call in the package
>>> to
>>> | > | have it run when installing the package?
>>> | >
>>> | >> You don't. As I said, 'R CMD INSTALL' et all do that.
>>> | >> Download an existing package with source, install it.  Study its
>>> sources,
>>> | >> study the 'Writing R Extensions' manual.  Ask on r-devel.
>>> | >> Basic R questions are off-topic here.
>>> | >
>>> | > |>>  Would I use the same command and just include it in a file
>>> somewhere
>>> in
>>> | > the
>>> | > |>>  package?
>>> | > |>>  If so, which file?
>>> | > |>
>>> | > |>  Hmm ... I'm curious what you're trying to do, exactly?
>>> | > |
>>> | > | I'm trying to figure out how take commands such as " R CMD SHLIB X.cc
>>> | > | X_main.cc" followed by "dyn.load(paste("X", .Platform$dynlib.ext, sep
>>> =
>>> | > | ""))," which are commands I can get to work for myself as a human
>>> | > | interactively, and put the commands into a package to be automatically
>>> run
>>> | > | when installing the package. I mean, it's great if I can compile a c++
>>> | > file
>>> | > | and then use it inside R, but I'm only doing that so I can let other
>>> | > people
>>> | > | do that via a package. As much as I read this documentation, I keep
>>> | > missing
>>> | >
>>> | >> Again, I like working from an existing, working package. As I said,
>>> there
>>> are
>>> | >> almost 1000 to pick from.
>>> | >> Please direct follow-ups that have no bearing on Rcpp to r-devel.
>>> | >> Dirk
>>> | >
>>> | > I've tried to figure this out for weeks by looking at other packages and
>>> | > reading the confusing and nonintegrated documentation, but it hasn't
>>> taught
>>> | > me how to put the two commands into a package so that they are run when
>>> the
>>> | > package is installed. I'm simply trying to find out where in my package
>>> I
>>> | > should put the commands 'R CMD SHLIB X.cc X_main.cc' and
>>> | > 'dyn.load(paste("X",.Platform$dynlib.ext,sep="")),'
>>> | > in order to have them run when my package is installed.
>>> | >
>>> | >
>>> | > | the connections between the different sections. This is a section I am
>>> | > | loving because it works very well. Thus, I want to figure out how to
>>> take
>>> | > | the baby steps I'm doing and combine them into a package.
>>> Specifically,
>>> I
>>> | > | want to take these two commands and insert them into a package so that
>>> | > these
>>> | > | commands will compile my code and make a dynamic ".so" file where R
>>> can
>>> | > | access its functions when others install my package.
>>> | > |
>>> | > |>
>>> | > |>>  Question 2:
>>> | > |>>  dyn.load(paste("X", .Platform$dynlib.ext, sep = ""))
>>> | > |>>
>>> | > |>>  Where does .Platform$dynlib.ext come from?
>>> | > |>>  What does it mean?
>>> | > |>>  What do itšs components .Platform and $dynlib and .ext mean?
>>> | > |>
>>> | > |>  .Platform is lust a normal list -- it is defined internally (I
>>> guess).
>>> | > |>  You can access "named" elements of a list with `$`.
>>> | > |>
>>> | > |>  .Platform$dynlyb (or .Platform[['dynlib']]) tells you the extension
>>> | > |>  your particular system uses for shared libraries:
>>> | > |>
>>> | > |>  R>  .Platform
>>> | > |>  $OS.type
>>> | > |>  [1] "unix"
>>> | > |>
>>> | > |>  $file.sep
>>> | > |>  [1] "/"
>>> | > |>
>>> | > |>  $dynlib.ext
>>> | > |>  [1] ".so"
>>> | > |>
>>> | > |>  $GUI
>>> | > |>  [1] "X11"
>>> | > |>
>>> | > |>  $endian
>>> | > |>  [1] "little"
>>> | > |>
>>> | > |>  $pkgType
>>> | > |>  [1] "mac.binary.leopard"
>>> | > |>
>>> | > |>  $path.sep
>>> | > |>  [1] ":"
>>> | > |>
>>> | > |>  $r_arch
>>> | > |>  [1] "x86_64"
>>> | > |>
>>> | > |>  See ?.Platform for more help.
>>> | > |
>>> | > | Ah, thanks, that clarifies exactly what .Platform$dynlib.ext is, it's
>>> | > ".so"
>>> | > | on my system.
>>> | > |
>>> | > | This, the dyn.load(paste("X", .Platform$dynlib.ext, sep = "")) is
>>> | > equivalent
>>> | > | to the command dyn.load("X.so) which now makes sense in that context!
>>> | > |
>>> | > |
>>> | > | _______________________________________________
>>> | > | 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
>>> | >
>>> |
>>> | ______________________________________________
>>> | R-devel at r-project.org mailing list
>>> | https://stat.ethz.ch/mailman/listinfo/r-devel
>> 
>> 
>>        [[alternative HTML version deleted]]
>> 
>> 
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>> 
>> 



More information about the R-devel mailing list