I am trying to build some simple test cases of Rcpp. One of the simplest I can think of is below.
#include <Rcpp.h>
int main ()
{
auto i = Rcpp::IntegerVector::create(1,2);
}
This compiles fine and links against libR, but segmentation faults when run. My test system is a Mac with clang++.
Is there an obvious reason this should not work?
Cheers,
Brook
[Rcpp-devel] Segmentation faults
9 messages · Brook Milligan, Balamuta, James Joseph, Qiang Kou +1 more
Greetings and Salutations Brook,
The segfault is related to `int main() {}`. There is no need to use `int main() {}` unless you want to make this standalone in which case you want to use RInside to embed R within a C++ application.
Rcpp enables embedding C++ within R. So, R already is using its own `int main() {}`. Modifying code to the following should work:
In C++
test_fun.cpp
#include <Rcpp.h>
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
void test_fun()
{
auto i = Rcpp::IntegerVector::create(1,2);
Rcpp::Rcout << "Hello World!" << std::endl;
}
In R
Rcpp::sourceCpp("path/to/test_fun.cpp")
test_fun()
# Hello World!
For other examples, consider looking at examples from the introduction paper:
https://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-introduction.pdf
Sincerely,
JJB
On 12/6/19, 3:48 PM, "Rcpp-devel on behalf of Brook Milligan" <rcpp-devel-bounces at lists.r-forge.r-project.org on behalf of brook at nmsu.edu> wrote:
I am trying to build some simple test cases of Rcpp. One of the simplest I can think of is below.
#include <Rcpp.h>
int main ()
{
auto i = Rcpp::IntegerVector::create(1,2);
}
This compiles fine and links against libR, but segmentation faults when run. My test system is a Mac with clang++.
Is there an obvious reason this should not work?
Cheers,
Brook
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel at lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
On Dec 6, 2019, at 3:00 PM, Balamuta, James Joseph <balamut2 at illinois.edu> wrote:
The segfault is related to `int main() {}`. There is no need to use `int main() {}` unless you want to make this standalone in which case you want to use RInside to embed R within a C++ application.
I am in fact trying to use this standalone, so I think the problem is that I need RInside. FYI, that is not entirely obvious from the Rcpp docs. Anyway, I cannot find any useful documentation on the RInside API. The example given on the main page illustrates a use of R[], which presumably is a way to call the embedded R. Where is the real documentation, though? Thanks for your help. ‘ Cheers, Brook
RInside is to call the embedded R. What do you want to do with R/Rcpp? Best, KK -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20191206/f88c80d5/attachment.html>
On Dec 6, 2019, at 3:50 PM, Qiang Kou <qkou at qkou.info> wrote: RInside is to call the embedded R. What do you want to do with R/Rcpp?
I am trying to write a bridge between R and C++ and need some test cases. I was hoping I could use Rcpp objects directly in C++, but apparently that is not possible outside of an R environment, which I presume is what RInside provides. Is there any way to test the correctness of Rcpp-using functions without running the C++ code within R? I am thinking not, but would like to find otherwise, as this makes testing much harder. Cheers, Brook
I suggest you check all the examples of RInside. For example, https://github.com/eddelbuettel/rinside/blob/master/inst/examples/standard/rinside_sample16.cpp This might meet your requirements. Best, Qiang Kou
On Fri, Dec 6, 2019 at 2:54 PM Brook Milligan <brook at nmsu.edu> wrote:
On Dec 6, 2019, at 3:50 PM, Qiang Kou <qkou at qkou.info> wrote: RInside is to call the embedded R. What do you want to do with R/Rcpp?
I am trying to write a bridge between R and C++ and need some test cases. I was hoping I could use Rcpp objects directly in C++, but apparently that is not possible outside of an R environment, which I presume is what RInside provides. Is there any way to test the correctness of Rcpp-using functions without running the C++ code within R? I am thinking not, but would like to find otherwise, as this makes testing much harder. Cheers, Brook
-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20191206/11007636/attachment.html>
On Dec 6, 2019, at 3:58 PM, Qiang Kou <qkou at qkou.info> wrote: I suggest you check all the examples of RInside. For example, https://github.com/eddelbuettel/rinside/blob/master/inst/examples/standard/rinside_sample16.cpp This might meet your requirements.
Thanks.
The sample16 showed several things you might need in your case: 1. construct R objects and functions using Rcpp 2. call them through the embedded R and pass results back to C++ I think it is a good example, if you want to write testing cases for R/C++ bridge code. Best, KK
On Fri, Dec 6, 2019 at 3:08 PM Brook Milligan <brook at nmsu.edu> wrote:
On Dec 6, 2019, at 3:58 PM, Qiang Kou <qkou at qkou.info> wrote: I suggest you check all the examples of RInside. For example,
This might meet your requirements.
Thanks. From the examples I glean the following: - RInside::RInside(int,char*[]) - RInside::operator[](...) - Several RInside::parseEval*(...) member functions Is this the entire public API? Is there any documentation on these more specific than guessing from the examples? Thanks for your help. Cheers, Brook
-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20191206/69e23276/attachment.html>
Brook, Rcpp: Browseable and searchable class hierarchy http://dirk.eddelbuettel.com/code/rcpp/html/ (still complicated as there is a lot of "machinery") RInside: Similar (but less as there simply is less: Rcpp does the marshalling) http://dirk.eddelbuettel.com/code/rinside/html/ Otherwise: Nine vignettes for Rcpp including two peer-reviewed papers *Plenty* of working examples as documention for RInside below examples/ (or inst/examples/ in the sources) A book about it all Lots of other documentation on other places on the interwebs Hope this helps, Dirk
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org