代码之家  ›  专栏  ›  技术社区  ›  thc

Rcpp代码可在Mac上编译,但不能在linux上编译

  •  3
  • thc  · 技术社区  · 8 年前

    我的C++代码是接收字符向量并将其转换为std::集。以下代码可在Mac上编译,但不能在Linux上编译:

    #include <Rcpp.h>
    using namespace Rcpp;
    
    // [[Rcpp::plugins(cpp11)]]
    // [[Rcpp::export]]
    void test(CharacterVector x) {
      std::set<std::string> vs(x.begin(), x.end());
    }
    

    Linux编译失败,原因是:

    /usr/include/c++/4.8.2/bits/stl_function.h:481:7: note:   no known conversion for argument 1 from ‘Rcpp::internal::string_proxy<16>’ to ‘const std::basic_string<char>&’
    

    Mac信息:

    R版本3.4.1(2017-06-30) 平台:x86\u 64-apple-darwin15.6.0(64位) 运行条件:macOS High Sierra 10.13.3 Rcpp\U 0.12.16

    Linux信息:

    R版本3.4.2(2017-09-28) 平台:x86\u 64-redhat-linux-gnu(64位) 运行环境:CentOS Linux 7(核心) Rcpp\U 0.12.16

    作为一种解决方法,我可以首先将字符向量转换为std::vector,然后将std::vector转换为std::set。然而,在我的测试中,这会导致大约5-10%的性能损失,这是我想要避免的。

    这是虫子吗?如何在linux上正确执行?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Dirk is no longer here    8 年前

    此变体适用于Ubuntu 17.10上的g++7.2:

    #include <Rcpp.h>
    using namespace Rcpp;
    
    // [[Rcpp::plugins(cpp11)]]
    // [[Rcpp::export]]
    void test(std::vector<std::string> x) {
      std::set<std::string> vs(x.begin(), x.end());
    }
    

    并运行(但不执行任何操作)

    R> Rcpp::sourceCpp("/tmp/soQ.cpp")
    R> test(rep("lalala", 3))
    R> 
    

    特征向量复杂;迭代器可能存在一些不太理想的情况。欢迎谨慎的PRs;与此同时,你有一个简单的选择。

    推荐文章