02 - Setup

C++ Setup

You will need a working C++ compiler capable of compiling C++ code using the C++11 standard or newer. To get it:

  • On Windows, install Rtools.
  • On Mac, install Xcode from the App Store.
  • On Linux, sudo pacman -Syu install build-essential g++-11 or similar.

On my laptop, this is the output of g++ --version:

g++ (GCC) 15.2.1 20250813
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

For Ubuntu Users

After installing g++-11, you may still encounter issues compiling C++ code due to missing standard library headers. This does not happen when testing on Manjaro Linux.

You can try this:

sudo apt install build-essential g++-11 libc++-11-dev libc++abi-11-dev

Then you can attempt to compile a simple example to print a number:

// save this as 01-print-number.cpp or similar
#include <iostream> // required for the cout function

// function to print a message
int main() {
    std::cout << 1 << std::endl;
    return 0;
}

Then try to compile and run it with the following bash instruction:

g++ 01-print-number.cpp
./01-print-number # prints "1"

Otherwise, not installing the additional packages may lead to the following error:

fatal error: 'cstdio' file not found
fatal error: 'vector' file not found
cannot find -lc++abi: No such file or directory

R Setup

You can install the R packages cpp4r, tinydev and microbenchmark with the following command:

install.packages(c("cpp4r", "tinydev", "microbenchmark"))

To verify that R can compile C++ code, you can run tinydev::check_cpp_compiler(), which will return an output similar to:

using C++ compiler: ‘g++ (GCC) 16.1.1 20260430’
g++ -std=gnu++20 -I"/usr/include/R/" -DNDEBUG   -I/usr/local/include    -fpic  -Wall -O3 -pedantic  -c check_cpp_compiler.cpp -o check_cpp_compiler.o
g++ -std=gnu++20 -shared -L/usr/lib64/R/lib -Wl,-O1 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,-z,pack-relative-relocs -flto=auto -o check_cpp_compiler.so check_cpp_compiler.o -L/usr/lib64/R/lib -lR
using C++ compiler: ‘g++ (GCC) 16.1.1 20260430’
add(2, 3) = 5

Loading...