Written and maintained by CASRAI Editorial Board
Last updated
Rcpp is an R package, maintained by Dirk Eddelbuettel and listed on CRAN as “Seamless R and C++ Integration”, that lets you write functions in C++ and call them from R as ordinary R functions. Researchers use it to speed up loops and simulations that are slow in plain R. After this page you should be able to install Rcpp, compile a first C++ function from R, choose between cppFunction(), sourceCpp() and a package, and record the details needed to reproduce your results. The current CRAN release at the time of reading (2026-11-08) is Rcpp 1.1.2, published 2026-07-05; it depends on R 3.5.0 or later.
What Rcpp is used for in research
The Rcpp project site describes the package as a way of extending and growing R applications through an easy-to-use, robust and performant C++ interface, mapping R data types to C++ ones. Typical research uses are numerical code that cannot be vectorised (iterative algorithms, Monte Carlo and MCMC loops, recursive calculations) and wrapping an existing C++ library so analysts can call it from R. The project site states that more than 3000 CRAN packages use Rcpp (its own figure, as read on 2026-11-08). The site also points to the Rcpp Book (Springer, useR! series), the Rcpp Gallery with 100+ examples, the Stack Overflow tag rcpp and the rcpp-devel mailing list for support.
Rcpp does not change what your statistics mean; it changes how a calculation is executed. Keep an R reference implementation of anything you move to C++ and compare outputs before trusting the faster version.
Install or access (official source only)
- Install Rcpp from CRAN inside R:
install.packages("Rcpp"), then load it withlibrary(Rcpp). For general package-installation behaviour see How to Install R Packages (CRAN, Bioconductor, GitHub). - Have a C++ compiler available, because Rcpp compiles your code into a shared library. On Windows, CRAN publishes Rtools, the toolchains for building R and R packages from source. The CRAN Rtools page (read 2026-11-08) pairs versions as follows: Rtools 4.5 with R 4.5.0 and later, Rtools 4.4 with R 4.4.x, Rtools 4.3 with R 4.3.x, Rtools 4.2 with R 4.2.x, and Rtools 4.0 with R 4.0.0 to 4.1.3.
- If you have not yet installed R itself, start with How to Install R and RStudio on Windows, Mac and Linux.
Source code is at github.com/RcppCore/Rcpp, and CRAN carries the reference manual and vignettes (introduction, attributes, extending, FAQ, modules, package, quickref, sugar and others).
Step-by-step: compile your first C++ function from R
Rcpp offers three routes. Pick by the size of the job, using what the sourceCpp reference page says about each.
| Route | Use it when | What the documentation says |
|---|---|---|
cppFunction() |
One short function defined from an R string | Takes C++ source as a string, compiles it into a shared library and creates an R function that calls it; the cppFunction reference notes that sourceCpp is usually easier to maintain for larger projects. |
sourceCpp() |
A standalone .cpp file, or inline code via code= |
Parses the file, finds functions marked // [[Rcpp::export]], and makes them available in the environment you give it (global by default). |
| An R package | More than one source file, or external dependencies | The sourceCpp page says to create an R package in that case; the Rcpp::export attribute also works inside packages through compileAttributes. |
Steps
- Install and load Rcpp:
install.packages("Rcpp")thenlibrary(Rcpp). - Write the C++ function in a file such as
fibonacci.cpp, with the export attribute above it:#include <Rcpp.h> // [[Rcpp::export]] int fibonacci(const int x) { if (x == 0) return(0); if (x == 1) return(1); return (fibonacci(x - 1)) + fibonacci(x - 2); } - Compile and load it from R:
sourceCpp("fibonacci.cpp"). The same code can be passed as a string withsourceCpp(code = '...'). - Call it like any R function:
fibonacci(10). - Check it against an R version and time both, for example
fib_r <- function(x) if (x < 2) x else fib_r(x - 1) + fib_r(x - 2), thenidentical(fibonacci(20L), fib_r(20))andsystem.time(fib_r(25))againstsystem.time(fibonacci(25L)).
The documented rules for exported functions: mark them with [[Rcpp::export]], define them in the global namespace, use return types compatible with Rcpp::wrap and parameter types compatible with Rcpp::as. You can also embed R test code in the .cpp file inside a block comment starting /*** R; it runs after compilation, so the compiled functions are available to it.
Output and how to read it
sourceCpp()returns, invisibly, a list with two elements:functions(names of the exported functions) andmodules(names of Rcpp modules). Assign it to a variable if you want to see them.- After a successful compile, the exported function appears in your global environment (or the
envyou passed) and returns ordinary R objects. - Compiled libraries are cached. By default the cache is
tempdir(), so it lasts only for the current R session; passcacheDirto keep it across sessions.rebuild = TRUEforces recompilation, andverbose = TRUEprints details about the generated code.
Troubleshooting
- Function not found after
sourceCpp(). Check the// [[Rcpp::export]]comment sits directly above a function defined in the global namespace. - Compile error about a type. Parameter and return types must be convertible with
Rcpp::asandRcpp::wrap. - Compiler not found on Windows. Install the Rtools release that matches your R version (table above in the install section).
- Stale behaviour after editing code. Call
sourceCpp(..., rebuild = TRUE). - Multi-file project is awkward. Move to an R package, as the documentation advises.
Reporting Rcpp in research and keeping it reproducible
For computational reproducibility, record the R version, the Rcpp version (1.1.2 as of 2026-07-05), the compiler or Rtools version, and the C++ source file itself alongside your analysis scripts. renv can lock package versions, and How to Cite Software, Code and R Packages explains how to cite Rcpp; citation("Rcpp") is the standard R call for a package’s citation details.
Free alternatives
| Tool | Language | What its own documentation says |
|---|---|---|
| cpp11 | R | A C++11 interface to R’s C API; goals and syntax similar to Rcpp, described as a ground-up rewrite with different design trade-offs; registers functions with [[cpp11::register]]; header-only (cpp11.r-lib.org). |
| pybind11 | Python | A small header-only library that exposes C++ types in Python and Python types in C++, mainly for wrapping existing C++ code (pybind11.readthedocs.io). |
| Cython | Python | A compiler for Python and its Python-based extension language that produces C extensions able to call C or C++ directly (cython.org). |
| Numba | Python | An open-source compiler that turns a subset of Python and NumPy code into machine code at runtime via a decorator, without a separate build step or a C/C++ compiler (numba.pydata.org). |
For broader tool choice see Free Statistical Software for Research.
FAQ
What is Rcpp?
An R package from CRAN that provides a C++ interface to R, so C++ functions can be compiled and called from R. The current version read on 2026-11-08 is 1.1.2.
How do I install Rcpp?
Run install.packages("Rcpp") in R, then library(Rcpp). You also need a C++ toolchain; on Windows CRAN provides Rtools matched to your R version.
When should I use sourceCpp instead of cppFunction?
Use sourceCpp() for a standalone .cpp file; use an R package once you have several source files or external dependencies.
Sources (official docs)
- CRAN: Rcpp package page, cran.r-project.org/web/packages/Rcpp/index.html (read 2026-11-08)
- Rcpp project site, www.rcpp.org (read 2026-11-08)
- Rcpp reference: sourceCpp and cppFunction, search.r-project.org/CRAN/refmans/Rcpp/html/ (read 2026-11-08)
- CRAN Rtools, cran.r-project.org/bin/windows/Rtools/ (read 2026-11-08)
- cpp11, cpp11.r-lib.org; pybind11, pybind11.readthedocs.io; Cython, cython.org; Numba, numba.pydata.org (read 2026-11-08)







