<font face="arial,helvetica,sans-serif">Okay, very glad to hear you've got things working!</font><div><font face="arial,helvetica,sans-serif"><br></font></div><div><font face="arial,helvetica,sans-serif">J.J.<br></font><br>
<div class="gmail_quote">On Fri, Apr 26, 2013 at 3:35 AM, Finlay Scott <span dir="ltr"><<a href="mailto:drfinlayscott@gmail.com" target="_blank">drfinlayscott@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div><div><div><div><div>Fixed it.<br></div>It was me making a stupid mistake. I had:<br><br>PKG_CXXFLAGS=-I../inst/include<br><br></div>in the Makevars when I should have had<br><br>PKG_CPPFLAGS=-I../inst/include<br>
<br></div>After using J.J.'s suggestion of renaming the header files, the package now compiles and my minimal test now works.<br><br></div>Thanks to everyone for responding quickly.<span class="HOEnZb"><font color="#888888"><br>
<br></font></span></div><span class="HOEnZb"><font color="#888888">Finlay<br><div><div><div>
<div><br></div></div></div></div></font></span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Apr 25, 2013 at 12:28 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><br>
On 25 April 2013 at 10:47, Finlay Scott wrote:<br>
| Thanks for the reply. I followed your suggestion and changed the name of the<br>
| header file in /inst/include to 'asWrapExample.h' (the name of the package). I<br>
| then changed the #include in the 'DummyClass_example.cpp' file to reflect that<br>
| change. I also added:<br>
|<br>
| PKG_CXXFLAGS=-I../inst/include<br>
|<br>
| To the top of the Makevars file. However, I still get the same error. I have<br>
| the feeling I'm missing something obvious. Thanks for the help so far.<br>
<br>
</div>Seems you are "just" having issues with package mechanics.<br>
<br>
You could try to look at an existing package as eg out RcppGSL which has had<br>
this working for several years. Try comparing all the relevant files<br>
mentioned in the corresponding vignette Rcpp-extending.<br>
<br>
Dirk<br>
<div><div><br>
| Yours<br>
|<br>
| Finlay<br>
|<br>
|<br>
|<br>
| On Wed, Apr 24, 2013 at 5:41 PM, JJ Allaire <<a href="mailto:jj.allaire@gmail.com" target="_blank">jj.allaire@gmail.com</a>> wrote:<br>
|<br>
| Hi Finlay,<br>
|<br>
| If you name your include file the same name as your package then it will be<br>
| included automatically in RcppExports.cpp. The convention at work here is<br>
| that any custom as/wrap handlers should be accumulated (or referenced from)<br>
| that single file. This mirrors the existing convention used by Rcpp,<br>
| RcppArmadillo, RcppEigen, etc. to have a single global header file for<br>
| their C++ API.<br>
|<br>
| J.J.<br>
|<br>
|<br>
| On Wed, Apr 24, 2013 at 8:22 AM, Finlay Scott <<a href="mailto:drfinlayscott@gmail.com" target="_blank">drfinlayscott@gmail.com</a>><br>
| wrote:<br>
|<br>
| Hi<br>
| First of all I want to say how impressed I am with Rcpp. I think it is<br>
| going to be very useful for some of the packages I am developing. Thank<br>
| you very much for developing it.<br>
|<br>
| I have a question regarding writing custom as and wrap functions for my<br>
| own classes. Following the example in:<br>
|<br>
| <a href="http://gallery.rcpp.org/articles/custom-as-and-wrap-example/" target="_blank">http://gallery.rcpp.org/articles/custom-as-and-wrap-example/</a><br>
|<br>
| I can get my own minimal example to work with a very simple class, and<br>
| using the sourceCpp() function.<br>
|<br>
| The cpp code saved as a *.cpp file:<br>
|<br>
| #include <RcppCommon.h><br>
|<br>
| class DummyClass {<br>
| public:<br>
| double value;<br>
| };<br>
|<br>
| namespace Rcpp {<br>
| // non-intrusive extension via template specialisation<br>
| template <> DummyClass as(SEXP dt);<br>
| // non-intrusive extension via template specialisation<br>
| template <> SEXP wrap(const DummyClass &d);<br>
| }<br>
|<br>
| #include <Rcpp.h><br>
|<br>
| // define template specialisations for as and wrap<br>
| namespace Rcpp {<br>
| template <> DummyClass as(SEXP dtsexp) {<br>
| S4 dc_s4 = Rcpp::as<S4>(dtsexp);<br>
| DummyClass dc;<br>
| dc.value = dc_s4.slot("value");<br>
| return dc;<br>
| }<br>
|<br>
| template <> SEXP wrap(const DummyClass &d) {<br>
| Rcpp::S4 dc_s4("DummyClass");<br>
| dc_s4.slot("value") = d.value;<br>
| return Rcpp::wrap(dc_s4);<br>
| }<br>
| }<br>
|<br>
| // [[Rcpp::export]]<br>
| DummyClass test_as_wrap(DummyClass dc, double multiplier){<br>
| DummyClass dc_out;<br>
| dc_out.value = dc.value * multiplier;<br>
| return dc_out;<br>
| }<br>
|<br>
|<br>
| And the following R code compiles and calls the function:<br>
|<br>
| library(Rcpp)<br>
| sourceCpp("DummyClass_example.cpp")<br>
| setClass("DummyClass", representation(value = "numeric"))<br>
| dc <- new("DummyClass")<br>
| dc@value <- 23<br>
| test_as_wrap(dc, 4)<br>
|<br>
| This works just fine (like magic!) and the test_as_wrap() function is<br>
| happily called from R and returns an object of type DummyClass. I want<br>
| to use a similar approach in a package, so I made a minimal package<br>
| using:<br>
|<br>
| Rcpp.package.skeleton("asWrapExample",attributes=TRUE)<br>
|<br>
| I then split my original cpp file above into header and source code<br>
| files. In the /inst/include directory I placed a file<br>
| 'DummyClass_example.h' which has:<br>
|<br>
| #include <RcppCommon.h><br>
|<br>
| class DummyClass {<br>
| public:<br>
| double value;<br>
| };<br>
|<br>
| namespace Rcpp {<br>
| // non-intrusive extension via template specialisation<br>
| template <> DummyClass as(SEXP dt);<br>
| // non-intrusive extension via template specialisation<br>
| template <> SEXP wrap(const DummyClass &d);<br>
| }<br>
|<br>
| In the /src directory I placed a file 'DummyClass_example.cpp' which<br>
| has:<br>
|<br>
| #include "../inst/include/DummyClass_example.h"<br>
| #include <Rcpp.h><br>
|<br>
| // define template specialisations for as and wrap<br>
| namespace Rcpp {<br>
| template <> DummyClass as(SEXP dtsexp) {<br>
| S4 dc_s4 = Rcpp::as<S4>(dtsexp);<br>
| DummyClass dc;<br>
| dc.value = dc_s4.slot("value");<br>
| return dc;<br>
| }<br>
|<br>
| template <> SEXP wrap(const DummyClass &d) {<br>
| Rcpp::S4 dc_s4("DummyClass");<br>
| dc_s4.slot("value") = d.value;<br>
| return Rcpp::wrap(dc_s4);<br>
| }<br>
| }<br>
|<br>
| // [[Rcpp::export]]<br>
| DummyClass test_as_wrap(DummyClass dc, double multiplier){<br>
| DummyClass dc_out;<br>
| dc_out.value = dc.value * multiplier;<br>
| return dc_out;<br>
| }<br>
|<br>
| When I try to compile the package I get this error message:<br>
|<br>
| RcppExports.cpp:9:1: error: 'DummyClass' does not name a type<br>
|<br>
| This is probably caused by the RcppExports.cpp not having an #include<br>
| for my DummyClass_example.h.<br>
| I understand the RcppExports.cpp file is automatically generated by the<br>
| magic of Rcpp so there is no point in adding it there by hand.<br>
| I've looked at the documentation but it is not clear to me how I can<br>
| tell RcppExports to also include my header file (if this is the source<br>
| of the problem).<br>
| Have I missed something in the documentation, or is there an example I<br>
| can follow?<br>
| Any help is appreciated.<br>
|<br>
| Yours<br>
|<br>
| Finlay<br>
|<br>
|<br>
|<br>
|<br>
|<br>
| _______________________________________________<br>
| Rcpp-devel mailing list<br>
| <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
| <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
|<br>
|<br>
|<br>
|<br>
|<br>
</div></div>| ----------------------------------------------------------------------<br>
<div><div>| _______________________________________________<br>
| Rcpp-devel mailing list<br>
| <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
| <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</div></div><span><font color="#888888">--<br>
Dirk Eddelbuettel | <a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a> | <a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a><br>
</font></span></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br></blockquote></div><br></div>