1

I'm trying to compile C++ code in R so I can install packages from source. I’m running R 4.0.2 on Mac OS X Catalina (10.15.6). I've followed instructions here for installing R Compiler Tools for Rcpp on Mac OS. However, when following the instructions, I receive an error when trying to compile the following C++ code in R (saved as /Users/itpetersen/Desktop/helloworld.cpp):

#include <RcppArmadillo.h>   

// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
void hello_world() {
  Rcpp::Rcout << "Hello World!" << std::endl;  
}

// After compile, this function will be immediately called using
// the below snippet and results will be sent to the R console.

/*** R
hello_world() 
*/

Here’s the error I receive when trying to compile the above C++ code in R:

> Rcpp::sourceCpp('/Users/itpetersen/Desktop/helloworld.cpp')
helloworld.cpp:1:10: fatal error: 'RcppArmadillo.h' file not found
#include <RcppArmadillo.h>   
         ^~~~~~~~~~~~~~~~~
1 error generated.
make: *** [helloworld.o] Error 1
clang++ -mmacosx-version-min=10.13 -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Packages/Rcpp/include" -I"/Users/itpetersen/Desktop" -I/usr/local/include   -fPIC  -Wall -g -O2  -c helloworld.cpp -o helloworld.o
Error in Rcpp::sourceCpp("/Users/itpetersen/Desktop/helloworld.cpp") : 
  Error 1 occurred building shared library.

I also receive an error when trying to compile the mirt package from source:

> install.packages("mirt", type = "source")
Installing package into ‘/Library/Frameworks/R.framework/Packages’
(as ‘lib’ is unspecified)
trying URL 'https://cran.r-project.org/src/contrib/mirt_1.32.1.tar.gz'
Content type 'application/x-gzip' length 811759 bytes (792 KB)
==================================================
downloaded 792 KB

* installing *source* package ‘mirt’ ...
** package ‘mirt’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
clang++ -mmacosx-version-min=10.13 -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG  -I'/Library/Frameworks/R.framework/Packages/Rcpp/include' -I'/Library/Frameworks/R.framework/Packages/RcppArmadillo/include' -I/usr/local/include   -fPIC  -Wall -g -O2  -c Estep.cpp -o Estep.o
In file included from Estep.cpp:1:
In file included from ./Misc.h:4:
In file included from /Library/Frameworks/R.framework/Packages/RcppArmadillo/include/RcppArmadillo.h:31:
/Library/Frameworks/R.framework/Packages/RcppArmadillo/include/RcppArmadilloForward.h:26:10: fatal error: 'RcppCommon.h' file not found
#include <RcppCommon.h>
         ^~~~~~~~~~~~~~
1 error generated.
make: *** [Estep.o] Error 1
ERROR: compilation failed for package ‘mirt’
* removing ‘/Library/Frameworks/R.framework/Packages/mirt’
Warning in install.packages :
  installation of package ‘mirt’ had non-zero exit status

The downloaded source packages are in
    ‘/private/var/folders/p5/tkdpnffs2_g5ybr2t1yq1nd0rq_9pt/T/RtmpHLa2q1/downloaded_packages’

Here’s what I’ve done:

  • I deleted the file at: ~/.R/Makevars
  • I deleted the file at: ~/.Renviron
  • I installed Xcode version 11.6
  • I verified the Xcode installation by typing gcc --version into terminal. Here was the output:
gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
  • I installed gfortran version 8.2 from here
  • I installed the Rcpp and RcppArmadillo packages in R
  • I also tried copying the contents of the include folder from the RcppArmadillo folder to the include folder from the Rcpp folder (as suggested here), but continued to receive an error (albeit a different error) when trying to compile C++ code
  • I also tried the solutions to a similar question on StackOverflow here, but they did not solve the problem. Note that I am using a newer version of R than the other question, so my question should not be considered a duplicate question.

Here's my session info:

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RcppArmadillo_0.9.900.2.0 Rcpp_1.0.5               

loaded via a namespace (and not attached):
[1] compiler_4.0.2 tools_4.0.2    tinytex_0.25   xfun_0.16
itpetersen
  • 1,475
  • 3
  • 13
  • 32
  • You could try checking if the header is in fact in the include folder. It's located at /usr/include on linux, should be the same for MacOS. See if there's a RcppArmadillo.h in there. – tralph3 Aug 10 '20 at 02:13
  • Thanks for responding. I don't have a `/usr/include` folder on my Mac. However, I do have a `usr/local/include` folder. But I don't see `RcppArmadillo.h` there. How should I fix that? – itpetersen Aug 10 '20 at 13:00
  • Well it should appear there when it gets installed. If it's not there then it means the installation wasn't successful or you installed something else, or it's called something else. I'm not familiar with Macs, neither with this package, or R, but at least that gives you some insight into where it went wrong. – tralph3 Aug 10 '20 at 18:20

0 Answers0