Installing GSL in R (two versions?) - r

Trying to figure out how to get a new version of gsl recognized by R. I added the last two lines to the Makevars file because I couldn't get the system to recognize this new version as default.
CXX14FLAGS=-O3 -march=native -mtune=native -fPIC
CXX14=g++ -std=c++14
CXX_STD=CXX14
GSL_CFLAGS=-I/usr/local/gsl/2.4/include
GSL_LIBGS=-L/usr/local/gsl/2.4/lib -lgsl -lgslcblas -lm
However, when I go to install, you can see part of it is calling 2.4 and part isn't.
Note that according to gsl's github page, the error I'm getting is consistent with a 1.X version of gsl.
Any help (1) getting centos to recognize the new gsl version or (2) just getting R to use it to compile the gsl package would be greatly appreciated!

The problem is that gsl 2.5>= is needed. The error message is not updated, see
https://github.com/RobinHankin/gsl/issues/18

Related

Rcpp fails to compile c++ code after series of updates

I'd like to keep my OS, R, and R packages up to date. I unknowingly treaded into deep water by upgrading to OSX 10.15.6 and upgrading to R 4.0.2. Currently, Rcpp is failing to compile c++ code, which I believe is causing the CRAN installation for certain packages to fail (e.g. glmmTMB), and is also causing installations from source to fail. I'll describe what I've done and hopefully someone can elucidate a solution.
In a rough order, here's what I've done:
Installed OSX 10.15.6
I don't remember the date but it was fairly recent. I don't know how closely this is related to the problem, I was having issues installing other R packages (e.g. rstan) that needed Rcpp prior to this.
Ran into an issue trying to run some old glmmTMB models
The exact error was identical to this issue. I followed the various solutions on that thread to no avail. Perhaps the most frustrating was when attempting to install from source, which then failed to compile some c++.
This wasn't the first time I had seen similar errors. Something similar happened when I tried installing rstan, so I asked for some help on their repo under a similar issue. Nothing helped.
This was on R 3.6, so I thought maybe it'd be worth updating R and glmmTMB. Before doing this I wanted to install Xcode, as opposed to just the Command Line Developer Tools, in hopes that the installation would ensure I'd have everything clang- and c++ compiler-related squared away.
Installed Xcode to help set a baseline c++ environment
Relatively painless, am able to build and compile fairly simple c++ projects.
Installed R 4.0.2
From the r-project site, I installed R-4.0.2.pkg without issues and with the default install settings. According to the R project Tools site, I was reassured in installing Xcode and continued on to install the GNU Fortran 8.2 installer.
Followed #coatless's guide to installing Rcpp
Discovered this guide and followed each step, except for those related to xcode-select.
Testing Rcpp and RcppArmadillo from the guide
One of the last steps is to test the installation with a simple helloworld.cpp test. This fails with this error (abridged for clarity):
> Rcpp::sourceCpp("~/Documents/BergenLab/nlp_cancer_metaphor/helloworld.cpp")
In file included from helloworld.cpp:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppArmadillo/include/RcppArmadillo.h:31:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppArmadillo/include/RcppArmadilloForward.h:26:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include/RcppCommon.h:29:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include/Rcpp/r/headers.h:67:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include/Rcpp/platform/compiler.h:100:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:317:9: error: no member named 'signbit' in the global namespace
using ::signbit;
~~^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make: *** [helloworld.o] Error 1
clang++ -mmacosx-version-min=10.13 -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I../inst/include -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppArmadillo/include" -I"/Users/alex/Documents/BergenLab/nlp_cancer_metaphor" -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fPIC -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -c helloworld.cpp -o helloworld.o
Error in Rcpp::sourceCpp("~/Documents/BergenLab/nlp_cancer_metaphor/helloworld.cpp") :
Error 1 occurred building shared library.
Tried to debug Rcpp to no avail
Found some somewhat related reports, such as Packages cannot build from source due to math.h not found, Rcpp doesn't under macOS, math.h not found, Source Cpp file failed due to math.h not found, and Cannot compile R packages with c++ code after updating to macOS Catalina.
None of the solutions in these answers have helped at all. The only solution which gave me feedback was using installer to install macOS_SDK_headers_for_macOS_10.14.pkg which subsequently gave me installer: Error - the package path specified was invalid (not sure how what the implications of this are).
Installing glmmTMB from source fails
One of the solutions from an above glmmTMB issue recommends installing from source. Here is the error on my attempt:
* installing *source* package ‘glmmTMB’ ...
** package ‘glmmTMB’ 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/Versions/4.0/Resources/library/TMB/include' -I'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include' -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fPIC -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -c glmmTMB.cpp -o glmmTMB.o
In file included from glmmTMB.cpp:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/TMB/include/TMB.hpp:53:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Dense:1:
In file included from /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppEigen/include/Eigen/Core:96:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/complex:245:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:317:9: error: no member named 'signbit' in the global namespace
using ::signbit;
~~^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
13 warnings and 20 errors generated.
make: *** [glmmTMB.o] Error 1
ERROR: compilation failed for package ‘glmmTMB’
* removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/glmmTMB’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/glmmTMB’
Warning in install.packages :
installation of package ‘glmmTMB’ had non-zero exit status
Overall, I'm still unable to run my glmmTMB models (and I presume unable to build rstan as well). Something with my c++ compiler must be off but I'm a c++ beginner and am unsure how to interpret the errors I'm getting. Something's wrong with the header files, but it seems perhaps like it's not due to math.h being missing (like most other posts). It should go without saying but any help would be greatly appreciated!
Edit 1
I've tried to narrow down the error and thus followed each step from #coatless's guide, exactly as listed there and according to the R installation documentation. My exact steps:
Removed gfortran 8.2. Removed ~/.Renviron and ~/.R/Makevars. Unlinked both of these files from Rstudio with unlink().
Ran xcode-select --install, which returned with "use "Software Update" to install updates." Accordingly, softwareupdate --list estimated that nothing needed updating. Regardless, I uninstalled Command Line Tools with sudo rm -rf /Library/Developer/CommandLineTools and installed the Command Line Tools with xcode-select —install. Up to this point everything is working as expected. Small c++ programs are being compiled without issue by both gcc and clang++.
According to the R manual, from https://mac.r-project.org/libs-4/, I installed pcre2-10.34-darwin.17-x86_64.tar.gz, xz-5.2.4-darwin.17-x86_64.tar.gz , and readline-5.2.14-darwin.17-x86_64.tar.gz to /usr/local. No big issues here, although I'm not really sure how to verify that these work as expected.
Restarted an R session in RStudio.
Installed Rcpp and RcppArmadillo with install.packages(c(‘Rcpp’, ‘RcppArmadillo’))
Created a ~/helloworld.cpp with the code from Slide 15 mentioned in the comments.
Running Rcpp::sourceCpp("~/helloworld.cpp") fails with the exact error as mentioned in the body of the post. Similarly evalCpp() fails with a similar error.
After a bit of digging, the issue turned out to be neither glmmTMB, Rcpp, or really anything high-level. From what I understand: When R uses Rcpp to compile c++ code, a certain set of arguments are passed to the system c++ compiler. In my case this compiler was clang++ since I had just installed Xcode. The c++ compiler relies on a set of c++ header files to be included in exactly the right order. Seemed like the order of mine were incorrect, thus causing a slew of errors regarding "missing template definitions."
I looked into changing the order of the includes but couldn't find any straightforward solution. I knew that Rcpp was calling a very specific array of arguments to clang++, so I looked into how that might elucidate an answer. I looked into the manuals for clang++ to figure out what -isysroot and -I meant and felt like they might play a part in reconfiguring how the system header files were being compiled. From one of the threads below I discovered the $(R RHOME)/etc/Makeconf Makefile and proceeded to change the directory of the OSX SDK from what I believe was the Command Line Tools path to the Xcode path. This seemed to solve the issue.
Some threads I used to find this solution: Cannot compile R packages with c++ code after updating to macOS Catalina and Catalina C++: Using <cmath> headers yield error: no member named 'signbit' in the global namespace.
What worked for me:
Note the current header file search
From clang++ -Wp,-v -E -:
#include <...> search starts here:
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.3/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
Note the OSX SDK path
From xcrun --show-sdk-path:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
Modify the R configuration
With vim $(R RHOME)/etc/Makeconf, changed the CPPFLAGS flag to:
CPPFLAGS = -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I/usr/local/include
Notice how this SDK directory is what was found in step 2. This apparently cleans up the location and order of the SDK header file includes.
Reinstall glmmTMB from source
I was still seeing a FreeADFunObject error when trying to fit a glmmTMB model, so I went back to this GH thread and followed the first suggestion: to reinstall from source. This wasn't working before since I needed a properly set up c++ configuration, which I fixed in the last two steps.
Hence, install.packages("glmmTMB", type="source") installed without issue and I can now build and fit glmmTMB models.
For posterity's sake:
> 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

Can't get R to recognize installed libgfortran when installing packages

I'm trying to install packages in R that depend on libgfortran, but I keep getting this error:
dyld: Library not loaded: /usr/local/opt/gcc/lib/gcc/7/libgfortran.4.dylib
Referenced from: /usr/local/Cellar/r/3.4.3_1/lib/libR.dylib
Reason: image not found
I have gfortran installed through homebrew gcc:
❯❯ readlink `which gfortran`
../Cellar/gcc/8.2.0/bin/gfortran
There's a libgfortran available at /usr/local/opt/gcc/lib/gcc/8/libgfortran.5.dylib, but this is not the one R is looking for. How do I convince R to look for gcc 8 instead of gcc 7?
"How do I convince R to look for gcc 8 instead of gcc 7?"
You should not do that! Those libraries are NOT compatible, that's why soname is changed. The ABI is different. You need the correct version of GCC which was used to compile your R package. See very similar (but Linux) questions and answers like R v3.4.0-2 unable to find libgfortran.so.3 on Arch How to install libgfortran.so.4 on ubuntu 16.06
You really need the compatible library or update your R to a version that uses GCC 8.

undefined references when using Rcpp

I am trying to build an R package that is a wrapper around a C library (and which uses gsl) in Windows. I can transition to Linux, if necessary
For better or worse, I put the make commands for the C library in Makevars.in.
I used Rstudio's Rcpp skeleton feature to create the package. I have installed Rtools 3.3 and using R 3.3.1
and I included Rcpp and RcppGSL in LinkingTo field of DESCRIPTION. I am still getting undefined reference errors for the make command
$(CXX) $(CXXFLAGS) $(OBJECTS) ../inst/libgraphm.a $(PKG_LIBS)
A few of the error are as follows
c:/Rtools/mingw_64/bin/g++ -shared -s -static-libgcc -o RGraphM.dll tmp.def graphmatch_rcpp.o RcppExports.o -L../inst -lgraphm -LC:/tools/gsl/lib/x64 -lgsl -lgslcblas -Ld:/Compiler/gcc-4.9.3/local330/lib/x64 -Ld:/Compiler/gcc-4.9.3/local330/lib -LC:/PROGRA~1/R/R-33~1.1/bin/x64 -lR
#cd graphm && /usr/bin/make
c:/Rtools/mingw_64/bin/g++ -O2 -Wall -mtune=core2 graphmatch_rcpp.o RcppExports.o ../inst/libgraphm.a -L../inst -lgraphm -LC:/tools/gsl/lib/x64 -lgsl -lgslcblas
graphmatch_rcpp.o: In function `PreserveStorage':
C:/Users/sadali/Documents/R/win-library/3.3/Rcpp/include/Rcpp/storage/PreserveStorage.h:10: undefined reference to `__imp_R_NilValue'
graphmatch_rcpp.o: In function `Vector':
C:/Users/sadali/Documents/R/win-library/3.3/Rcpp/include/Rcpp/vector/Vector.h:58: undefined reference to `Rf_allocVector'
graphmatch_rcpp.o: In function `Rcpp_ReplaceObject':
The answer to this similar (in my eyes) question seems to be inapplicable
Undefined reference errors when including Rcpp.h
as I am using LinkingTo and trying to create an R package
Edit: as the first answer suggested, I have tried building RcppZiggurat with the following Makevars.win :
PKG_CPPFLAGS = -I. -I../inst/include -IC:/tools/gsl/include
## Use the R_HOME indirection to support installations of multiple R version
PKG_LIBS = $(LDFLAGS) -L../inst -L$(LIB_GSL)/lib/x64 $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "RcppGSL:::LdFlags()")
I am getting these errors.
ziggurat.o:ziggurat.cpp:(.text+0x86): undefined reference to `gsl_rng_free'
ziggurat.o:ziggurat.cpp:(.text+0xa6): undefined reference to `gsl_rng_free'
ziggurat.o:ziggurat.cpp:(.text+0xc6): undefined reference to `gsl_rng_free'
ziggurat.o:ziggurat.cpp:(.text+0x1254): undefined reference to `gsl_rng_set'
I was able to get the windows build running successfully using this template for Makevars.win
https://github.com/adalisan/RGraphM/blob/master/src/Makevars.win.gen
and this script creates the Makevars.win
https://github.com/adalisan/RGraphM/blob/master/configure.win
Once the pre-built gsl windows libraries have been put in the directory defined by env. var LIB_GSL, both the compilation and linking steps worked.
One small issue is that the zip file for the pre-built libraries downloaded here include there are two subdirectories for two sub-architectures (i386 and x64)
You should make sure that you link against the right version and that -L"$(LIB_GSL)\lib\$(ARCH)" argument in the linker calls evaluates to the right directory for the sub-arch.
Thanks to the creators of Rcpp and RcppGSL for making the packaging of gsl-based code easier.
You have a misunderstanding of how this works:
The LinkingTo: field of DESCRIPTION does not linking, despite its name. It only helps for header files. Which is why your package compiled but did not link.
You need linker instructions. As has been said before, the mvabund and RcppZiggurat packages both link to the GSL using a tool from RcppGSL.
RcppZiggurat in particular may be a good example to follow as it is small. There is also an entire example package included in RcppGSL -- which we use as reference as well as in unit testing.

gfortran not found when installing r-packages

I am repeatedly encountering the following error
/usr/bin/ld: cannot find -lgfortran
when trying to install a lot of r-packages (e.g. igraph, ergm, blockmodels, and I guess every package that requires gcc and fortran in some way or another)
I run R on Ubuntu Trusty, and I have recently upgraded R from 3.1.0 to 3.3.1.
When on 3.1.0, I had been able to install igraph and ergm (and others) without any problem, but not blockmodels.
I have checked for the presence of libgfortran on my system: it is there. Actually there is more than one version of it (3, 4.7, 4.8) and also libgfortran-dev is installed (multiple versions as well)
Any idea of what is going on?
Thank you for any advice.
In Arch Linux, the gfortran package is named gcc-fortran. To install it:
sudo pacman -S gcc-fortran

How to reproduce ERROR seen by CRAN maintainer / how to add .R/Makevars

As part of my PhD project I have completely redesigned an early draft of my R package quantspec. After I uploaded the tar.gz-file I obtained via BUILD, which passed CHECK on my Windows 7 computer, using the webform, I received an email that one of the examples in my package produced an ERROR.
I've tested my package by installing it via
devtools::install_github("tobiaskley/quantspec", ref="develop")
on different computers (> 5) that are using Windows, Linux and MacOS as operating systems, but was unable to reproduce the ERROR reported.
EDIT: It seems to me that I'm missing some important step when building and checking the package. In Section 4.3.6 of Writing R Extensions it says that I should add to ~/.R/Makevars something like (for gfortran)
FCFLAGS = -g -O2 -mtune=native -fbounds-check
FFLAGS = -g -O2 -mtune=native -fbounds-check
How do I do that?
I've finally managed to reproduce the error.
For some reason, on all systems (> 5) I have used to test my package, checking of the array bounds was turned off. It can be switched on by options(CBoundsCheck = TRUE) or via an environment variable R_C_BOUNDS_CHECK=yes.

Resources