Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6727579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:03:58+00:00 2026-05-26T10:03:58+00:00

All right, so I’m programming in R and I want to make a C++

  • 0

All right, so I’m programming in R and I want to make a C++ function. I’ve imported the Rcpp and inline libraries. For right now, I’m just trying to make a simple function that adds 2 numbers, but no matter what I try, I get errors.

Here’s my code:

cppstring = 'double ss = RcppSexp(s).asDouble(); return RcppSexp(ss+4).asSexp();'
hi <- cfunction(body=cppstring, signature(s="double"), Rcpp = TRUE)

and when I enter that second line, I get

file628a34ce.cpp: In function ‘SEXPREC* file628a34ce(SEXPREC*)’:
file628a34ce.cpp:9: error: ‘RcppSexp’ was not declared in this scope
make: *** [file628a34ce.o] Error 1

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source:
1: #include <Rcpp.h>
2: 
3: 
4: extern "C" {
5:   SEXP file628a34ce ( SEXP s );
6: }
7: 
8: SEXP file628a34ce ( SEXP s ) {
9: double ss = RcppSexp(s).asDouble(); return RcppSexp(ss+4).asSexp();
10:   Rf_warning("your C program does not return anything!");
11:   return R_NilValue;
12: }
Error in compileCode(f, code, language, verbose) : 
Compilation ERROR, function(s)/method(s) not created! file628a34ce.cpp: In function    ‘SEXPREC* file628a34ce(SEXPREC*)’:
file628a34ce.cpp:9: error: ‘RcppSexp’ was not declared in this scope
make: *** [file628a34ce.o] Error 1

I’ve tried everything I could possibly think of, from casting, to moving code around, to #including RcppSexp, to just plain returning s, and every time I get some error, whether it’s

cannot convert ‘double’ to ‘SEXPREC*’ in return

or

invalid use of undefined type ‘struct SEXPREC’

or

forward declaration of ‘struct SEXPREC’

…I’m so confused 🙁 I’ve looked at several examples online, and what I currently have seems to be what everyone else is doing, and it magically works for them…

What is this SEXPREC* thing I keep seeing everywhere? And what is that extern “C” function thing that it makes? And why does it generate statements AFTER my return statement and tell me that my function doesn’t return anything, even though it does?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:03:59+00:00Added an answer on May 26, 2026 at 10:03 am

    Is there a reason you are not starting from the (literally!!) dozens of Rcpp examples using inline?

    Also, what on earth is RcppSexp? What documentation are your following?

    Here is an example I did last night for someone on the rcpp-devel (which you should probably join):

    library(Rcpp)
    library(inline)
    
    xorig <- c(1, -2, 3, -4, 5, -6, 7)
    
    code <- '
        Rcpp::NumericVector x(xs);
        Rcpp::NumericVector xa = sapply( x, ::fabs );
        return(xa);
        '
    
    xabs <- cxxfunction(signature(xs="numeric"),
                        plugin="Rcpp",
                        body=code)
    
    xabs(xorig)
    

    This is a more advanced example as it uses Rcpp sugar to give us vectorised expression a la R in C++, which we demonstrate here with a the simple sapply() from Rcpp sugar:

    R> library(Rcpp)
    R> library(inline)
    R> 
    R> xorig <- c(1, -2, 3, -4, 5, -6, 7)
    R> 
    R> code <- '
    +     Rcpp::NumericVector x(xs);
    +     Rcpp::NumericVector xa = sapply( x, ::fabs );
    +     return(xa);
    +     '
    R> 
    R> xabs <- cxxfunction(signature(xs="numeric"),
    +                     plugin="Rcpp",
    +                     body=code)
    R> 
    R> xabs(xorig)
    [1] 1 2 3 4 5 6 7
    R> 
    

    This most clearly demonstrates two of your requests: we use the implicit template converters as<>() to go from a SEXP given from R to the initial vector, and then use the implicit template converter wrap() to return the transformed second vector.

    All this is explained in detail in the Rcpp-introduction vignette and the other vignettes in the Rcpp documentation,.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the OnIdle-event for some simple animations, and it works all right. The
Right now all of my times on my reports are in GMT. Anybody know
Right now all I am using to calculate the size are the files in
I'm developing a facebook app right now all by my lonesome. I'm attempting to
right now i am simply ftping everything (all of my source code included) but
Loop vectorization is when all right-hand-side expressions are computed at the onset. I just
I try to send values to the GLSL, int is just all right, but
All right all, So I've got a UITableView that is inited in applicationDidFinishLaunching like
you all know: right click -> zoom in or out in flash file, well,
The idea is to move all of the right elements into the left and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.