2012-07-21 6 views
7

Rcppを使用して、Windowsの一部のOSレベルのものにアクセスする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でこれを実行すると、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 eは、まだいくつかのエラー:第一近似で

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と定義されているように見えますが、定義の間には多少の矛盾があります。だから私は#undefの両方のマクロをWindowsヘッダーを含める前にする必要がありました。また、-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がちょうどC++用接着剤とテンプレートの魔法の多くのよりよいAPIを作るようあなたはR自体を構築することができれば、あなただけのRcppを構築することができます。

これらのヘッダーをR単独のプログラムでビルドしない限り、Rcppでどのように構築できるかはわかりません。

+1

ありがとうございます。これはRヘッダーとWindowsヘッダーの一般的な問題のようです。 – wch

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 

私はこの解決策が嫌いですが、私のプログラムは現在コンパイル中です。これを行うことによって将来の問題が発生する可能性があるので、変更内容を文書化しました。誰もがより良い解決策を持っていますか?

関連する問題