An open API service indexing awesome lists of open source software.

https://github.com/jmaerte/cmake-rcpp-template

A CRAN-ready R package template that integrates the simplicity of CMake with the power of Rcpp, enabling platform-independent linkage to external C/C++ libraries such as OpenCL, OpenGL, and others.
https://github.com/jmaerte/cmake-rcpp-template

cmake cmaketemplate cpp cran cran-r opencl opengl r rcpp

Last synced: 3 months ago
JSON representation

A CRAN-ready R package template that integrates the simplicity of CMake with the power of Rcpp, enabling platform-independent linkage to external C/C++ libraries such as OpenCL, OpenGL, and others.

Awesome Lists containing this project

README

          

# ๐Ÿ“ฆ Template Project: CMake for Rcpp

A **CRAN-ready R package template** that integrates the simplicity of **CMake** with the power of **Rcpp**, enabling platform-independent linkage to external C/C++ libraries such as **OpenCL**, **OpenGL**, and others.

> This template streamlines the development of high-performance R packages that rely on native code, without manual environment configuration.

---

## โœจ Highlights

- โœ… Platform-independent configuration and linkage with **CMake**
- โœ… Built `tar.gz` does not have any binaries - everything is compiled at package installation.
- โœ… No manual `Makefile` or environment variable setup required
- โœ… Clean separation of public/private headers in C++
- โœ… Simple Rcpp integration โ€” no need to link Rcpp to the external libraries
- โœ… Example: GPU-accelerated distance matrix using **OpenCL**

---

## ๐Ÿ“ Project Structure

```
cmake-rcpp-template
โ”‚ # Package metadata files
| # configure and configure.win handle the installation
|
โ”œโ”€โ”€ src
โ”‚ โ”‚ # *.cpp define the Rcpp wrapper functions
โ”‚ โ”‚
โ”‚ โ””โ”€โ”€ backend
โ”‚ โ”‚ # A standard CMake project
| |
| โ”œโ”€โ”€ include
โ”‚ | | # public headers; should not include external
| | โ””โ”€โ”€ # library headers; otherwise Rcpp needs to link too.
| |
| โ”œโ”€โ”€ src
| | | # *.cpp define the actual implementation of functionality
| | |
| | โ””โ”€โ”€ internal
| | | # Some internal header files that may
| | โ””โ”€โ”€ # include external library headers.
| |
| โ””โ”€โ”€ lib
| โ””โ”€โ”€ # Some external library headers.
|
โ”œโ”€โ”€ R
| โ””โ”€โ”€ # Caller scripts for the Rcpp functions and other R functions
|
โ”œโ”€โ”€ man
| โ””โ”€โ”€ # Documentation goes here
|
โ””โ”€โ”€ inst
| # The library features the possibility to load external
โ””โ”€โ”€ # resources from this folder.
```

๐Ÿ“ **Note:** Public headers (`backend/include`) define C/C++ interfaces compatible with `Rcpp`; they should not include external library headers as `Rcpp` does not link against them. Private headers (`backend/src/internal`) manage actual implementation details, allowing clean abstraction.

## โš™๏ธ Backend with CMake
The `src/backend` directory is a standalone **CMake** project. It builds a shared library that is automatically linked to the R package.

* Extend linkage easily in `CMakeLists.txt` for OpenCL, OpenGL, etc.

* CMake handles all platform-dependent paths and setup.

## ๐Ÿ“‚ The `inst` Folder

The `inst/` directory contains runtime resources (e.g., `.cl` kernels). During installation, files from `src/backend/kernels` are copied into `inst/kernels` using the `configure` and `configure.win` scripts.

``````r
# Accessed in R via:
system.file("kernels", package = "CMakeRcppTemplate")
``````

These paths are injected into the shared library at runtime using the `.onLoad` hook (see `R/CMakeRcppTemplate.R`), enabling dynamic loading of external resources like OpenCL kernels or OpenGL shaders.

## ๐Ÿ”— Rcpp Integration

This template uses **manual `SEXP` wrappers** for each Rcpp function (instead of `[[Rcpp::export]]`) to avoid platform-specific issues:

> On some systems, `enterRNGScope` errors to be undefined in `RcppExports.o` even though it was not used in `RcppExports.cpp` when using auto-generated bindings via `compileAttributes()`.

However, for simplicity, you *may* choose to use `[[Rcpp::export]]` if you're willing to trade off some portability.

๐Ÿ“Œ **Important**:

- Add `import(Rcpp)` to the top of your `NAMESPACE` file to avoid errors like:

```text
Rcpp_precious_remove not provided by package Rcpp.
```

- Manually written R wrappers for `SEXP` functions should defensively check input types, since auto-type checks are not generated without `[[Rcpp::export]]`.

## ๐Ÿ“ค Exports

Your package may export:

- `SEXP`-wrapper functions via `Rcpp`
- High-level R functions that internally call C/C++ via `Rcpp`
- High-level R functions that are independent of the C/C++ backend

## ๐Ÿงช Example Use Case

This template includes an example GPU-based distance matrix computation via **OpenCL**. No changes are required to Rcpp code when adding new backend libraries โ€” just update the CMake configuration.

## ๐Ÿ“š Resources

- [CMake Documentation](https://cmake.org/documentation/)
- [Rcpp Package](https://cran.r-project.org/package=Rcpp)
- [Writing R Extensions](https://cran.r-project.org/doc/manuals/r-release/R-exts.html)

## ๐Ÿ› ๏ธ Getting Started

1. Clone this repository (use `--recursive` option in order to get the OpenCL headers aswell!)
2. Modify the `src/backend/` CMake code and `src/*.cpp` `Rcpp`- and `SEXP`-bindings
3. Add new R functions in the `R/` directory calling your `SEXP`-binding functions
4. Document functions via `Rmarkdown` in `man`
5. Adjust the section `copying kernels` in `configure` and `configure.win` according to your external resources.
6. Build and install using `devtools::install()` or `R CMD INSTALL`

## ๐Ÿง‘โ€๐Ÿ’ป Contributions

Contributions, suggestions, and issues are welcome! Feel free to open a pull request or issue.

## ๐Ÿ“„ License

MIT License. See `LICENSE` for details.

> โš ๏ธ Author's Note: This project was created while I was affiliated with **IAP-GmbH Intelligent Analytics Projects**.