2012-07-21 51 views
7

我正在嘗試使用Rcpp編寫一些在Windows中訪問某些操作系統級別的東西的C++代碼。只要我包含windows.hshlobj.h,我會收到一堆編譯錯誤。當我運行這個代碼時,它可以工作,所以我知道我正在獲得一些基礎知識。但是當我取消註釋與Windows相關的#include這兩行時,它不起作用。在特定於Windows的環境中使用Rcpp包含

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

注:當我在RStudio運行此,從coutprintf輸出出現在控制檯,但是當我從Windows RGUI運行,輸出不會出現。我認爲這與RGui處理文本輸出的方式有關。

當我取消這些包括行,我得到的錯誤是這樣的:

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] 

...等等

如何使這項工作任何提示?


更新:我設法弄到一些錯誤消失,但有些仍然存在。

我也按照從http://tolstoy.newcastle.edu.au/R/e2/devel/06/11/1242.html

inc一些建議得到了Realloc誤差應改爲:

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; 
' 

我也得到了通過傳遞-fpermissive編譯器排除其他錯誤的,因爲從這個問題: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') 

但是有AR Ë還存在一些誤區:

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 

回答

5

我想出了最後一個問題。看起來R和Windows頭文件都定義了ReallocFree,但是定義之間存在一些衝突。所以在包含Windows頭文件之前,我需要這兩個宏的#undef。並且還有將-fpermissive標誌傳遞給編譯器的問題。

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

在第一近似,則只能使用RCPP建立,如果你能有R自身建設成爲RCPP只是使API更好了很多C++膠模板魔法。

所以除非你讓這些頭文件在R程序中生成,否則我不會看到它是如何用Rcpp生成的。

+1

謝謝,你是對的 - 這看起來像R和Windows標題的一般問題。 – wch 2012-07-21 16:33:54

0

我有這些錯誤了。行599錯誤花了我很長時間來修復。我註釋掉了599行並解決了下面的問題。

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 

我不喜歡這個解決方案,但我的程序現在正在編譯。這樣做可能會有未來的問題,所以我記錄了我的變化。任何人有更好的解決方案

相關問題