Skip to content

[Rcpp-devel] Segmentation faults

9 messages · Brook Milligan, Balamuta, James Joseph, Qiang Kou +1 more

#
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
#
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
#
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
#
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:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20191206/11007636/attachment.html>
#
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:

            
-------------- 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