Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Rcpp with Windows-specific includes
    primarykey
    data
    text
    <p>I'm trying to write some C++ code that accesses some OS-level things in Windows, using Rcpp. As soon as I include <code>windows.h</code> or <code>shlobj.h</code>, I get a bunch of compilation errors. When I run this code, it works, so I know I'm getting some of the basics right. But when I uncomment either of the Windows-related <code>#include</code> lines, it doesn't work.</p> <pre><code>library(inline) inc &lt;- ' #include &lt;iostream&gt; #include &lt;stdio.h&gt; // #include &lt;windows.h&gt; // #include &lt;shlobj.h&gt; using namespace std; ' src &lt;- ' cout &lt;&lt; "foo\\n"; printf("foo2\\n"); return Rcpp::wrap(20); ' fun &lt;- cxxfunction(signature(), includes = inc, src, plugin="Rcpp") fun() </code></pre> <p>Note: When I run this in RStudio, the output from <code>cout</code> and <code>printf</code> appear in the console, but when I run it from the Windows RGui, the output doesn't appear. I assume this has something to do with the way RGui handles text output.</p> <p>When I uncomment those include lines, the errors I get look like this:</p> <pre><code>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] </code></pre> <p>... and so on</p> <p>Any hints on how to make this work?</p> <hr> <p><strong>Update</strong>: I managed to get some of the errors to go away, but some remain.</p> <p>I also got of the <code>Realloc</code> errors by following some advice from <a href="http://tolstoy.newcastle.edu.au/R/e2/devel/06/11/1242.html" rel="nofollow noreferrer">http://tolstoy.newcastle.edu.au/R/e2/devel/06/11/1242.html</a></p> <p>The <code>inc</code> should be replaced with:</p> <pre><code>inc &lt;- ' #include &lt;iostream&gt; #include &lt;stdio.h&gt; // This is taken from http://tolstoy.newcastle.edu.au/R/e2/devel/06/11/1242.html #include &lt;R.h&gt; #undef Realloc #define R_Realloc(p,n,t) (t *) R_chk_realloc( (void *)(p), (size_t)((n) * sizeof(t)) ) #include &lt;shlobj.h&gt; using namespace std; ' </code></pre> <p>I also got rid of other errors by passing <code>-fpermissive</code> to the compiler, as from this question: <a href="https://stackoverflow.com/questions/7063265/how-to-set-g-compiler-flags-using-rcpp-and-inline">How to set g++ compiler flags using Rcpp and inline?</a></p> <pre><code>settings &lt;- getPlugin("Rcpp") settings$env$PKG_CXXFLAGS &lt;- paste('-fpermissive',settings$env$PKG_CXXFLAGS,sep=' ') fun &lt;- cxxfunction(signature(), includes = inc, src, plugin = "Rcpp", settings = settings) Sys.unsetenv('PKG_CXXFLAGS') </code></pre> <p>But there are still some errors:</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload