Search code examples
rgccc++14rstan

rstan C++14 error while installing (centos)


while installing rstan getting following error:

Error in .shlib_internal(args) :
C++14 standard requested but CXX14 is not defined

from research got to know that C++14 compiler should be available. How to install the same while configuring R. Using the below command to configure R

./configure --with-readline=no --with-x=no

and installing

yum install -y devtoolset-6

but still not able to update C++14 and rstan gives the error

Default C++ compiler:      g++   -g -O2
C++98 compiler:            g++  -g -O2
C++11 compiler:            g++ -std=gnu++11 -g -O2
C++14 compiler:            g++   -g -O2  
C++17 compiler:              
Fortran 90/95 compiler:    gfortran -g -O2
Obj-C compiler: 

setup.sh

 yum -y update
 yum install -y centos-release-scl
 yum install -y devtoolset-6
 yum install -y devtoolset-6-gcc-gfortran
 scl enable devtoolset-6 bash
 scl enable devtoolset-6-gcc-gfortran bash
 mkdir packages
 cd packages
 wget -qO- 
 https://downloads.sourceforge.net/project/libpng/zlib/1.2.8/zlib- 
 1.2.8.tar.gz | tar zvx
 cd zlib-1.2.8
 ./configure
 make
 make install
 cd ..
 wget -qO- http://downloads.sourceforge.net/pcre/pcre-8.35.tar.gz | 
 tar xzv
 cd pcre-8.35
 ./configure
 make
 make install
 cd ..
 wget -qO- http://tukaani.org/xz/xz-5.2.2.tar.gz | tar xzv
 cd xz-5.2.2
 ./configure
 make
 make install
 cd ..
 wget -qO- https://curl.haxx.se/download/curl-7.47.1.tar.gz | tar xzv
 cd curl-7.47.1
 ./configure
 make
 make install
 cd ..


 wget -qO- https://cran.r-project.org/src/base/R-3/R-3.4.4.tar.gz | 
 tar xzv
 cd R-3.4.4
 ./configure --with-readline=no --with-x=no --prefix=/packages/R-3.4.4 
 F77=gfortran
 make
 make install

Solution

  • I also got this problem, here I record how to solve it.

    The key point is installing proper g++ and configure it.

    Firstly, install g++ Version >= 5 as https://github.com/stan-dev/rstan/wiki/Installing-RStan-on-Linux said:

    Using RStan requires either g++ version 4.9 and up

    Here I am installing g++8 (You can change the version as your wish):

    Run

    $ sudo yum install centos-release-scl
    $ sudo yum install devtoolset-8-gcc*
    

    Now you have an alternative g++ along with default g++ in your OS.

    You can enable this and check the version:

    $ scl enable devtoolset-8 bash
    $ gcc --version
    $ g++ --version
    

    Find its location:

    $ which g++
    /opt/rh/devtoolset-8/root/usr/bin/g++
    

    Next, you need to configure ~/.R/Makevars, put the following content to it either using vim (or other editors):

    CXX14FLAGS=-O3 -march=native -mtune=native -fPIC
    CXX14=/opt/rh/devtoolset-8/root/usr/bin/g++
    

    Or using R commands:

    dotR <- file.path(Sys.getenv("HOME"), ".R")
    if (!file.exists(dotR)) dir.create(dotR)
    M <- file.path(dotR, "Makevars")
    if (!file.exists(M)) file.create(M)
    cat("\nCXX14FLAGS=-O3 -march=native -mtune=native -fPIC",
        "CXX14=/opt/rh/devtoolset-8/root/usr/bin/g++", # or clang++ but you may need a version postfix
        file = M, sep = "\n", append = TRUE)
    

    NOTE: these R commands are copied from https://github.com/stan-dev/rstan/wiki/Configuring-C-Toolchain-for-Linux but CXX14 flag is modified according to the location above.

    Now, you can install rstan package now:

    install.packages("rstan")
    

    Hope this helps.


    PS: R users can use this approach to modify the compiler flags in a similar way.