Using Rcpp with Windows-specific includes

怎甘沉沦 提交于 2019-12-03 12:59:13

I figured out the last problem. It looks like both the R and Windows headers define Realloc and Free, but there's some conflict between the definitions. So I needed to #undef both of those macros before including the Windows headers. And there's also the matter of passing the -fpermissive flag to the 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()

At a first approximation, you can only build with Rcpp if you can build with R itself as Rcpp just makes the API nicer with a lot of C++ glue and template magic.

So unless you get these headers to build in a program with R alone, I don't see how it could build with Rcpp.

Kaiyu

I have these errors, too. And the line 599 error took me a long time to fix. I commented out the 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 don't like this solution, but my program is compiling now. There might be future problems by doing this, so I documented my change. Anyone has better solution?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!