我正在嘗試使用Rcpp編寫一些在Windows中訪問某些操作系統級別的東西的C++代碼。只要我包含windows.h
或shlobj.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運行此,從cout
和printf
輸出出現在控制檯,但是當我從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
謝謝,你是對的 - 這看起來像R和Windows標題的一般問題。 – wch 2012-07-21 16:33:54