From jelkink at gmail.com  Wed Apr  4 20:46:31 2007
From: jelkink at gmail.com (Jos Elkink)
Date: Wed, 4 Apr 2007 14:46:31 -0400
Subject: [Rd] Accessing C++ code from R
In-Reply-To: <5C9CB370-AC77-4A0C-A081-433AEC58E1C3@r-project.org>
References: <cc06ac9d0704041044l273113ecn5c6d6153ba3cfbeb@mail.gmail.com>
	<5C9CB370-AC77-4A0C-A081-433AEC58E1C3@r-project.org>
Message-ID: <cc06ac9d0704041146j30808556ta5a50a3a40a5ad89@mail.gmail.com>

Hi Simon,

Thank you - that indeed fixes the first problem (about cout). It still
leaves the second error, though:

main.cpp:52: error: brace-enclosed initializer used to initialize
'R_NativePrimitiveArgType*'

Any ideas about that?

Thanks again for the first suggestion, anyway, and I'll keep your
point about cout in mind.

Jos

On 4/4/07, Simon Urbanek <simon.urbanek at r-project.org> wrote:
> Jos,
>
> you omitted the crucial part but my guess would be that all you have
> to do is to put #include <iostream> *before* R headers includes (R
> defines "length" which throws off iostream).
> Also note that using cout is not a good idea, because it is not
> captured by R, you should be using Rprintf instead.
>
> Cheers,
> Simon
>
> On Apr 4, 2007, at 1:44 PM, Jos Elkink wrote:
>
> > Hi,
> >
> > I am trying to use existing C++ code from R. I have no problems
> > compiling C code and using it in R, but with C++ I'm running into
> > problems.
> >
> > Here's the compiler output:
> >
> > Macintosh-10:~/Desktop/dissertation/Model - CPP version/R labguest$
> > g++ -I/Library/Frameworks/R.framework/Resources/include
> > -I/Library/Frameworks/R.framework/Resources/include/i386 *.cpp
> > In file included from /usr/include/c++/4.0.0/bits/locale_facets.h:
> > 1533,
> >                  from /usr/include/c++/4.0.0/bits/basic_ios.h:44,
> >                  from /usr/include/c++/4.0.0/ios:50,
> >                  from /usr/include/c++/4.0.0/ostream:44,
> >                  from /usr/include/c++/4.0.0/iostream:44,
> >                  from main.cpp:5:
> > /usr/include/c++/4.0.0/bits/codecvt.h:217:45: error: macro "length"
> > passed 4 arguments, but takes just 1
> > /usr/include/c++/4.0.0/bits/codecvt.h:216: error: expected ';'
> > before 'const'
> > /usr/include/c++/4.0.0/bits/codecvt.h:220: error: expected `;'
> > before 'int'
> > main.cpp:51: error: brace-enclosed initializer used to initialize
> > 'R_NativePrimitiveArgType*'
> >
> >
> > Line 51 refers to the standard part to register the function in R:
> >
> > R_CMethodDef cMethods[] = {
> >   {"runSimulation", (void* (*) ()) &runSimulation, 27, {INTSXP,
> > INTSXP, INTSXP, REALSXP, REALSXP, REALSXP, INTSXP, INTSXP, INTSXP,
> > INTSXP, INTSXP, INTSXP, REALSXP, REALSXP, INTSXP, INTSXP, INTSXP,
> > INTSXP, INTSXP, INTSXP, INTSXP, INTSXP, INTSXP, INTSXP, REALSXP,
> > REALSXP, REALSXP}},
> >   {NULL, NULL, 0}
> > };
> >
> >
> > I had to add the explicit cast of &runSimulation to get rid of one
> > compiler error, but do not know how to deal with this one.
> > Encapsulating this part of the code in
> >
> > extern "C" {
> > }
> >
> > has no effect.
> >
> > runSimulation is just a regular function, with a lot of int* and
> > double* parameters, and it is not part of a class. It does inside
> > create a class, though.
> >
> > Outline:
> >
> > void runSimulation(int *p_1 ... etc) {
> >
> >    try {
> >      CModel Model();
> >      Model.Run();
> >    } catch (CException& e) {
> >      cout << "exception" << endl;
> >    }
> > }
> >
> > Encapsulating this function in extern "C" also has no effect.
> >
> > Anyone any ideas what I should do? The system I am using is an
> > Intel-based Mac.
> >
> > Thanks in advance!
> >
> > Jos
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
> >
>
>


-- 
Jos Elkink
PhD Candidate, Trinity College Dublin
HMDC Fellow, Harvard University
http://jaeweb.cantr.net



[Rd] Accessing C++ code from R
Jos Elkink