2

I am trying to set up Rcpp GSL for windows.. I read lot of post on the subject, but I have been unable to find a solution for a while now.. Sorry in advance..

So I am running R 3.1 on windows 8.1. Both Rcpp and Rcpp Armadillo are working fine..

R version 3.1.0 (2014-04-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RcppGSL_0.2.0           RcppArmadillo_0.4.300.0 Rcpp_0.11.1             foreign_0.8-61         

loaded via a namespace (and not attached):
[1] tools_3.1.0

1) I made sure that there is no space in the R directory 2) I installed gnuwin32 3) I changed my Makevars.win

PKG_CPPFLAGS=-I$(LIB_GSL=C:/GnuWin32)/include -I../inst/include
PKG_LIBS=-L$(LIB_GSL=C:/GnuWin32)/lib -lgsl -lgslcblas $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "Rcpp:::LdFlags()")

4) Here is my Makeconf, but let's be honest here.. I have no idea what this is saying

#-*- Makefile -*-
# $(R_HOME}/etc$(R_ARCH)/Makeconf

# Hand-edited version for gnuwin32.

ifdef DEBUG
  DLLFLAGS=
  DEBUGFLAG=-gdwarf-2
else
  DLLFLAGS=-s
  DEBUGFLAG=
endif

DLLFLAGS+= -static-libgcc
LINKFLAGS+= -static-libgcc

## Things which are substituted by fixed/Makefile (and also -O2 -> -O2)
WIN = 64
BINPREF = 
# SYMPAT = 's/^.* [BCDRT] _/ /p' for 32-bit
# SYMPAT = 's/^.* [BCDRT] / /p' for 64-bit
SYMPAT = 's/^.* [BCDRT] / /p'
IMPDIR = bin/x64
# flags for multilib builds, for compilers, dlltool and windres
M_ARCH = -m64
DT_ARCH = -m i386:x86-64 --as-flags --64
RC_ARCH = -F pe-x86-64
# Used by packages tcltk and tkrplot
TCL_HOME = $(R_HOME)/Tcl
LOCAL_SOFT = d:/RCompile/CRANpkg/extralibs64/local

ifneq ($(strip $(LOCAL_SOFT)),)
LOCAL_CPPFLAGS = -I"$(LOCAL_SOFT)/include"
LOCAL_LIBS = -L"$(LOCAL_SOFT)/lib$(R_ARCH)" -L"$(LOCAL_SOFT)/lib"
endif

DLLTOOL = $(BINPREF)dlltool --as $(BINPREF)as $(DT_ARCH)
DLLTOOLFLAGS = -k
NM = $(BINPREF)nm
RESCOMP = $(BINPREF)windres $(RC_ARCH)
## MAIN_LD needs to be set by the package
LINKER = $(MAIN_LD)
## as a default
DLL = $(CC)

## Things defined in Renviron on Unix
SED = sed
TAR = tar

## Things defined in MkRules
CAT = cat
CP = cp
MKDIR = mkdir
RM = rm -f
SORT = sort

## For use in packages
GRAPHAPP_LIB = -lRgraphapp
TCL_VERSION = 85
ZLIB_LIBS = -lRzlib

AR = $(BINPREF)ar
## Used by packages 'maps' and 'mapdata'
AWK = gawk
BLAS_LIBS = -L"$(R_HOME)/$(IMPDIR)" -lRblas
C_VISIBILITY = 
CC = $(BINPREF)gcc $(M_ARCH)
CFLAGS = -O2 -Wall $(DEBUGFLAG) -std=gnu99 -mtune=core2
CPICFLAGS = 
CPPFLAGS =
CXX = $(BINPREF)g++ $(M_ARCH)
CXXCPP = $(CXX) -E
CXXFLAGS = -O2 -Wall $(DEBUGFLAG) -mtune=core2
CXXPICFLAGS = 
CXX1X = $(BINPREF)g++ $(M_ARCH)
CXX1XFLAGS = -O2 -Wall $(DEBUGFLAG) -mtune=core2
CXX1XPICFLAGS =
CXX1XSTD = -std=c++0x
DYLIB_EXT = .dll
DYLIB_LD = $(DLL)
DYLIB_LDFLAGS = -shared
DYLIB_LINK = $(DYLIB_LD) $(DYLIB_LDFLAGS) $(LDFLAGS)
ECHO = echo
ECHO_C = 
ECHO_N = -n
ECHO_T = 
FC = $(BINPREF)gfortran $(M_ARCH)
FCFLAGS = -O2 $(DEBUGFLAG) -mtune=core2
# additional libs needed when linking with $(FC), e.g. on Solaris
FCLIBS =
F77 = $(BINPREF)gfortran $(M_ARCH)
F77_VISIBILITY = 
FFLAGS = -O2 $(DEBUGFLAG) -mtune=core2
FLIBS = -lgfortran
FCPICFLAGS =
FPICFLAGS = 
FOUNDATION_CPPFLAGS =
FOUNDATION_LIBS =
JAR =
JAVA =
JAVAC = 
JAVAH =
# JAVA_HOME = 
# JAVA_LD_LIBRARY_PATH = 
JAVA_LIBS = -L"$(JAVA_HOME)/bin/client" -ljvm -ljvm-w32
JAVA_CPPFLAGS = -I"$(JAVA_HOME)/../include" -I"$(JAVA_HOME)/../include/win32"
LAPACK_LIBS = -L"$(R_HOME)/$(IMPDIR)" -lRlapack
## we only need this is if it is external, as otherwise link to R
LIBINTL=
LIBM = -lm
LIBR = -L"$(R_HOME)/$(IMPDIR)" -lR
LIBS =  -lm
## needed by R CMD config
LIBnn = lib
LIBTOOL =
LDFLAGS =
## needed to build applications linking to static libR
# MAIN_LD =
# MAIN_LDFLAGS =
# MAIN_LINK = $(MAIN_LD) $(MAIN_LDFLAGS) $(LDFLAGS)
# don't have this: would 'mkdir -p' do?
# MKINSTALLDIRS = $(R_HOME)/bin/mkinstalldirs
## There is no ObjC support in the recommended toolchain,
## but if a toolchain had support, this it what it would be.
OBJC = $(CC)
OBJCFLAGS = -O2
OBJC_LIBS = -lobjc
OBJCXX =
R_ARCH = /x64
RANLIB = $(BINPREF)ranlib
SAFE_FFLAGS = -O2 -ffloat-store
SED = sed
## it seems some makes (but not ours) get upset if SHELL is set.
# SHELL = /bin/sh
SHLIB_CFLAGS = 
SHLIB_CXXFLAGS = 
SHLIB_CXXLD = $(CXX)
## ideally -shared -static-libstdc++, but not for gcc 4.2.1
SHLIB_CXXLDFLAGS = -shared
SHLIB_CXX1XLD = $(CXX1X)
SHLIB_CXX1XLDFLAGS = -shared
SHLIB_EXT = .dll
SHLIB_FCLD = $(FC)
## ideally -shared -static-libgfortran, but not for gcc 4.2.1
SHLIB_FCLDFLAGS = -shared
SHLIB_FFLAGS = 
SHLIB_LD = $(CC)
SHLIB_LDFLAGS = -shared
SHLIB_LIBADD =
SHLIB_LINK = $(SHLIB_LD) -Wl,--export-all-symbols $(SHLIB_LDFLAGS) $(LDFLAGS)
SHLIB_OPENMP_CFLAGS = -fopenmp
SHLIB_OPENMP_CXXFLAGS = -fopenmp
SHLIB_OPENMP_FCFLAGS = -fopenmp
SHLIB_OPENMP_FFLAGS = -fopenmp
SHLIB_PTHREAD_FLAGS = -pthread
STRIP_LIBS = $(BINPREF)strip --strip-unneeded
STRIP_STATIC_LIBS = $(BINPREF)strip --strip-debug
TCLTK_CPPFLAGS = -I "$(TCL_HOME)/include" -DWin32
TCLTK_LIBS = -L"$(TCL_HOME)/bin64" -ltcl$(TCL_VERSION) -ltk$(TCL_VERSION)

STATIC_LIBR =

R_XTRA_CFLAGS =
R_XTRA_CPPFLAGS = -I"$(R_HOME)/include" -DNDEBUG
R_XTRA_CXXFLAGS = 
R_XTRA_FFLAGS = 

ALL_CFLAGS = $(R_XTRA_CFLAGS) $(PKG_CFLAGS) $(CPICFLAGS) $(SHLIB_CFLAGS) $(CFLAGS)
ALL_CPPFLAGS = $(R_XTRA_CPPFLAGS) $(PKG_CPPFLAGS) $(CPPFLAGS) $($*-CPPFLAGS) $(CLINK_CPPFLAGS) $(LOCAL_CPPFLAGS)
ALL_CXXFLAGS = $(R_XTRA_CXXFLAGS) $(PKG_CXXFLAGS) $(CXXPICFLAGS) $(SHLIB_CXXFLAGS) $(CXXFLAGS)
ALL_OBJCFLAGS = $(PKG_OBJCFLAGS) $(CPICFLAGS) $(SHLIB_CFLAGS) $(OBJCFLAGS)
ALL_OBJCXXFLAGS = $(PKG_OBJCXXFLAGS) $(CXXPICFLAGS) $(SHLIB_CXXFLAGS) $(OBJCXXFLAGS)
ALL_FFLAGS = $(R_XTRA_FFLAGS) $(PKG_FFLAGS) $(FPICFLAGS) $(SHLIB_FFLAGS) $(FFLAGS)
ALL_LIBS = $(PKG_LIBS) $(LOCAL_LIBS) $(SHLIB_LIBADD) $(LIBR) $(LIBINTL)

.SUFFIXES:
.SUFFIXES: .c .cc .cpp .d .f .f90 .f95 .m .mm .M .o .a .def .dll .exe .rc

.c.o:
    $(CC) $(ALL_CPPFLAGS) $(ALL_CFLAGS) -c $< -o $@
.c.d:
    @echo "making $@ from $<"
    @$(CC) -std=gnu99 -MM $(ALL_CPPFLAGS) $< > $@
.cc.o:
    $(CXX) $(ALL_CPPFLAGS) $(ALL_CXXFLAGS) -c $< -o $@
.cpp.o:
    $(CXX) $(ALL_CPPFLAGS) $(ALL_CXXFLAGS) -c $< -o $@
.cc.d:
    @echo "making $@ from $<"
    @$(CXX) -M $(ALL_CPPFLAGS) $< > $@
.cpp.d:
    @echo "making $@ from $<"
    @$(CXX) -M $(ALL_CPPFLAGS) $< > $@
.m.o:
    $(OBJC) $(ALL_CPPFLAGS) $(ALL_OBJCFLAGS) -c $< -o $@
.m.d:
    @echo "making $@ from $<"
    @$(OBJC) -MM $(ALL_CPPFLAGS) $< > $@
.mm.o:
    $(OBJCXX) $(ALL_CPPFLAGS) $(ALL_OBJCXXFLAGS) -c $< -o $@
.M.o:
    $(OBJCXX) $(ALL_CPPFLAGS) $(ALL_OBJCXXFLAGS) -c $< -o $@
.f.o:
    $(F77) $(ALL_FFLAGS) -c $< -o $@
.f95.o:
    $(FC) $(PKG_FCFLAGS) $(FCPICFLAGS) $(FCFLAGS) -c  $< -o $@
.f90.o:
    $(FC) $(PKG_FCFLAGS) $(FCPICFLAGS) $(FCFLAGS) -c  $< -o $@

%.exe:
    $(LINKER) $(LINKFLAGS) $($*-LINKFLAGS) -o $@ $^ $($*-LIBS) $(LOCAL_LIBS) $(LIBS)

%.dll:
    @echo EXPORTS > $*.def
    @$(NM) $^ | $(SED) -n $(SYMPAT) >> $*.def
    $(SHLIB_LD) -shared $(DLLFLAGS) -o $@ $*.def $^ $(ALL_LIBS)
    @$(RM) $*.def

lib%.dll.a: %.def
    $(DLLTOOL) $(DLLTOOLFLAGS) $($*-DLLTOOLFLAGS) --dllname $*.dll --input-def $*.def --output-lib $@

%.a:
    @$(RM) $@
    $(AR) crs $@ $^

%.o: %.rc
    $(RESCOMP) $(RESFLAGS) -i $< -o $@

5) I am just trying to reproduce the example in the GSL vignette

#include <RcppGSL.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>

extern "C" SEXP colNorm(SEXP sM) {

  try {

        RcppGSL::matrix<double> M = sM;     // create gsl data structures from SEXP
        int k = M.ncol();
        Rcpp::NumericVector n(k);           // to store results

        for (int j = 0; j < k; j++) {
            RcppGSL::vector_view<double> colview = gsl_matrix_column (M, j);
            n[j] = gsl_blas_dnrm2(colview);
        }
        M.free() ;
        return n;                           // return vector

  } catch( std::exception &ex ) {
        forward_exception_to_r( ex );

  } catch(...) {
        ::Rf_error( "c++ exception (unknown reason)" );
  }
  return R_NilValue; // -Wall
}

and I get this error

sourceCpp("gsltest.cpp")
g++ -m64 -I"C:/R/R-31~1.0/include" -DNDEBUG     -I"C:/R/R-3.1.0/library/Rcpp/include"  -I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall  -mtune=core2 -c gsltest.cpp -o gsltest.o gsltest.cpp:1:21: fatal error: RcppGSL.h: No such file or directory compilation terminated. make: *** [gsltest.o] Error 1 Warning message: running command 'make -f "C:/R/R-31~1.0/etc/x64/Makeconf" -f "C:/R/R-31~1.0/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="sourceCpp_15178.dll" WIN=64 TCLBIN=64 OBJECTS="gsltest.o"' had status 2  
Error in sourceCpp("gsltest.cpp") : 
  Error 1 occurred building shared library.

Thanks a lot..

msdu
  • 89
  • 8

3 Answers3

4

For Windows users with the same problem, you need to follow these steps:

  • Install R, making sure there are no spaces in the path;
  • Install Rtools;
  • Download file local300.zip as suggested by Dirk. And unzip it in the R directory;
  • Edit the path (e.g. following general instructions shown here http://www.computerhope.com/issues/ch000549.htm) to add the link the directory of local300 as LIB_GSL
  • Edit your Makevars.win, and you are ready to go

    PKG_CPPFLAGS=-I$(LIB_GSL)/include -I../inst/include
    PKG_LIBS=-L$(LIB_GSL)/lib/x64 -lgsl -lgslcblas 
    
Community
  • 1
  • 1
msdu
  • 89
  • 8
3

When you use sourceCpp, if the RcppGSL package does a good job of defining its inline plugin, you need to add to pull in headers, etc ...

// [[Rcpp::depends(RcppGSL)]]

The clue is in what sourceCpp gives you, this below is an extract of what you posted:

g++ -m64 -I"C:/R/R-31~1.0/include" -DNDEBUG     
  -I"C:/R/R-3.1.0/library/Rcpp/include"  
  -I"d:/RCompile/CRANpkg/extralibs64/local/include"    
  -O2 -Wall  -mtune=core2 -c gsltest.cpp -o gsltest.o 

gsltest.cpp:1:21: fatal error: RcppGSL.h: No such file or directory 
Romain Francois
  • 17,432
  • 3
  • 51
  • 77
1

If it is too hard to locally build GSL, note that you can also download the actual prebuilt GSL library which CRAN itself uses from this page -- you want the (large-ish) file local300.zip.

It should then should be a matter of adjusting the ennvironment variable LIB_GSL used here:

edd@max:~$ cat svn/rcpp/pkg/RcppGSL/src/Makevars.win 
## This assumes that the LIB_GSL variable points to working GSL libraries
PKG_CPPFLAGS=-I$(LIB_GSL)/include -I../inst/include
PKG_LIBS=-L$(LIB_GSL)/lib -lgsl -lgslcblas 

edd@max:~$ 

where I have removed the now-redundant invocation of Rscript for the Rcpp library we no longer need.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thanks for your quick answer. I included a dependency on RcppGSL as suggested by Romain. And I downloaded the local300 under R and modified the Makevars PKG_CPPFLAGS=-I$(LIB_GSL=C:/R/local300)/include -I../inst/include PKG_LIBS=-L$(LIB_GSL=C:/R/local300)/lib -lgsl -lgslcblas – msdu May 13 '14 at 20:39
  • and I now have this error gsl/gsl_vector.h: No such file or directory compilation terminated. I am afraid I need more hand holding.. – msdu May 13 '14 at 20:41
  • That means your environment variable, and the location of the local300.zip file content, do not agree. What you quoted about Makevars is wrong. Assign values first; then use them. What you have is not valid. – Dirk Eddelbuettel May 13 '14 at 22:39