I came across a useful post on generating codebooks in R by Prof. Claus Thorn Ekstrøm today. From it:

A codebook is a technical document that provides an overview of and information about the variables in a dataset. The codebook ensures that the statistician has the complete background information necessary to undertake the analysis, and a codebook documents the data to make sure that the data is well understood and reusable in the future.

Really quite useful. In order for me to follow along with his post, I had to install the dataMaid package. That’s when I ran into compiling issues. Sometime in the not so distant past, I’d updated something or the other (Xcode, perhaps?), and I started getting errors saying:

Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) : namespace 'pkg' 0.12.10 is already loaded, but >= 0.12.12 is required Error: package or namespace load failed for 'pkg'

And when I’d try to install the newer version of a package that needed compiling, I’d get errors such as:

ld: warning: directory not found for option '-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0'

Similar to those observed in this stackoverflow question:

* installing *source* package ‘mclust’ ...
** package ‘mclust’ successfully unpacked and MD5 sums checked
** libs
gfortran-4.8   -fPIC  -g -O2  -c mclust.f -o mclust.o
gfortran-4.8   -fPIC  -g -O2  -c mclustaddson.f -o mclustaddson.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o mclust.so mclust.o mclustaddson.o -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2 -lgfortran -lquadmath -lm -L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2 -lgfortran -lquadmath -lm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: warning: directory not found for option '-L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2'
ld: warning: directory not found for option '-L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2'
ld: library not found for -lquadmath
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mclust.so] Error 1
ERROR: compilation failed for package ‘mclust’
* removing ‘/Library/Frameworks/R.framework/Versions/3.3/Resources/library/mclust’
Warning in install.packages :
  installation of package ‘mclust’ had non-zero exit status

The solution presented worked great for me, but first I had to understand what they meant by

“your code is not being run under gcc instead it is being forwarded to clang

I had no idea whether I had gcc, and if yes, which version, or where it was located.

Googling my error initially led me to this post which explained gfortran and compiler errors. It was an introduction to compiling and an associated error. So now I had to look for what was doing the compiling, its version, and its path.

$ which gcc #do I have gcc
/usr/bin/gcc

$ gcc -v #what's the version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

I had gcc, but looking into its version gave me clang instead. That’s why my code was being forwarded to clang instead of gcc. Did I have another version lying around somewhere, perhaps installed via homebrew? I asked myself this because I’d been able to compile packages from source earlier, so it was probably masked by xcode somewhere down the line.

$ brew info gcc
gcc: stable 8.2.0 (bottled), HEAD
GNU compiler collection
https://gcc.gnu.org/
/usr/local/Cellar/gcc/8.2.0 (1,495 files, 344.8MB) *
  Poured from bottle on 2018-11-30 at 16:58:27
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/gcc.rb
==> Dependencies
Required: gmp ✔, isl ✔, libmpc ✔, mpfr ✔
==> Options
--HEAD
	Install HEAD version
==> Analytics
install: 88,705 (30 days), 206,678 (90 days), 761,080 (365 days)
install_on_request: 42,028 (30 days), 100,930 (90 days), 364,332 (365 days)
build_error: 0 (30 days)

So yes, I did have another gcc installed previously installed by homebrew!

$ which gcc-8
/usr/local/bin/gcc-8

$ gcc-8 -v
Using built-in specs.
COLLECT_GCC=gcc-8
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/8.2.0/libexec/gcc/x86_64-apple-darwin17.7.0/8.2.0/lto-wrapper
Target: x86_64-apple-darwin17.7.0
Configured with: ../configure --build=x86_64-apple-darwin17.7.0 --prefix=/usr/local/Cellar/gcc/8.2.0 --libdir=/usr/local/Cellar/gcc/8.2.0/lib/gcc/8 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-8 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --enable-checking=release --with-pkgversion='Homebrew GCC 8.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --disable-nls
Thread model: posix
gcc version 8.2.0 (Homebrew GCC 8.2.0) 

If you don’t have gcc, you can install it via brew install gcc.

Now I needed to make my way over to the ~/.R/Makevars/ file and update its compile statements. This was the first time I’d opened and edited Makevars, and I found it already contained:

CC=clang
CXX=clang++

I changed it to:

VER=-8
CC=gcc$(VER)
CXX=g++$(VER)
CFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
CXXFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
FLIBS=-L/usr/local/Cellar/gcc/8.2.0/lib/gcc/8

Packages were finally being compiled and successfully installed again :)

Coming back to dataMaid, it’s pretty cool and I’m definitely going to explore it further!

All thanks to TheCoatlessProfessor, who wrote the awesome compiler post and the stackoverflow answer that helped solve this mystery.