lapack not linked to when compiling package quantreg - r

Package quantreg fails to install because it does not link to lapack:
install.packages("quantreg")
[snip]
gcc -std=gnu99 -shared -o quantreg.so akj.o boot.o brute.o chlfct.o cholesky.o combos.o crq.o crqfnb.o dsel05.o etime.o extract.o idmin.o iswap.o kuantile.o mcmb.o penalty.o powell.o rls.o rq0.o rq1.o rqbr.o rqfn.o rqfnb.o rqfnc.o rqs.o sparskit2.o srqfn.o srqfnc.o srtpai.o -L/home/myself/R/x86_64-pc-linux-gnu-library/2.15/Rcpp/lib -lRcpp -Wl,-rpath,/home/myself/R/x86_64-pc-linux-gnu-library/2.15/Rcpp/lib -lprofiler -lgfortran -lm -lquadmath -L/usr/lib/R/lib -lR
[snip]
unable to load shared object '/home/myself/R/x86_64-pc-linux-gnu-library/2.15/quantreg/libs/quantreg.so':
/home/myself/R/x86_64-pc-linux-gnu-library/2.15/quantreg/libs/quantreg.so: undefined symbol: dposv_
Weirdly enough, when creating a new user from scratch, everything works perfectly (and there is a "-llapack" in the logs).
I suspect some environment variables to be responsible for this, but could not identify which one: when unsetting LIBRARY_PATH and LD_LIBRARY_PATH, this still happens. Thanks for any idea!

I had a makevars file in .R that contained
PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` -lprofiler

Related

Error in dyn.load(dll_copy_file) --A error when building R package via linux system

I am currently building a local R package with Rcpp and FFTW3.
When I run devtools::document() the error shows up, below is the full error message:
Error in dyn.load(dll_copy_file) :
unable to load shared object '/tmp/RtmpcsGXOO/pkgload7f8a5462dce4/pmd.so':
/tmp/RtmpcsGXOO/pkgload7f8a5462dce4/pmd.so: undefined symbol: fftw_execute
pmd is my package name. I am wondering it might relate to my Makevar file.
My Makevars.in looks like this:
PKG_CPPFLAGS=#CPPFLAGS# #FFTW_CFLAGS#
PKG_LIBS=#LIBS# #FFTW_LIBS#
all: $(SHLIB)
PKG_CPPFLAGS = -I../inst/include -I/usr/local/include
PKG_LIBS= -L/usr/local/lib -lfftw3
Actually, I have no idea how to write a Makevars file, the way I wrote this Makevars is copying Makevars from other existing packages.
Originally, I used C to perform what I want, and I used dyn.load after typing following codes in terminal so that I could .C() those C functions in R:
C_INCLUDE_PATH=~/usr/include
export C_INCLUDE_PATH
export LD_RUN_PATH=$HOME/usr/lib
R CMD SHLIB functions.c -I$HOME/usr/include -L$HOME/usr/lib -lfftw3 -lgsl -lgslcblas -lm -o functions.so
.

Invoking R CMD SHLIB through system() doesn't find GSL library

I have a C program that uses GNU Scientific Library (GSL) for root finding and I want to make this into a shared library (.so or .dll) for use in R.
Say my GSL_MRE.c file looks like this:
#include <gsl/gsl_math.h>
#include <gsl/gsl_roots.h>
#include <gsl/gsl_errno.h>
#include <R.h>
int gsl_rootsolve ()
{
const gsl_root_fdfsolver_type *T;
gsl_root_fdfsolver *s;
T = gsl_root_fdfsolver_newton;
s = gsl_root_fdfsolver_alloc (T);
printf ("using %s method\n", gsl_root_fdfsolver_name (s));
gsl_root_fdfsolver_free (s);
return 0;
}
If I run the following in Terminal on MacOS 10.13.6:
$ R CMD SHLIB GSL_MRE.c -lgsl -lgslcblas -lm -L/opt/local/lib
($ is the terminal prompt) it creates GSL_MRE.so that I can load by executing in R:
R> dyn.load(paste("GSL_MRE", .Platform$dynlib.ext, sep = ""))
(R> is R prompt)
But when I try to create the .so file from R with system():
R> system("R CMD SHLIB GSL_MRE.c -lgsl -lgslcblas -lm -L/opt/local/lib")
It can't find the header files. I get:
GSL_MRE.c:1:10: fatal error: 'gsl/gsl_math.h' file not found
#include <gsl/gsl_math.h>
^~~~~~~~~~~~~~~~
1 error generated.
make: *** [GSL_MRE.o] Error 1
I've been trying to set all sorts of FLAGS in ~/.R/Makevars, but nothing works. My Makevars currently only contains the following line:
CC=gcc -Wall -Wextra
Why does system() from R not work? And how do I make the .so from R programmatically?
There are a few packages that wrap around the GSL, I recommend you study them. At a minimum you could use gsl-config (which you should test for) to derive include and linker statements. On my Uvuntu system:
edd#rob:~$ gsl-config --cflags
-I/usr/include
edd#rob:~$ gsl-config --libs
-L/usr/lib/x86_64-linux-gnu -lgsl -lgslcblas -lm
edd#rob:~$
More fundamentally, you got trapped between linking (where you correctly pointo to the GSL libraries) and compiling (where you have told the compiler where your GSL headers reside). You apprently need to tell it on your system.
Also, some packages provide facilities. My RcppGSL package offers some simple vector and matrix wrappers, but also deals with GSL detection so that for example my RcppZiggurat (which uses GSL RNGs) can use:
edd#rob:~$ cat git/rcppziggurat/src/Makevars
PKG_CPPFLAGS = -I. -I../inst/include
## Use the R_HOME indirection to support installations of multiple R version
PKG_LIBS = `$(R_HOME)/bin/Rscript -e "RcppGSL:::LdFlags()"`
edd#rob:~$
It's not a trivial problem as you need to sort our compiling and linking, but it has been done before so you can lean on that.

R CMD check error : 'DESCRIPTION' file missing error despite having been recognized before in the prompt

I am trying to build and check a package with some Fortran code in it under Windows 10
with R 3.4.0 updating some code previously in R 2.8.
I am doing this in three steps in the command window:
>R CMD build src
[...]
* checking DESCRIPTION meta-information ... OK
So I assume everything is ok with the DESCRIPTION file
then:
>R CMD check --no-examples --no-tests src
I get among other stuff:
* checking for file 'src/DESCRIPTION' ... OK
A bit further it gets wrong at
* checking whether package 'pckgname' can be installed
Looking at the log inside the file /src.Rcheck/00install.out, I read:
Error in .get_package_metadata(dir, FALSE) :
Files 'DESCRIPTION' and 'DESCRIPTION.in' are missing.
ERROR: installing Rd objects failed for package 'samara'
I have copied and pasted the DESCRIPTION file pretty much everywhere it made sense, including the top level directory and the \src directory or in those directory alone in some other trials, with always the same result.
This is strange because sometimes the DESCRIPTION file is recognized, sometimes it is not
There is only one similar post treating this subject:
R package building error
the author seems to have solved his problem but he doesn't know how.
Does anybody know what happened?
Following some requests, here is the content of the files 00check.log and 00install.out:
00check.log
C:\Users\username\Documents\PCKGNAME\SRC_5.0.2E\pckgname>R CMD check --no- examples --no-tests src
* using log directory 'C:/Users/username/Documents/PCKGNAME/SRC_5.0.2E/pckgname/src.Rcheck'
* using R version 3.4.0 (2017-04-21)
* using platform: x86_64-w64-mingw32 (64-bit)
* using session charset: ISO8859-1
* using options '--no-examples --no-tests'
* checking for file 'src/DESCRIPTION' ... OK
* checking extension type ... Package
* this is package 'pckgname' version '5.0.2.5'
* checking package namespace information ... OK
* checking package dependencies ... OK
* checking if this is a source package ... NOTE
Found the following apparent object files/libraries:
src-x64/FortPrg1.o src-x64/FortPrg2.o src-x64/FortPrg3.o
Object files/libraries should not be included in a source package.
* checking if there is a namespace ... OK
* checking for executable files ... OK
* checking for hidden files and directories ... NOTE
Found the following hidden files and directories:
R/.Rhistory
These were most likely included in error. See section 'Package
structure' in the 'Writing R Extensions' manual.
* checking for portable file names ... OK
* checking whether package 'pckgname' can be installed ... ERROR
Installation failed.
See 'C:/Users/username/Documents/PCKGNAME/SRC_5.0.2E/pckgname/src.Rcheck/00install.out' for details.
* DONE
Status: 1 ERROR, 2 NOTEs
In Prompt, there is this information in addition to what appeared before:
* checking whether package 'pckgname' can be installed ...
Warning: l'exécution de la commande '"C:/PROGRA~1/R/R-34~1.0/bin/x64/Rcmd.exe"
INSTALL -l "C:/Users/username/Documents/PCKGNAME/SRC_5.0.2E/pckgname/src.Rcheck" --no-html
"C:\Users\username\DOCUME~1\PCKGNAME\SRC_30~1.7E\pckgname\src"' renvoie un statut 1
Now in the file 00install.out:
* installing *source* package 'pckgname' ...
** libs
*** arch - i386
c:/Rtools/mingw_32/bin/gfortran -O3 -mtune=core2 -c FortPrg1.f90 -o FortPrg1.o
c:/Rtools/mingw_32/bin/gfortran -O3 -mtune=core2 -c FortPrg2.f90 -o FortPrg2.o
c:/Rtools/mingw_32/bin/gfortran -O3 -mtune=core2 -c FortPrg3.f90 -o FortPrg3.o
c:/Rtools/mingw_32/bin/gfortran -shared -s -static-libgcc -o pckgname.dll tmp.def FortPrg1.o FortPrg2.o FortPrg3.o -Ld:/Compiler/gcc-4.9.3/local330/lib/i386 -Ld:/Compiler/gcc-4.9.3/local330/lib -LC:/PROGRA~1/R/R-34~1.0/bin/i386 -lR
installing to C:/Users/username/Documents/PCKGNAME/SRC_5.0.2E/pckgname/src.Rcheck/pckgname/libs/i386
*** arch - x64
c:/Rtools/mingw_64/bin/gfortran -O2 -mtune=core2 -c FortPrg1.f90 -o FortPrg1.o
c:/Rtools/mingw_64/bin/gfortran -O2 -mtune=core2 -c FortPrg2.f90 -o FortPrg2.o
c:/Rtools/mingw_64/bin/gfortran -O2 -mtune=core2 -c FortPrg3.f90 -o FortPrg3.o
c:/Rtools/mingw_64/bin/gfortran -shared -s -static-libgcc -o pckgname.dll tmp.def FortPrg1.o FortPrg2.o FortPrg3.o -Ld:/Compiler/gcc-4.9.3/local330/lib/x64 -Ld:/Compiler/gcc-4.9.3/local330/lib -LC:/PROGRA~1/R/R-34~1.0/bin/x64 -lR
installing to C:/Users/username/Documents/PCKGNAME/SRC_5.0.2E/pckgname/src.Rcheck/pckgname/libs/x64
** R
** demo
** preparing package for lazy loading
Warning: package 'survival' was built under R version 3.4.4
Warning: package 'boot' was built under R version 3.4.4
Warning: package 'xlsx' was built under R version 3.4.4
Warning: package 'ggplot2' was built under R version 3.4.4
Warning: package 'GGally' was built under R version 3.4.4
[1] "Chargement de gdata ..."
[1] "Chargement de survival ..."
[1] "Chargement de boot ..."
[1] "Chargement de tcltk ..."
[1] "Chargement de xlsx ..."
---------------------------------------------
Package PCKGNAME genere le ven. sept. 28 17:14:21 2018
---------------------------------------------
Librairie Pckgname V5.0.2E Chargee
** help
No man pages found in package 'pckgname'
Error in .get_package_metadata(dir, FALSE) :
Files 'DESCRIPTION' and 'DESCRIPTION.in' are missing.
ERROR: installing Rd objects failed for package 'pckgname'
* removing 'C:/Users/username/Documents/PCKGNAME/SRC_5.0.2E/pckgname/src.Rcheck/pckgname'
Usually you would store a package named samara in a directory of the same name. You appear to have it in src. That should work, but I wouldn't be surprised if something in the check code implicitly assumes you were following the usual convention.
The other thing that is usually done is that you create a source tarball of the package and check that. That is, you run R CMD check on the output of R CMD build, not on the input. Again, what you are doing should mostly work (though not perfectly in this case; tarball contents typically are not identical to source directories), but perhaps these two nonstandard ways of working are causing your problems.
With the help of user2554330, I solved the problem.
I did the following things:
-I reorganized into pckgname/R and pckgname/src
(instead of pckgname/src/R and pckgname/src/src previously )
-I used R Studio. First I created a new project (create a new package) with RStudio and devtools: File>Create new project>R Package using devtools. And I put my files there
-I built and checked exclusively with the Rstudio interface

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.

How use the option -std=c99 for installing R packages

I have problem in installing plyr R package, and got the following error:
Installing package into '/home/mousavian/R/x86_64-pc-linux-gnu-library/3.2'
(as 'lib' is unspecified)
* installing *source* package 'plyr' ...
** package 'plyr' successfully unpacked and MD5 sums checked
** libs
g++ -I/share/apps/R/lib64/R/include -DNDEBUG -I/usr/local/include -I"/home/mousavian/R/x86_64-pc-linux-gnu-library/3.2/Rcpp/include" -fpic -g -O2 -c RcppExports.cpp -o RcppExports.o
gcc -I/share/apps/R/lib64/R/include -DNDEBUG -I/usr/local/include -I"/home/mousavian/R/x86_64-pc-linux-gnu-library/3.2/Rcpp/include" -fpic -g -O2 -c loop_apply.c -o loop_apply.o
loop_apply.c: In function 'loop_apply':
loop_apply.c:15:3: error: 'for' loop initial declarations are only allowed in C99 or C11 mode
for(int i = 0; i < n1; i++) {
^
loop_apply.c:15:3: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code
make: *** [loop_apply.o] Error 1
ERROR: compilation failed for package 'plyr'
* removing '/home/mousavian/R/x86_64-pc-linux-gnu-library/3.2/plyr'
Warning message:
In install.packages("R packages/plyr_1.8.3.tar.gz", repos = NULL) :
installation of package 'R packages/plyr_1.8.3.tar.gz' had non-zero exit status
How can I use -std=c99 option when try to install R packages from the source by install.packages command?
Thanks
Try C11 First
The compiler error tells you to choose either C99 or C11. So unless you're knowingly compiling legacy code, try C11 first. You can always then try the legacy C99 if that doesn't work.
Use withr::with_makevars
Editing individual package Makevars or coordinating global changes to your include/lib directories for every package that doesn't directly compile from source is not a happy strategy for the future. Don't do that! A great alternative to editing your R's make configuration is to use withr::with_makevars to manipulate the Makevars only for the installation command:
library(withr)
with_makevars(c(PKG_CFLAGS = "-std=c11"),
install.packages("plyr", repos = NULL, type = "source"),
assignment = "+=")
Plus, you likely already have withr installed since it's a devtools dependency.
If not already existing, create a directory in your $HOME (/home/mousavian/.R in your case). Inside, create a Makevars file (no extension). Edit this file with your favorite editor and write:
CC = gcc -std=c99
Then, save it and after starting R, simply run
install.packages("plyr", dependencies = TRUE)
It should compile with gcc -std=c99.
The default /etc/R/Makeconf includes CC = gcc -std=gnu99 (R 3.3.2 and probably most or all other recent versions, although I think the in-progress development version jumps up into the 21st century)
The accepted answer may well work, but is not the R default. Setting CC=gcc with no other options would drop this default, and this another reason for getting this error. Best to leave defaults alone unless you know what you're doing.
As an addendum, to possibly save someone a few minutes: I ran into a similar issue after an R update to 3.3.2 on an Rstudio server (that I have no control over), but with g++. Compiler options for g++ may be added to Makevars like this:
CXX1XSTD = -std=gnu++11
(the package I needed to compile required gnu++11)

Resources