2012-07-21 27 views
7

मैं कुछ सी ++ कोड लिखने की कोशिश कर रहा हूं जो आरसीपीपी का उपयोग करते हुए विंडोज़ में कुछ ओएस-स्तरीय चीजों को एक्सेस करता है। जैसे ही मैं windows.h या shlobj.h शामिल करता हूं, मुझे संकलन त्रुटियों का एक गुच्छा मिलता है। जब मैं यह कोड चलाता हूं, यह काम करता है, इसलिए मुझे पता है कि मुझे कुछ मूलभूत बातें मिल रही हैं। लेकिन जब मैं विंडोज़ से संबंधित #include लाइनों में से किसी एक को अपूर्ण करता हूं, तो यह काम नहीं करता है।विंडोज-विशिष्ट के साथ आरसीपीपी का उपयोग

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 से उत्पादन कंसोल में नहीं दिखाई, लेकिन जब मैं विंडोज RGui से चलाने, उत्पादन प्रकट नहीं होता है। मुझे लगता है कि आरजीईई पाठ आउटपुट को संभालने के तरीके के साथ ऐसा कुछ करने के लिए है।

जब मैं uncomment उन पंक्तियों में शामिल हैं, त्रुटियों मैं इस तरह दिखेगा:

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

मुझे अंतिम समस्या का पता चला। ऐसा लगता है कि आर और विंडोज हेडर दोनों Realloc और Free को परिभाषित करते हैं, लेकिन परिभाषाओं के बीच कुछ संघर्ष है। इसलिए मुझे विंडोज हेडर समेत उन मैक्रोज़ में #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 साथ निर्माण के रूप में Rcpp सिर्फ सी ++ गोंद और टेम्पलेट जादू का एक बहुत साथ अच्छे एपीआई बनाता है यदि आप आर के साथ ही निर्माण कर सकते हैं कर सकते हैं।

तो जब तक आप अकेले आर के साथ प्रोग्राम में इन शीर्षकों को बनाने के लिए नहीं मिलते हैं, तो मुझे नहीं लगता कि यह आरसीपीपी के साथ कैसे बनाया जा सकता है।

+1

धन्यवाद, आप सही हैं - यह आर और विंडोज हेडर के साथ एक सामान्य समस्या की तरह दिखता है। – wch

0

मेरे पास ये त्रुटियां भी हैं। और लाइन 59 9 त्रुटि ने मुझे ठीक करने में काफी समय लगाया। मैंने लाइन 59 9 पर टिप्पणी की और नीचे दी गई समस्या को ठीक किया।

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 

मुझे यह समाधान पसंद नहीं है, लेकिन मेरा प्रोग्राम अब संकलित कर रहा है। ऐसा करके भविष्य की समस्याएं हो सकती हैं, इसलिए मैंने अपना परिवर्तन दस्तावेज किया। किसी के पास बेहतर समाधान है?

संबंधित मुद्दे