为什么这些 #include RcppArmadillo 会导致 R 在 Ubuntu 22.04 上使用 C++11 而不是 C++17 编译 Rcpp 代码?

问题描述 投票:0回答:1

我最近开始使用装有 Ubuntu 22.04 的机器,我发现我现有的(Windows)

.cpp
文件不再用
sourceCpp
进行编译。问题是我需要至少使用
C++14
进行编译,事实上,如果我显式设置标志
sourceCpp
,我可以使用
// [[Rcpp::plugins("cpp14")]]
成功编译文件。

我的问题是,当

C++
可用且似乎是简单程序中的默认设置时,为什么 R 选择使用较低版本的
C++17
?比较以下两个示例
.cpp
文件,它们似乎仅在包含
RcppArmadillo
库方面有所不同:

# test_stan.cpp
// [[Rcpp::depends(rstan)]]
#include <stan/math.hpp>
#include <iostream>
// [[Rcpp::export]]
int simple() {
    std::cout << "log normal(1 | 2, 3)="
    << stan::math::normal_log(1, 2, 5)
    << std::endl;
    return 0;
}
# test_stan_with_more_complicated...

// [[Rcpp::depends(rstan)]]
#include <stan/math.hpp>
#include <iostream>
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
// [[Rcpp::export]]
int simple() {
    std::cout << "log normal(1 | 2, 3)="
              << stan::math::normal_log(1, 2, 5)
              << std::endl;
    return 0;
}

运行

sourceCpp
分别导致对 C++ 编译器的以下调用:

使用C++编译器:‘g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0’ g++ -std=gnu++17 -I"/usr/share/R/include" -DNDEBUG -I"/usr/local/lib/R/site-library/Rcpp/include/" -I"/usr/local/ lib/R/site-library/RcppEigen/include/" -I"/usr/local/lib/R/site-library/RcppEigen/include/unsupported" -I"/usr/local/lib/R/site-library /BH/include" -I"/usr/local/lib/R/site-library/StanHeaders/include/src/" -I"/usr/local/lib/R/site-library/StanHeaders/include/" -我“/usr/local/lib/R/site-library/RcppParallel/include/”-I“/usr/local/lib/R/site-library/rstan/include”-DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -include '/usr/local/lib/R/site-library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp' -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/usr/local/lib/R/site-library/rstan/include" -I"/ home/rdpdp/Desktop/data/debug_gcc_version"-fpic -g -O2 -ffile-prefix-map=/build/r-base-H8urij/r-base-4.4.0=。 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -c test_stan.cpp -o test_stan.o

g++ -std=gnu++11 -I"/usr/share/R/include" -DNDEBUG
-I"/usr/local/lib/R/site-library/Rcpp/include/" -I"/usr/local/lib/R/site-library/RcppEigen/include/" -I"/usr/local/ lib/R/site-library/RcppEigen/include/unsupported"-I"/usr/local/lib/R/site-library/BH/include"-I"/usr/local/lib/R/site-library/ StanHeaders/include/src/" -I"/usr/local/lib/R/site-library/StanHeaders/include/" -I"/usr/local/lib/R/site-library/RcppParallel/include/" -我“/usr/local/lib/R/site-library/rstan/include”-DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DUSE_STANC3 -DSTRICT_R_HEADERS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -D_HAS_AUTO_PTR_ETC=0 -包括'/usr/local/lib/R/site -library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp' -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 -I../inst/include -fopenmp -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/usr/local/lib/R/site-库/rstan/include" -I"/usr/local/lib/R/site-library/RcppArmadillo/include" -I"/home/rdpdp/Desktop/data/debug_gcc_version" -fpic -g -O2 -ffile-prefix -map=/build/r-base-H8urij/r-base-4.4.0=。 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -c test_stan_with_more_complicated_includes.cpp -o test_stan_with_more_complicated_includes.o

除了添加

RcppArmadillo
fopenmp
标志之外,唯一的区别似乎是在 C++ 版本中:

diffchecker visualisation of sourceCpp output

我的机器有以下编译器:

 dpkg --list | grep compiler
ii  g++                                        4:11.2.0-1ubuntu1                       amd64        GNU C++ compiler
ii  g++-11                                     11.4.0-1ubuntu1~22.04                   amd64        GNU C++ compiler
ii  gcc                                        4:11.2.0-1ubuntu1                       amd64        GNU C compiler
ii  gcc-11                                     11.4.0-1ubuntu1~22.04                   amd64        GNU C compiler
ii  gfortran                                   4:11.2.0-1ubuntu1                       amd64        GNU Fortran 95 compiler
ii  gfortran-11                                11.4.0-1ubuntu1~22.04                   amd64        GNU Fortran compiler
ii  libllvm14:amd64                            1:14.0.0-1ubuntu1.1                     amd64        Modular compiler and toolchain technologies, runtime library
ii  libllvm15:amd64                            1:15.0.7-0ubuntu0.22.04.3               amd64        Modular compiler and toolchain technologies, runtime library
ii  libxkbcommon0:amd64                        1.4.0-1                                 amd64        library interface to the XKB compiler - shared library
ii  rpcsvc-proto                               1.4.2-0ubuntu6                          amd64        RPC protocol compiler and definitions

R

sessionInfo()
摘录为:

R version 4.4.0 (2024-04-24)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0


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

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5        cli_3.6.2          rlang_1.1.3        generics_0.1.3    
 [5] jsonlite_1.8.8     RcppParallel_5.1.7 glue_1.7.0         colorspace_2.1-0  
 [9] rstan_2.32.6       gridExtra_2.3      pkgbuild_1.4.4     stats4_4.4.0      
[13] scales_1.3.0       fansi_1.0.6        StanHeaders_2.32.7 grid_4.4.0        
[17] munsell_0.5.1      tibble_3.2.1       lifecycle_1.0.4    QuickJSR_1.1.3    
[21] inline_0.3.19      compiler_4.4.0     codetools_0.2-19   dplyr_1.1.4       
[25] pkgconfig_2.0.3    Rcpp_1.0.12        R6_2.5.1           tidyselect_1.2.1  
[29] utf8_1.2.4         parallel_4.4.0     pillar_1.9.0       magrittr_2.0.3    
[33] loo_2.7.0          tools_4.4.0        gtable_0.3.5       matrixStats_1.3.0 
[37] ggplot2_3.5.1 
c++ r c++11 rcpp
1个回答
0
投票

其原因在于

RcppArmadillo
配置其 C++ 编译设置的方式。带
RcppArmadillo
的默认版本是
gnu++11
RcppArmadillo
的默认设置由
Rcpp
应用。

如果您特别希望它使用另一个版本的 C++ 编译器,那么有几种方法可以做到这一点:

  1. 添加到环境 在运行 R 的环境中定义
    CXX_STD
    变量。
export CXX_STD=CXX14
  1. 在代码中指定 将另一个指令添加到
    .cpp
    文件中(只需将其放在
    // [[Rcpp::depends(RcppArmadillo)]]
    行之后)。
// [[Rcpp::plugins(cpp14)]]

我的机器上没有

gnu++17
,但上述两种方法都提供了
gnu++14
作为后一个
.cpp
文件的编译器。

© www.soinside.com 2019 - 2024. All rights reserved.