1

I face an error when compiling an simple rcpp code.

Rcpp::sourceCpp(code = '
#include <RcppArmadillo.h>
#ifdef _OPENMP
# include <omp.h>
#endif
                  
                  // [[Rcpp::depends(RcppArmadillo)]]
                  // [[Rcpp::plugins(openmp)]]
                  // [[Rcpp::export]]
                  void omp_test()
                  {
#ifdef _OPENMP
                    Rprintf("OpenMP threads available: %d\\n", omp_get_max_threads());
#else
                    Rprintf("OpenMP not supported\\n");
#endif
                  }
')

When I compile the same code in R, not in Rstudio, it works well. However, when I compile it in Rstudio, it shows an error like below: [![enter image description here][1]][1]

> Rcpp::sourceCpp("R/utils/cpp_utils.cpp")
ld: warning: directory not found for option '-L/opt/R/arm64/lib'
ld: warning: dylib (/Library/Frameworks/R.framework/Resources/lib/libRlapack.dylib) was built for newer macOS version (20.0) than being linked (12.0)
ld: warning: dylib (/Library/Frameworks/R.framework/Resources/lib/libRblas.dylib) was built for newer macOS version (20.0) than being linked (12.0)
ld: warning: dylib (/opt/R/arm64/gfortran/lib/libgfortran.dylib) was built for newer macOS version (12.1) than being linked (12.0)
ld: warning: dylib (/Library/Frameworks/R.framework/R) was built for newer macOS version (20.0) than being linked (12.0)
Error in dyn.load("/private/var/folders/bm/_jr48kjs1ss7gk_vwbcxc7nr0000gn/T/RtmpFzxtqw/sourceCpp-aarch64-apple-darwin20-1.0.8/sourcecpp_6e64632dce2/sourceCpp_4.so") : 
  unable to load shared object '/private/var/folders/bm/_jr48kjs1ss7gk_vwbcxc7nr0000gn/T/RtmpFzxtqw/sourceCpp-aarch64-apple-darwin20-1.0.8/sourcecpp_6e64632dce2/sourceCpp_4.so':
  dlopen(/private/var/folders/bm/_jr48kjs1ss7gk_vwbcxc7nr0000gn/T/RtmpFzxtqw/sourceCpp-aarch64-apple-darwin20-1.0.8/sourcecpp_6e64632dce2/sourceCpp_4.so, 0x0006): Library not loaded: @rpath/libgfortran.5.dylib
  Referenced from: /private/var/folders/bm/_jr48kjs1ss7gk_vwbcxc7nr0000gn/T/RtmpFzxtqw/sourceCpp-aarch64-apple-darwin20-1.0.8/sourcecpp_6e64632dce2/sourceCpp_4.so
  Reason: tried: '/usr/lib/libgfortran.5.dylib' (no such file)

To install rcpp, I followed the instructions in this page: Configuring compilers on Mac M1 (Big Sur, Monterey) for Rcpp and other tools

Moreover, as I want to debug the rcpp with lldb, I installed R from the website: https://mac.r-project.org/

I tried to link the libgfortran.5.dylib to usr/lib but permission was denied even with sudo.

I am using Mac monetery with M1,

Thanks for your solutions to the issue.

Gwan
  • 31
  • 4
  • Maybe try the r-sig-mac mailing list; there is little I can help you with. The Rcpp and RcppArmadillo package build and test fine at CRAN -- and whereever the correct tools are installed. – Dirk Eddelbuettel Jan 24 '22 at 12:00
  • 1
    Please do not enter code or error messages as pictures. It is *extremely* important that they are searchable and accessible to everyone. See this very recent thread https://meta.stackoverflow.com/questions/415324/where-are-new-users-supposed-to-learn-that-they-should-show-code-and-error-messa Please delete the picture and copy and paste the error output *as text*. See also [ask]. – Vladimir F Героям слава Jan 24 '22 at 12:17
  • I've since updated the [answer](https://stackoverflow.com/q/70638118/12685768) with new instructions that should get things to work running R from Terminal or RStudio. See the discussion [here](https://stackoverflow.com/q/70955692/12685768). Someone else reported an identical problem. – Mikael Jagan Feb 17 '22 at 01:49

1 Answers1

1

See this answer: Configuring compilers on Mac M1 (Big Sur, Monterey) for Rcpp and other tools

The issue is that your library path configured is invalid - it starts with "-L". This issue could be in a makefile for the project.

Check your RStudio build flags if you are specifying them in the command line. To specify the library path, you may need to use a lower-case L based on the documentation.

-l, --library=LIB library directory used for test installation

Alternatively, this site details the commands to check and set the library paths in RStudio.

John Glenn
  • 1,469
  • 8
  • 13