A new version of GCC (one of the C/C++ compilers used with R) has been released, and with that release CRAN has started testing packages against it. The result is a lot of new warnings and errors with existing packages, or with new packages. Quite reasonably, package developers are being asked to fix them.
The problem is that just because the new version (GCC 7.1) is technically released doesn’t mean it’s easily available. If you have a Linux or Windows box, changing your gcc version is somewhere between frustrating and near-impossible. Mac, you might not have gcc at all. And, well, it’s pretty hard to reliably fix bugs when you can’t reproduce them.
After a few days of tinkering, I’ve put together a Linux testbed for R using GCC 7.1. This here blog post is a recipe for replicating said testbed, letting you easily reproduce and fix existing warnings or altogether avoid new ones.
We’re gonna assume that you have a fresh Debian/Ubuntu instance with R on it - either something like a DigitalOcean droplet or a Docker image. Whatever it is, make sure you have R (here’s how to install it if you don’t) and open up a terminal.
Setting up devtools
The package you’re trying to fix probably lives on GitHub or somewhere similar, so it makes sense to pull and test against that version - first, because there’s the off-chance you accidentally fixed the bug already and just didn’t submit the fix to CRAN yet, and second, because it makes it easy to enter an iterative cycle of “try fix” - “push possible fix to GitHub” - “run on testbed” - “repeat”.
Accordingly we’re want to get set up with devtools
, home of the install_github
function. This comes with a couple of system level dependencies, so let’s handle those first. In the terminal of your testbed, type:
sudo apt-get install libssl-dev libcurl4-openssl-dev
Then enter R and run, well, install.packages("devtools")
. You’re now all set up to grab things from GitHub, bitbucket, or other sites in the same sort of area.
Installing GCC 7.1
Next up we need to actually install GCC 7.1. For the time being it’s not accessible through the default Ubuntu repositories. Instead, exit R, heading back to the terminal, and add it with:
sudo add-apt-repository ppa:jonathonf/gcc-7.1
sudo apt-get update
sudo apt-get install gcc-7 g++-7
That should install GCC 7.1, in parallel with whatever version your OS came with. Because it’s in parallel you save on accidentally breaking something important - the only cost is we have to separately configure it so that R points to it, which we’ll do next!
Linking GCC 7.1 and R
R stores information about (amongst other things) what it uses to compile C++ in the Makeconf
file. By default on Ubuntu it points straight to g++
- we want it to point to the new version we just installed, instead.
So: go to the Makeconf file (on Debian/Ubuntu, this is at /usr/lib/R/etc/Makeconf/
by default), find the line that reads:
CXX = g++
And change it to:
CXX = g++-7
Save it and close the file.
Develop!
Done! If you install or check a compiled package now, you should see G++-7 be used to built it instead of just g++. As a result, any errors CRAN is finding should show up on your testbed, and you can go fix them. At some point the compiler versions will start to line up again and this documentation will be superfluous - until then, hopefully it’s useful. And if anyone wants to build an R/GCC7.1 docker image from this post and cut out the middle person, all the better!