stdio.h not found during r package installation - r

I have been having some difficulties with my R install. I realised that I was unable to install packages and thought that it was due to having been tinkering with the packages I had installed recently. As I am a novice I thought that a clean install might fix it so I uninstalled R and Rstudio (via homebrew).
On reinstall I'm faced with the same issue. This is part of the console output from
install.packages("tidyverse")
/usr/local/opt/llvm/bin/clang -fopenmp -I"/usr/local/Cellar/r/3.5.1/lib/R/include" -DNDEBUG -I/usr/local/opt/gettext/include -I/usr/local/opt/llvm/include -fPIC -g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe -c assumptions.c -o assumptions.o
In file included from assumptions.c:21:
/usr/local/Cellar/r/3.5.1/lib/R/include/Rinternals.h:39:11: fatal error: 'stdio.h' file not found
# include <stdio.h>
^~~~~~~~~
1 error generated.
Searching this site and others suggested that this is to do with lacking the Xcode command line tools. I have checked that these are installed.
Searching for stdio.h in terminal shows numerous versions in the various subfolders (tvos, iOS etc.)
other missing files include: stdlib.h, assert.h and unistd.h
I can provide the full output if that would help

Further searches have identified that in Xcode 10 (as is bundled with macOS Mojave), the location of the standard path for stdio.h and other utilities has moved.
"Legacy software" that looks for the macOS headers in the base system under /usr/include will not find the required files.
To patch this, Xcode includes a package to create links for such software to find the files.
For Xcode 10 the package file is located at:
/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
This information was sourced from the apple Developer forums
After installing this package and restarting the computer I have successfully installed the tidyverse.
I did run into another issue where my C compiler was not able to create executables but this was solved in this thread.

Related

Installing R on OSX Big Sur (EDIT: and Apple M1) for use with Rcpp and openMP

There's probably a gazillion threads on OSX+Rcpp+openMP, but the bottom line right now appears to be this (per coatless):
Unfortunately, with R 4.0.0 the CRAN distributed version of R loses
the ability to use OpenMP without a custom setup.
I came across other ideas, including compiling llvm yourself, using homebrew or macports to install R and/or llvm and/or gcc, and then figuring out how to use the right compiler and/or flags with (R)cpp. However, I find this all very confusing.
I am not a mac user, but it seems to me that setting up a mac to compile Rcpp packages or code snippets with openMP seems to be too difficult for most mac users. However, I would like my R package on github to be used by more users, and since it relies on openMP, I am losing that audience.
Can someone provide the necessary steps to set up R on mac in a way that it can compile Rcpp code with openMP? I'd like to turn that into a quick tutorial.
EDIT: I should have added - on Apple Silicon, because there are some extra confusions where things go - /usr/local vs /opt
I spent a day figuring this out (original post here); here are the steps I used to compile R packages from source with openMP:
Install xcode from the app store (instructions for installing xcode) then install/reinstall the xcode command line tools from the terminal:
# To delete an existing command line tools installation:
sudo rm -rf /Library/Developer/CommandLineTools
# To install the command line tools
sudo xcode-select --install
Install gcc via Homebrew (instructions for installing Homebrew) or, if you already have gcc installed, skip to step 3.
# WARNING: This can take several hours
brew install gcc
To avoid "legacy" version issues:
brew cleanup
brew update
brew upgrade
brew reinstall gcc
Link some headers into /usr/local/include
sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/
# You can ignore warnings like this:
#ln: /usr/local/include//tcl.h: File exists
#ln: /usr/local/include//tclDecls.h: File exists
#ln: /usr/local/include//tclPlatDecls.h: File exists
#ln: /usr/local/include//tclTomMath.h: File exists
#ln: /usr/local/include//tclTomMathDecls.h: File exists
#ln: /usr/local/include//tk.h: File exists
#ln: /usr/local/include//tkDecls.h: File exists
#ln: /usr/local/include//tkPlatDecls.h: File exists
Check your version of gfortran (cd /usr/local/gfortran/lib/gcc/x86_64-apple-darwin19/; ls) then edit your ~/.R/Makevars file (if you don't have a file called Makevars in your ~/.R/ directory) and include only these lines:
LOC = /usr/local/gfortran
CC=$(LOC)/bin/gcc -fopenmp
CXX=$(LOC)/bin/g++ -fopenmp
CXX11 = $(LOC)/bin/g++ -fopenmp
CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe
LDFLAGS=-L$(LOC)/lib -Wl,-rpath,$(LOC)/lib
CPPFLAGS=-I$(LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
# (check that the version of gfortran - in this case 10.2.0 - matches the version specified in FLIBS)
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin19/10.2.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm
CXX1X=/usr/local/gfortran/bin/g++
CXX98=/usr/local/gfortran/bin/g++
CXX11=/usr/local/gfortran/bin/g++
CXX14=/usr/local/gfortran/bin/g++
CXX17=/usr/local/gfortran/bin/g++
Open R and install a package to test that it compiles with openMP enabled (when asked, compile from source = "Yes"):
install.packages("data.table", type = "source")
Unfortunately, I do not believe a more "simple" setup exists.
Eventually, I found a process that works on a M1 mac with Big Sur.
Head over to https://mac.r-project.org/, it contains most things you will need
Download and install R via R-4.1-branch.pkg. The CRAN version might also work, but I used the installer from mac.r-project.org, which required opening the osx security settings to allow the installation.
Install RStudio, start it, and let it install the developer tools. Alternatively, run sudo xcode-select --install in Terminal.
Head to https://mac.r-project.org/openmp/. Download openmp-11.0.1-darwin20-Release.tar.gz and install it (see Terminal commands below).
curl -O https://mac.r-project.org/openmp/openmp-11.0.1-darwin20-Release.tar.gz
sudo tar fvx openmp-11.0.1-darwin20-Release.tar.gz -C /
Now we need to add compiler flags so that clan uses openMP. In Terminal, create the Makevars file.
cd ~
mkdir .R
nano .R/Makevars
in nano, paste these additional compiler flags into the Makevars file:
CPPFLAGS += -Xclang -fopenmp
LDFLAGS += -lomp
Hit Control+O, Control+X to save and close
Head over to the gfortran page: https://github.com/fxcoudert/gfortran-for-macOS/releases
Use the installer gfortran-ARM-11.0-BigSur.pkg to install gfortran.
For some reason it appears to install in /usr/local/gfortran, but R expects it in /opt. The mac-R team likes to separate arm64 and intel related files. We could go and fix paths, or simply also install gfortran under /opt. Download the tar file gfortran-ARM-11.0-BigSur.tar.xz. You can use curl, or just download it and point tar in the command line to it.
cd /opt/R/arm64/
sudo mkdir gfortran
sudo tar -xzyf gfortran-ARM-11.0-BigSur.tar.xz -C /opt/R/arm64/
(replace gfortran-ARM-11.0-BigSur.tar.xz with /users/YOURUSERNAME/downloads/gfortran-ARM-11.0-BigSur.tar.xz)
Now it should work.
Not an expert in OSX, but doing this so others can figure out how to use my R package. I'd like to streamline the process some more, but wiping the mac, reinstalling osx and testing it takes so much time.

Installing Packages Takes A Very Long Time on Ubuntu

I was using Windows as OS and RStudio for windows, switched to Ubuntu today and installed R and RStudio again. When I try to install some packages from CRAN (only tidyverse !) using install.packages(), I see something something on console I have never seen on Windows, it looks like this;
* installing *source* package ‘data.table’ ...
** package ‘data.table’ successfully unpacked and MD5 sums checked
** using staged installation
gcc -std=gnu99 9.3.0
zlib 1.2.11 is available ok
OpenMP supported
** libs
gcc -std=gnu99 -I"/usr/share/R/include" -DNDEBUG -fopenmp -fpic -g -O2 -fdebug-prefix-map=/build/r-base-5iUtQS/r-base-4.0.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c assign.c -o assign.o
gcc -std=gnu99 -I"/usr/share/R/include" -DNDEBUG -fopenmp -fpic -g -O2 -fdebug-prefix-map=/build/r-base-5iUtQS/r-base-4.0.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c between.c -o between.o
...
then it continues like this and installing process is taking much longer than the time I encountered on Windows. (tidyverse which includes about seven packages took 30 minutes)
Is this normal ? if it is not, how can I solve it ?
A summary of the comments that helped resolve the issue.
In general, the default installation method in linux is type="source", which means that any package with non-R source code (e.g., C++, Fortran) needs to be compiled. Further, CRAN and all of its repository mirrors only provide binary packages windows and macos.
It appears that there are two ways to install binary packages on linux vice the default:
#duckmayr's suggestion to read https://cran.r-project.org/bin/linux/ubuntu/README.html (similar pages exist for other linux distros) identifies how to set up the CRAN2deb4ubuntu PPA, so that one can the the OS-level software management apt (and related tools). These ubuntu/debian packages are maintained well and reasonably up-to-date, though not nearly as frequently updated as a straight-shot to the CRAN servers.
But instead of the long-running R command install.packages("tidyverse"), you can simply run this for a binary installation, same effect:
$ sudo apt-get install r-cran-tidyverse
Unfortunately, the PPA does not include every single contributed package supplied to CRAN (as that would take significant effort on the PPA maintainers' side, trying to keep up with CRAN's daily onslaught of new packages and package-updates ... the testing alone sounds prohibitive). (The number suggested is 4000+ packages in the PPA, out of CRAN's 16,278 packages (as of 30 seconds ago).
A recent addition to the repository scene is RStudio's Public Package Manager. In a recent blog post, RStudio announced it to have (at least) three fundamental features:
Access to pre-compiled packages on Linux via install.packages ...
Historical checkpoints for CRAN enabling reproducible work ...
Expanded Windows support for older versions of R ...
(That's just a small snapshot of the blog post, I suggest you read the original for more details and context.)
A quick search (of packages for Ubuntu 20.04 Focal) reveals: 15,217 binary and 16,216 source packages.
Borrowing from RStudio's "Setup" page, for Ubuntu 20.04 Focal you can set this as your repository with
options(repos = c(PkgMgr="https://packagemanager.rstudio.com/all/__linux__/focal/latest"))
(I named it "PkgMgr", that's arbitrary. You may want/need more repos, over to you, see ?options and ?setRepository. This is a sample only, provided for convenience; please go to RStudio's documentation for how to set up your R for your installation.)

'RcppArmadillo.h' file not found when installing RcppArmadillo from source

RcppArmadillo 0.8.400.0.0 works just fine on my computer, both from sourceCpp and cxxfunction. Now I want to upgrade to 0.8.500.0, which requires compilation from source. Then I got the error
ccache /usr/local/clang6/bin/clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include -I/Applications/CPLEX_Studio128/cplex/include -I/Applications/CPLEX_Studio128/concert/include -DIL_STD -fPIC -Wall -g -O2 -c RcppArmadillo.cpp -o RcppArmadillo.o
RcppArmadillo.cpp:22:10: fatal error: 'RcppArmadillo.h' file not found
#include <RcppArmadillo.h>
^~~~~~~~~~~~~~~~~
1 error generated.
I got this error whether I ran install.packages("RcppArmadillo") in RStudio or in R console outside RStudio. My compiler is clang4, and I have no problem installing RcppParallel or compiling my own Rcpp code that uses RcppArmadillo and RcppParallel. R version: 3.5.0, MacOS High Sierra.
Edit: I looked at https://github.com/RcppCore/RcppArmadillo/issues/143, which is very relevant. I did install clang6 and gfortran 6.1, both downloaded from the CRAN website. I also changed the .R/Makevars file to reflect the changes to clang6. I have also installed ccache as suggested, to make compilation faster. Here's my Makevars:
F77 = /usr/local/gfortran/bin/gfortran
FC = $F77
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm
CC=ccache /usr/local/clang6/bin/clang
SHLIB_CXXLD=ccache /usr/local/clang6/bin/clang++
CXX= ccache /usr/local/clang6/bin/clang++ -Wall
CXX1X= ccache /usr/local/clang6/bin/clang++
CXX98= ccache /usr/local/clang6/bin/clang++
CXX11= ccache /usr/local/clang6/bin/clang++
CXX14= ccache /usr/local/clang6/bin/clang++
CXX17= ccache /usr/local/clang6/bin/clang++
LDFLAGS=-L/usr/local/clang6/lib
From the outputs in the R console when I was installing the package (added to the error message above), I can tell that clang6 was used for compilation. I also found the downloaded RcppArmadillo tarball, unpacked it, and ran ./configure from the command line. Nothing seemed wrong there; no config.log file was generated. This doesn't seem to be a configuration problem. Then I still got the exact same error when trying to install the package in R, after configuration, in libs. I also didn't get any message about -fopenmp. It seems that my problem is a bit different from that in the GitHub ticket.
R 3.5.x requires clang6 and adding clang6 to your system PATH variable.
Steps:
x Download and install the clang-6.0.0.pkg official package from CRAN.
x Delete your ~/.R/Makevars as it is likely set to use clang4. To do this, open Terminal and type:
sudo rm ~/.R/Makevars
x Then add to your ~/.bash_profile the following:
if [ -d "/usr/local/clang6/bin" ] ; then
export PATH="/usr/local/clang6/bin:$PATH"
fi
Note: If you are using a shell other than bash you will need to add the above into the appropriate file, e.g. for zsh add to ~/.zshrc, as this was not implemented in the official installer.
I should have a few cycles to finish writing a new unofficial Rtools installer a bit later. See: https://github.com/coatless/r-macos-rtools/issues/7
Note: Alternatively, you can remove the paths individual with
sudo vi ~/.R/Makevars
Pressing I to enter insert mode, deleting lines with clang4 in the path, and then pressing ESC + :wq to write changes to file.
Never mind, this problem went away with version 0.8.600. Later, this problem appeared again when I upgraded to RcppArmadillo 0.9.100.5.0. Somehow I only got this error when building from source; I downloaded the binary manually from the CRAN website, and used R CMD INSTALL to install the prebuilt version, and it worked; somehow R doesn't seem to know that there is a binary available for the newest version.
I did install clang6 and modified .R/Makevars to use clang6, but the same problem persisted; probably something else caused the problem.

R package installation with gfortran warnings in OSX

I have now been searching the web and tried different methods to get a R library (ctsmr) installed from the source, but without luck. I know it works on Linux and Windows, but have not succeeded on my Mac (OSX v10.12.6) with R v3.3.3.
When I try to install it like this:
install.packages("ctsmr", repo="http://ctsm.info/repo/dev", type="source")
I get the some warnings during the installation process, but the installation finishes.
* installing *source* package ‘ctsmr’ ...
** libs
gfortran-4.8 -fopenmp -fPIC -Wall -g -O2 -c fdf.f90 -o fdf.o
gfortran-4.8: warning: couldn’t understand kern.osversion ‘16.7.0
fdf.f90:19.30:
INTEGER I,ISTOP
1
Warning: Unused variable 'istop' declared at (1)
gfortran-4.8 -fPIC -g -O2 -c matutil.f -o matutil.o
gfortran-4.8: warning: couldn’t understand kern.osversion ‘16.7.0
gfortran-4.8 -fPIC -g -O2 -c opkda1.f -o opkda1.o
...
When I run an example in RStudio (the one that is called "Building 1" on the packages webpage, http://ctsm.info) I get an error message for one of the function from the package. (This error message actually does not show up at the moment, as RStudio just crash when I run the line):
fit <- model$estimate(data=X)
Error in dyn.load(x = m$libfile) :
unable to load shared object '/var/folders/7g/81dqdb9s4qq2r79ncggcv2l40000gn/T//RtmpNoNnQl/model5a613c359b3f.so':
dlopen(/var/folders/7g/81dqdb9s4qq2r79ncggcv2l40000gn/T//RtmpNoNnQl/model5a613c359b3f.so, 6): Symbol not found: _GOMP_parallel_end
Referenced from: /var/folders/7g/81dqdb9s4qq2r79ncggcv2l40000gn/T//RtmpNoNnQl/model5a613c359b3f.so
Expected in: dynamic lookup
It looks a little different if I run the same script in the R application:
fit <- model$estimate(data=X)
*** caught illegal operation ***
address 0x7fffa39b0e97, cause 'illegal opcode'
Trackback:
1: dyn.load(x = m$libfile)
2: CallFunction("ctsmdriver", .self, rho, verbose = verbose)
3: model$estimate(data = X)
So, I am wondering if the package is not installed correctly or if their is a bug in the package (maybe because of the double slashes in the path in the error message).
If I manually go look for the shared object, model5a613c359b3f.so, it actually exist on my computer.
Regarding the gfortran compiler I get the following form the therminal:
~ $ gfortran --version
GNU Fortran (GCC) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
~ $ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
I would appreciate if someone could tell me if it is a installation error or a bug in the package. And secondly, come up with possible solutions.

compiling package with Rcpp outside of R-Studio in Windows

I've successfully build my own package with Rcpp in R-Studio. However, when building the package in the Windows console, there are some error messages complaining file missing of R.h.
I set the path and R_Home environments with
SET PATH=D:\RTools\gcc-4.6.3\bin;D:\R3\bin;D:\RTools\bin;
SET R_HOME=D:\R3\
And the build command is
R CMD INSTALL --byte-compile --build mypkg
Below are the compiling error messages:
g++ -m32 -I"/include" -DNDEBUG -I"D:/R3/library/Rcpp/include" -I"d:
/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c Rcpp
Exports.cpp -o RcppExports.o
In file included from D:/R3/library/Rcpp/include/Rcpp.h:27:0,
from RcppExports.cpp:4:
D:/R3/library/Rcpp/include/RcppCommon.h:35:15: fatal error: R.h: No su
ch file or directory
compilation terminated.
You are missing an include for R.h, as the error says.
And if you look at your compile line, the statement
-I"/include"
is wrong relative to your stated R_HOME in D:/R3
You need to check your setup, somehow you confused R from using the correct include directory.
Rcpp builds just fine on Windows, you can even check by submitting your package to the win-builder service.

Resources