Using Rcpp with Windows-specific includes

I am trying to write some C ++ code that accesses some OS levels in Windows using Rcpp. As soon as I turn on windows.h or shlobj.h , I get a bunch of compilation errors. When I run this code, it works, so I know that I understand some basics correctly. But when I uncomment one of the #include lines related to Windows, it will not work.

 library(inline) inc <- ' #include <iostream> #include <stdio.h> // #include <windows.h> // #include <shlobj.h> using namespace std; ' src <- ' cout << "foo\\n"; printf("foo2\\n"); return Rcpp::wrap(20); ' fun <- cxxfunction(signature(), includes = inc, src, plugin="Rcpp") fun() 

Note. When I run this in RStudio, the output from cout and printf appears in the console, but when I launch it from Windows RGui, the output does not appear. I assume this is due to the way RGui handles text output.

When I uncomment those included lines, the errors I get are as follows:

 In file included from c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/include/objbase.h:154:0, from c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/include/ole2.h:16, from c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/include/windows.h:94, from file43c2f9e3518.cpp:22: c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/include/objidl.h:598:52: error: macro "Realloc" requires 3 arguments, but only 2 given c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/include/objidl.h:598:56: error: ISO C++ forbids initialization of member 'Realloc' [-fpermissive] c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/include/objidl.h:598:56: error: making 'Realloc' static [-fpermissive] 

... etc.

Any tips on how to make this work?


Update . I managed to remove some of the errors, but some of them remain.

I also got Realloc errors following the advice from http://tolstoy.newcastle.edu.au/R/e2/devel/06/11/1242.html

inc should be replaced by:

 inc <- ' #include <iostream> #include <stdio.h> // This is taken from http://tolstoy.newcastle.edu.au/R/e2/devel/06/11/1242.html #include <Rh> #undef Realloc #define R_Realloc(p,n,t) (t *) R_chk_realloc( (void *)(p), (size_t)((n) * sizeof(t)) ) #include <shlobj.h> using namespace std; ' 

I also got rid of other errors by passing -fpermissive compiler, as from this question: How to install g ++ flag compilers using Rcpp and inline?

 settings <- getPlugin("Rcpp") settings$env$PKG_CXXFLAGS <- paste('-fpermissive',settings$env$PKG_CXXFLAGS,sep=' ') fun <- cxxfunction(signature(), includes = inc, src, plugin = "Rcpp", settings = settings) Sys.unsetenv('PKG_CXXFLAGS') 

But there are some more errors:

 In file included from c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/include/objbase.h:154:0, from c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/include/ole2.h:16, from c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/include/shlobj.h:86, from file43c267d3279.cpp:26: c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/include/objidl.h:599:25: error: expected identifier before '(' token c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/include/objidl.h:599:25: error: 'parameter' declared as function returning a function c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/include/objidl.h:599:25: error: expected ')' before ',' token 
+7
source share
3 answers

I found out the last problem. Realloc R and Windows Realloc define Realloc and Free , but there is some conflict between the definitions. So I needed #undef both of these macros before including the Windows headers. And also the issue of passing the -fpermissive flag -fpermissive compiler.

 library(Rcpp) library(inline) inc <- ' // Taken from http://tolstoy.newcastle.edu.au/R/e2/devel/06/11/1242.html // Undefine the Realloc macro, which is defined by both R and by Windows stuff #undef Realloc // Also need to undefine the Free macro #undef Free #include <windows.h> #include <iostream> #include <stdio.h> using namespace std; ' src <- ' cout << "foo\\n"; printf("foo2\\n"); return Rcpp::wrap(20); ' # Need this for the Windows headers to work # Set -fpermissive, from: http://stackoverflow.com/questions/7063265/how-to-set-g-compiler-flags-using-rcpp-and-inline settings <- getPlugin("Rcpp") settings$env$PKG_CXXFLAGS <- paste('-fpermissive',settings$env$PKG_CXXFLAGS,sep=' ') fun <- cxxfunction(signature(), includes = inc, src, plugin = "Rcpp", settings = settings) fun() 
+5
source

In a first approximation, you can only build using Rcpp, if you can build with R itself, since Rcpp just makes the API more enjoyable with lots of glue and a C ++ template.

Therefore, if you do not earn these headers in the program with just R, I don’t see how it could be created with Rcpp.

+3
source

I also have these errors. And the error of line 599 took me a long time to fix. I commented on line 599 and fixed the problem below.

 c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64- mingw32/include/objidl.h:599:25: error: expected identifier before '(' token c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/include/objidl.h:599:25: error: 'parameter' declared as function returning a function c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../i686-w64-mingw32/include/objidl.h:599:25: error: expected ')' before ',' token 

I do not like this solution, but my program is compiling now. This may be due to future problems, so I documented my changes. Does anyone have a better solution?

0
source

All Articles