Gwan
Gwan

Reputation: 31

Rcpp compile issue in Rstudio

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.

Upvotes: 1

Views: 830

Answers (1)

John Glenn
John Glenn

Reputation: 1629

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.

Upvotes: 1

Related Questions