Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a different gcc version from that included with Rtools with Rcpp on Windows

Tags:

c++

windows

r

gcc

rcpp

Before I embark on updating gcc, has anyone actually attempted this, and can they confirm building R from source is required to update the gcc version one uses to compile c++ code with Rcpp (i.e. not necessarily for package authoring and certainly not for CRAN-valid packages)?

See Dirk's answer to this question, and the follow-on comment from the original poster How to use gcc 4.8.1 with Rcpp on Windows.

like image 717
Rusan Kax Avatar asked Aug 22 '14 21:08

Rusan Kax


2 Answers

Rebuilding R from source does not appear necessary. Here are the steps I used for a Windows 7 x64 system, running R 3.1.1 with Rtools 3.1.0.1942. The implications of this update to gcc have not been thoroughly tested:

  1. Start a clean R session and remove.packages("Rcpp") and anything else Rcpp related. Close R session.
  2. Download and run MinGW-builds from Link to MinGW-builds installer.
  3. Select gcc version 4.8.1/Arch x64/Threads posix/Exception sjlj /Build rev 5 and install to [Drive]:\Rtools\mingw-builds\ ...
  4. Update the system PATH variable to include these entries in the following order (at or near the top of PATH): [Drive]:\R\R-3.1.1\bin\x64;[Drive]:\Rtools\bin;[Drive]:\Rtools\mingw-build\x64-4.8.1-posix-sjlj-rev5\mingw64\bin\; the 3rd path entry replaces the one included by the Rtools installer: [Drive]:\Rtools\gcc-4.6.3\bin

  5. Restart, or otherwise, to reflect PATH changes.

  6. Start a clean R session and run install.packages("Rcpp") and repeat for all the other packages that were removed in step 1.

These steps have been followed using R 3.1.1 (2014-07-10) with Rcpp 0.11.2. It is easiest to do this using rgui.exe, and not via an IDE such as RStudio, due to the silent loading of previous workspaces and packages of the latter.

Test set-up by running system('gcc -v') in a R session to obtain:

COLLECT_GCC=F:\Rtools\MINGW-~1\X64-48~1.1-P\mingw64\bin\gcc.exe
COLLECT_LTO_WRAPPER=f:/rtools/mingw-~1/x64-48~1.1-p/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/4.8.1/lto-wrapper.exe
Target: x86_64-w64-mingw32
[Edited Config info]
Thread model: posix
gcc version 4.8.1 (rev5, Built by MinGW-W64 project) 

To confirm a selection of compiler bugs present with gcc 4.6.3 to 4.8.0 are no more, as well as test some new C++11 features available with gcc 4.8.*, in a R session running Rcpp::sourceCpp on the following code, saved as .cpp file, should generate no compiler warnings or errors (whereas this will totally fail using gcc 4.6.3):

#include <Rcpp.h>

// [[Rcpp::plugins("cpp11")]]


template<typename T>
struct Wrap 
{
    int test2(int depth)
    {
        m_test++;
        std::vector<int> v = { 0, 1, 2, 3 };
        return depth == 0? 1 : std::accumulate(
             v.begin(), v.end(), int(0), [=](int sub, int const&) {
             return sub + test2(depth - 1);
             });   
    }

    int m_test = 0;
};


  struct X
{
  template <class T> static void bar() {}

  template <class T> void foo(T p) 
  {
    [&] { bar<T>(); };
  }
};

// [[Rcpp::export]]
double inheriting(int in_){

struct A { 
  A(int u){
    hello = u*u/2.0;
  }; 
double hello;
};

struct B: A { using A::A; };

  B b(in_);
  return(b.hello);
}

// [[Rcpp::export]]
void test_lambda(int in_)
{
  X x;
  x.foo(in_);
}

// [[Rcpp::export]]
int test_bug_4_7_2(int in_){

Wrap<int> w;
return w.test2(in_);
}
like image 117
Rusan Kax Avatar answered Nov 03 '22 00:11

Rusan Kax


Here I describe how we can use GCC v8.1.0 with Rtools to compile source packages in R.

This is tested on Windows 10 64 bit Home edition with R v3.5.0 and Rtools35.

  1. Install R
  2. Install Rtools. The default installation location will be C:\Rtools. You will see following subfolders inside Rtools:

  1. Set the path so that Rtools is in path by editing environment variable as below:

  1. Delete whatever is present inside mingw_64 subfolder inside Rtools.
  2. Now go to download gcc from this MinGW distro. Download mingw-16.0-without-git.exe.

Install in its default location. Note that by default, it will get installed in MinGW\ folder. Copy/Cut whatever is inside this MinGW\ folder.

  1. Go to mingw_64 subfolder inside Rtools and paste whatever been copied/cut in step 5. After pasting you will see this 👇 inside mingw_64 sub-folder:

  1. Test your setup to compile C/C++ code in R package rdatatable with following lines in R console:

    remove.packages("data.table")
    install.packages("data.table", type = "source",
    repos = "http://Rdatatable.github.io/data.table")
    

If successful, you will find something like this:

It compiles Rcpp from source too !

It can compile all R packages with compiled code, except those with dependencies on external libraries.

like image 32
Suman Khanal Avatar answered Nov 03 '22 00:11

Suman Khanal