[Rcpp-devel] Request for help with compiling Rcpp example

Jelmer Ypma jelmerypma at gmail.com
Thu Oct 13 18:40:40 CEST 2011


There are a couple of things:

* As far as I know, 'src' should not include a main() statement, nor
should it have #include statements (these can be added in the
'includes' argument in cxxfunction)

* The first argument in cxxfunction is sig. Since you don't supply an
explicitly named sig argument, src is passed on to sig, which gives
you the error. Solution: explicitly name your arguments, or supply
signature() as the first argument.

* Once you successfully compile your code, std::cout will not give you
any output (on Windows). You could use Rprintf instead.

Below are two version that compile and run  for me (but the first one
doesn't give the output).

Hope this helps,
Jelmer

library('inline')

src1 <- '
std::cout << "Hello World!" << std::endl;
std::cout << "Welcome to C++ Programming" << std::endl;
'

fx1 <- cxxfunction(sig = signature(), body = src1, plugin = "Rcpp",
verbose = T, includes = "#include <iostream>")


src2 <- '
	Rprintf("Hello World!\\n");
'

fx2 <- cxxfunction(body = src2, plugin = "Rcpp",verbose=T)


> fx1()
NULL
> fx2()
Hello World!
NULL

On Thu, Oct 13, 2011 at 17:15, Jian Kang <allenhahaha at gmail.com> wrote:
> Thanks for your reply.
> Here is the response of 'Hello World' program:
>> src='#include <iostream>
> + using namespace std;
> + void main()
> + {
> + cout << "Hello World!" << endl;
> + cout << "Welcome to C++ Programming" << endl;
> + }'
>>
>> fx <- cxxfunction(src,plugin = "Rcpp",verbose=T)
>  >> setting environment variables:
> PKG_LIBS =  C:/R/Rsoft/library/Rcpp/lib/i386/libRcpp.a
>  >> LinkingTo : Rcpp
> CLINK_CPPFLAGS =  -I"C:/R/Rsoft/library/Rcpp/include"
>  >> Program source :
>    1 :
>    2 : // includes from the plugin
>    3 :
>    4 : #include <Rcpp.h>
>    5 :
>    6 :
>    7 : #ifndef BEGIN_RCPP
>    8 : #define BEGIN_RCPP
>    9 : #endif
>   10 :
>   11 : #ifndef END_RCPP
>   12 : #define END_RCPP
>   13 : #endif
>   14 :
>   15 : using namespace Rcpp;
>   16 :
>   17 :
>   18 : // user includes
>   19 :
>   20 :
>   21 : // declarations
>   22 : extern "C" {
>   23 : SEXP file3a1c304e( ) ;
>   24 : }
>   25 :
>   26 : // definition
>   27 :
>   28 : SEXP file3a1c304e(  ){
>   29 : BEGIN_RCPP
>   30 :
>   31 : END_RCPP
>   32 : }
>   33 :
>   34 :
> Compilation argument:
>  C:/R/Rsoft/bin/i386/R CMD SHLIB file3a1c304e.cpp 2>
> file3a1c304e.cpp.err.txt
> g++ -I"C:/R/Rsoft/include"    -I"C:/R/Rsoft/library/Rcpp/include"      -O2
> -Wall  -c file3a1c304e.cpp -o file3a1c304e.o
> g++ -shared -s -static-libgcc -o file3a1c304e.dll tmp.def file3a1c304e.o
> C:/R/Rsoft/library/Rcpp/lib/i386/libRcpp.a -LC:/R/Rsoft/bin/i386 -lR
> cygwin warning:
>   MS-DOS style path detected: C:/R/Rsoft/etc/i386/Makeconf
>   Preferred POSIX equivalent is: /cygdrive/c/R/Rsoft/etc/i386/Makeconf
>   CYGWIN environment variable option "nodosfilewarning" turns off this
> warning.
>   Consult the user's guide for more details about POSIX paths:
>     http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
> Error in as.function.default(c(value, if (is.null(bd) || is.list(bd))
> list(bd) else bd),  :
>   invalid formal argument list for "function"
> Here, I also tried the test by Hadley,
> _devel()
> cygwin warning:
>   MS-DOS style path detected: C:/R/Rsoft/etc/i386/Makeconf
>   Preferred POSIX equivalent is: /cygdrive/c/R/Rsoft/etc/i386/Makeconf
>   CYGWIN environment variable option "nodosfilewarning" turns off this
> warning.
>   Consult the user's guide for more details about POSIX paths:
>     http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
> gcc -I"C:/R/Rsoft/include"    -I"C:/R/Rsoft/library/Rcpp/include"      -O3
> -Wall  -std=gnu99 -c foo.c -o foo.o
> gcc -shared -s -static-libgcc -o foo.dll tmp.def foo.o
> C:/R/Rsoft/library/Rcpp/lib/i386/libRcpp.a -LC:/R/Rsoft/bin/i386 -lR
> [1] TRUE
> Sorry that I don't really get it how to rebuild R packages from source.
> Thanks,
> Kent
>
> On Thu, Oct 13, 2011 at 5:11 PM, Hadley Wickham <hadley at rice.edu> wrote:
>>
>> > My best guess is that you have not installed the Rtools correctly.  Can
>> > you
>> > compile a simple 'hello, world' example in C or C++?  Can you rebuild R
>> > packages from source?
>>
>> I wrote an function to check that you have a valid R development
>> environment (with help from Simon Urbanek) for devtools.  See code
>> below - and check with has_devel()
>>
>> Hadley
>>
>>
>> has_devel <- function() {
>>  foo_path <- file.path(tempdir(), "foo.c")
>>
>>  cat("void foo(int *bar) { *bar=1; }\n", file = foo_path)
>>  on.exit(unlink(foo_path))
>>
>>  R("CMD SHLIB foo.c", tempdir())
>>  dylib <- file.path(tempdir(), paste("foo", .Platform$dynlib.ext, sep=''))
>>  on.exit(unlink(dylib), add = TRUE)
>>
>>  dyn.load(dylib)
>>  on.exit(dyn.unload(dylib), add = TRUE)
>>
>>  stopifnot(do.call(".C", list("foo",0L))[[1]] == 1L)
>>  TRUE
>> }
>>
>> R <- function(options, path = tempdir()) {
>>  r_path <- shQuote(file.path(R.home("bin"), "R"))
>>  in_dir(path, system_check(paste(r_path, options)))
>> }
>>
>> system_check <- function(cmd) {
>>  res <- system(cmd)
>>  if (res != 0) {
>>    stop("Command failed (", res, ")", call. = FALSE)
>>  }
>>  invisible(TRUE)
>> }
>>
>> in_dir <- function(dir, code) {
>>  cur <- getwd()
>>  setwd(dir)
>>  on.exit(setwd(cur))
>>
>>  force(code)
>> }
>>
>> --
>> Assistant Professor / Dobelman Family Junior Chair
>> Department of Statistics / Rice University
>> http://had.co.nz/
>> _______________________________________________
>> 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
>
>
> _______________________________________________
> 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
>


More information about the Rcpp-devel mailing list