简体   繁体   English

在 Windows 上使用 Rtools 和 Rcpp 附带的不同 gcc 版本

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

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 (ie not necessarily for package authoring and certainly not for CRAN-valid packages)?在我开始更新 gcc 之前,有没有人实际尝试过这个,他们能否确认需要从源代码构建 R 来更新用于使用 Rcpp 编译 c++ 代码的 gcc 版本(即不一定用于包创作,当然也不适用于 CRAN-valid包)?

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 .请参阅 Dirk 对此问题的回答,以及来自原始海报How to use gcc 4.8.1 with Rcpp on Windows的后续评论。

Rebuilding R from source does not appear necessary.从源代码重建R似乎没有必要。 Here are the steps I used for a Windows 7 x64 system, running R 3.1.1 with Rtools 3.1.0.1942 .以下是我用于Windows 7 x64系统的步骤,运行R 3.1.1Rtools 3.1.0.1942 The implications of this update to gcc have not been thoroughly tested:此更新对gcc的影响尚未经过彻底测试:

  1. Start a clean R session and remove.packages("Rcpp") and anything else Rcpp related.启动一个干净的 R 会话并remove.packages("Rcpp")和任何其他Rcpp相关的东西。 Close R session.关闭 R 会话。
  2. Download and run MinGW-builds from Link to MinGW-builds installer .Link to MinGW-builds installer下载并运行 MinGW-builds。
  3. Select gcc version 4.8.1/Arch x64/Threads posix/Exception sjlj /Build rev 5 and install to [Drive]:\\Rtools\\mingw-builds\\ ...选择 gcc version 4.8.1/Arch x64/Threads posix/Exception sjlj /Build rev 5 并安装到 [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\\;更新系统PATH变量以按以下顺序包含这些条目(在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第三个路径条目替换Rtools安装程序中包含的Rtools[Drive]:\\Rtools\\gcc-4.6.3\\bin

  5. Restart, or otherwise, to reflect PATH changes.重新启动或以其他方式反映 PATH 更改。

  6. Start a clean R session and run install.packages("Rcpp") and repeat for all the other packages that were removed in step 1.启动一个干净的R会话并运行install.packages("Rcpp")并对步骤 1 中删除的所有其他包重复此操作。

These steps have been followed using R 3.1.1 (2014-07-10) with Rcpp 0.11.2 .使用R 3.1.1 (2014-07-10)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.使用rgui.exe最容易做到这rgui.exe ,而不是通过RStudio的 IDE,因为它会静默加载以前的工作区和后者的包。

Test set-up by running system('gcc -v') in a R session to obtain:通过在R会话中运行system('gcc -v')来测试设置以获得:

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 ):为了确认gcc 4.6.3 to 4.8.0中存在编译器错误的选择不再存在,并在运行Rcpp::sourceCppR会话中测试gcc 4.8.*可用的一些新的C++11功能gcc 4.8.*以下代码保存为.cpp文件,应该不会产生编译器警告或错误(而使用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_);
}

Here I describe how we can use GCC v8.1.0 with Rtools to compile source packages in R.在这里,我描述了我们如何使用GCC v8.1.0和 Rtools 来编译 R 中的源包。

This is tested on Windows 10 64 bit Home edition with R v3.5.0 and Rtools35 .这是在带有R v3.5.0Rtools35 的Windows 10 64 位家庭版上测试的

  1. Install R安装R
  2. Install Rtools .安装Rtools The default installation location will be C:\\Rtools .默认安装位置将是C:\\Rtools You will see following subfolders inside Rtools :您将在Rtools看到以下子文件夹:

  1. Set the path so that Rtools is in path by editing environment variable as below:通过编辑环境变量设置路径,使Rtools在路径中,如下所示:

  1. Delete whatever is present inside mingw_64 subfolder inside Rtools .删除mingw_64子文件夹中存在的任何Rtools
  2. Now go to download gcc from this MinGW distro .现在去从这个 MinGW 发行版下载 gcc。 Download mingw-16.0-without-git.exe .下载mingw-16.0-without-git.exe

Install in its default location.安装在其默认位置。 Note that by default, it will get installed in MinGW\\ folder.请注意,默认情况下,它将安装在MinGW\\文件夹中。 Copy/Cut whatever is inside this MinGW\\ folder.复制/剪切无论是这里面MinGW\\文件夹中。

  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:转到mingw_64子文件夹Rtools并粘贴在步骤5中任何被复制/粘贴切,你会看到这里面👇后mingw_64子文件夹:

  1. Test your setup to compile C/C++ code in R package rdatatable with following lines in R console:测试您的设置以在 R 包rdatatable编译 C/C++ 代码,在R控制台中使用以下几行:

     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 !Rcpp从源代码编译Rcpp

It can compile all R packages with compiled code, except those with dependencies on external libraries.它可以使用编译后的代码编译所有R packages ,但依赖于外部库的R packages除外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM