2012-07-21 11 views
7

Estoy tratando de escribir algunos códigos C++ que acceden a algunos elementos del nivel del sistema operativo en Windows, utilizando Rcpp. Apenas incluyo windows.h o shlobj.h, aparece un montón de errores de compilación. Cuando ejecuto este código, funciona, así que sé que estoy obteniendo algunos de los conceptos básicos correctos. Pero cuando descomiento cualquiera de las líneas #include relacionadas con Windows, no funciona.Uso de Rcpp con Windows específico incluye

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() 

Nota: Cuando ejecuto esto en rstudio, la salida de cout y printf aparece en la consola, pero cuando lo ejecuto desde el Rgui de Windows, no aparece la salida. Supongo que esto tiene algo que ver con la forma en que RGui maneja la salida de texto.

Cuando Descomentar estos incluyen líneas, los errores que recibo este aspecto:

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] 

... y así sucesivamente

¿Alguna pista sobre cómo hacer este trabajo?


actualización: me las arreglé para conseguir algunos de los errores que se vaya, pero algunos permanecen.

También me dio de los Realloc errores siguiendo algunos consejos de http://tolstoy.newcastle.edu.au/R/e2/devel/06/11/1242.html

El inc debe sustituirse por:

inc <- ' 
#include <iostream> 
#include <stdio.h> 

// This is taken from http://tolstoy.newcastle.edu.au/R/e2/devel/06/11/1242.html 
#include <R.h> 
#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; 
' 

también me deshice de otros errores al pasar -fpermissive para el compilador, como De esta pregunta: 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) 
Sys.unsetenv('PKG_CXXFLAGS') 

Pero hay e todavía algunos errores:

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 

Respuesta

5

Descubrí el último problema. Parece que los encabezados R y Windows definen Realloc y Free, pero hay algunos conflictos entre las definiciones. Así que necesitaba #undef ambas macros antes de incluir los encabezados de Windows. Y también está la cuestión de pasar la bandera -fpermissive al compilador.

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() 
3

En una primera aproximación, sólo se pueden construir con RCPP si se puede construir con R como RCPP sólo hace que la API más agradable con una gran cantidad de C++ y pegamento mágico plantilla.

Así que, a menos que consigas estos encabezados para compilar en un programa con solo R, no veo cómo podría compilarse con Rcpp.

+1

Gracias, tienes razón: parece un problema general con los encabezados R y Windows. – wch

0

que tienen estos errores, también. Y el error de la línea 599 me llevó mucho tiempo solucionarlo. Comenté la línea 599 y solucioné el problema a continuación.

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 

No me gusta esta solución, pero mi programa se está compilando ahora. Puede haber problemas futuros al hacer esto, así que documenté mi cambio. Alguien tiene una mejor solución?

Cuestiones relacionadas