c++ - 无法使用 c++11 编译简单的 Rcpp 函数

标签 c++ r c++11 rcpp macos-mojave

问题背景

升级到 后我似乎无法编译一个使用 // [[Rcpp::plugins(cpp11)]] 的简单 Rcpp 函数。我试过:

  • 编辑Makevars关注this answer
  • 正在运行 xcode-select --install关注linked discussion关于在 下编译 C升级后。
  • 更改包括,string/string.h导致同样的错误

代码

我起草的函数是为了生成错误:

#include <Rcpp.h>
#include <string.h> // std::string, std::stod
using namespace Rcpp;

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

// [[Rcpp::export]]
NumericVector convertToDouble(Rcpp::StringVector x) {
    // Numeirc vector to store results
    NumericVector res;
    // Double for converted values
    double converted_double;
    for(Rcpp::StringVector::iterator it = x.begin(); it != x.end(); ++it) {
        // Get [] for vector element
        int index = std::distance(x.begin(), it);
        // Add results to vector
        converted_double = std::stod(x[index]);
        res.push_back(converted_double);
    }
    return res;
}


// Source / test

/*** R
Rcpp::sourceCpp(
    file = "sample_stod.cpp",
    embeddedR = FALSE,
    verbose = TRUE,
    rebuild = TRUE,
    cleanupCacheDir = TRUE,
    showOutput = TRUE)
convertToDouble(c("2.3", "34.25a", "abc32def.43", "12", "1.5"))
*/

错误

>> Rcpp::sourceCpp(
+     file = "sample_stod.cpp",
+     embeddedR = FALSE,
+     verbose = TRUE,
+     rebuild = TRUE,
+     cleanupCacheDir = TRUE,
+     showOutput = TRUE)

Generated extern "C" functions 
--------------------------------------------------------


#include <Rcpp.h>
// convertToDouble
NumericVector convertToDouble(Rcpp::StringVector x);
RcppExport SEXP sourceCpp_1_convertToDouble(SEXP xSEXP) {
BEGIN_RCPP
    Rcpp::RObject rcpp_result_gen;
    Rcpp::RNGScope rcpp_rngScope_gen;
    Rcpp::traits::input_parameter< Rcpp::StringVector >::type x(xSEXP);
    rcpp_result_gen = Rcpp::wrap(convertToDouble(x));
    return rcpp_result_gen;
END_RCPP
}

Generated R functions 
-------------------------------------------------------

`.sourceCpp_1_DLLInfo` <- dyn.load('/private/var/folders/7x/kwc1y_l96t55_rwlv35mg8xh0000gn/T/Rtmp2H7VYU/sourceCpp-x86_64-apple-darwin15.6.0-0.12.19/sourcecpp_295d2f3a47a3/sourceCpp_2.so')

convertToDouble <- Rcpp:::sourceCppFunction(function(x) {}, FALSE, `.sourceCpp_1_DLLInfo`, 'sourceCpp_1_convertToDouble')

rm(`.sourceCpp_1_DLLInfo`)

Building shared library
--------------------------------------------------------

DIR: /private/var/folders/7x/kwc1y_l96t55_rwlv35mg8xh0000gn/T/Rtmp2H7VYU/sourceCpp-x86_64-apple-darwin15.6.0-0.12.19/sourcecpp_295d2f3a47a3

/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB -o 'sourceCpp_2.so' --preclean  'sample_stod.cpp'  
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I"/Users/huski/Documents/R Projects/RcppConversion" -I/usr/local/include   -fPIC  -Wall -g -O2 -c sample_stod.cpp -o sample_stod.o
sample_stod.cpp:17:28: error: no matching function for call to 'stod'
        converted_double = std::stod(x[index]);
                           ^~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3910:30: note: candidate function not viable: no known conversion from 'Rcpp::Vector<16, PreserveStorage>::Proxy' (aka 'string_proxy<16, PreserveStorage>') to 'const std::__1::string' (aka 'const basic_string<char, char_traits<char>, allocator<char> >') for 1st argument
_LIBCPP_FUNC_VIS double      stod (const string& __str, size_t* __idx = 0);
                             ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3930:30: note: candidate function not viable: no known conversion from 'Rcpp::Vector<16, PreserveStorage>::Proxy' (aka 'string_proxy<16, PreserveStorage>') to 'const std::__1::wstring' (aka 'const basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >') for 1st argument
_LIBCPP_FUNC_VIS double      stod (const wstring& __str, size_t* __idx = 0);
                             ^
1 error generated.
make: *** [sample_stod.o] Error 1
Error in Rcpp::sourceCpp(file = "sample_stod.cpp", embeddedR = FALSE,  : 
  Error 1 occurred building shared library.
>> 

~/.R/Makevars

# Force use of compilers maintained by Homebrew

# Clang and clang++
CC=/usr/local/opt/llvm/bin/clang 
CXX=/usr/local/opt/llvm/bin/clang++
CXX1X=clang-omp++

尝试通过CC\CXX\CXX1X变量使用通过 Homebrew 安装的编译器,但确实会生成不同的错误消息。所有编译尝试均失败

sample_stod.cpp:17:28: error: no matching function for call to 'stod'


  • 更新:找不到stod enter image description here

  • g++ --version

     g++ --version
     Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
     Apple LLVM version 10.0.0 (clang-1000.11.45.2)
     Target: x86_64-apple-darwin18.0.0
     Thread model: posix
     InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
    
  • 当前~/.R/Makevars

     # Force use of compilers maintained by Homebrew
    
     # Clang and clang++
     # CC=/usr/local/opt/llvm/bin/clang 
     # CXX=/usr/local/opt/llvm/bin/clang++
     # CXX1X=clang-omp++
    
     # Fortran
     # FC=/usr/local/opt/gcc/bin/gfortran
     # F77=/usr/local/opt/gcc/bin/gfortran
    
     # CC=/usr/local/clang4/bin/clang
     # CXX=/usr/local/clang4/bin/clang++
     # LDFLAGS=-L/usr/local/clang4/lib
     # CPPFLAGS="-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
     CPPFLAGS="-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
    

最佳答案

因此,除了与 macOS 上的编译器恶作剧相关的初始问题(涵盖 here )之外,在从单个字符串转换为 std::string 时,您必须帮助编译器解决问题。在本例中,Rcpp::StringVector 中的值。

#include <Rcpp.h>

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

// [[Rcpp::export]]
Rcpp::NumericVector convertToDouble(Rcpp::StringVector x) {
    // Numeirc vector to store results
    std::vector<double> res;

    // Double for converted values
    double converted_double;
    for(Rcpp::StringVector::iterator it = x.begin(); it != x.end(); ++it) {
        // Get [] for vector element
        int index = std::distance(x.begin(), it);

        // Help the conversion to string
        std::string temp = Rcpp::as<std::string>(x[index]);

        // Convert
        converted_double = std::stod(temp);

        // Add to a std vector... Do not use with Rcpp types
        res.push_back(converted_double);
    }

    // Convert and return the Rcpp type as desired.
    return Rcpp::wrap(res);
}

然后,我们可以运行:

convertToDouble(c("2.3", "34.25a", "abc32def.43", "12", "1.5"))
# Error in convertToDouble(c("2.3", "34.25a", "abc32def.43", "12", "1.5")) : stod: no conversion

此错误位于:abc32def.43

在尝试转换之前,您可能需要对字符串进行额外的清理...

关于c++ - 无法使用 c++11 编译简单的 Rcpp 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52917411/

相关文章:

r - 将数据框设置为另一个数据框的列名

c++ - 寻求有关消耗多个值的 C++11 printf 格式参数的解释

c++ - 如何使用 Rapidjson 打印或 to_string() 具有未知结构的嵌套 JSON

regex - r 中的精确字符串匹配

r - R 中的二元运算符/中缀函数是通用的吗?以及如何利用?

C++11 "late binding"模板参数

c++ - cpp文件中的C++变量能否定义为特殊符号β

c++ - Libcurl得到响应 “415 Unsupported Media Type”但使用Curl.exe可以很好地工作

c++ - 如何解析 `no viable overloaded operator[] for type ' std::map<AudioTypes, const char *>`?

c++ - 继承makefile C++ undefined symbol 首先被引用