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

时间:2018-10-21 16:22:58

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

问题背景

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

  • this answer之后编辑Makevars
  • 在升级后在linked discussion下编译C时,在之后运行xcode-select --install
  • 更改包含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:错误:没有匹配的函数可以调用“ 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"
    

1 个答案:

答案 0 :(得分:4)

因此,除了最初关于macOS上的编译器恶作剧(覆盖here)的问题之外,当您从{{ 1}}。

rescue

然后,我们可以运行:

std::string

此错误位于:Rcpp::StringVector

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