0

似乎 std::stringstream 不适用于 Rcpp。为了隔离这个问题,我写了一个最小的程序:

#include <string>
#include <sstream>
#include <Rcpp.h>

float atof(std::string a) {
        std::stringstream ss(a);
        Rf_PrintValue(Rcpp::wrap(a));
        float f;
        Rf_PrintValue(Rcpp::wrap(f));
        ss >> f;
        Rf_PrintValue(Rcpp::wrap(f));
        return (f);
}

RcppExport SEXP tsmall(SEXP sR) {
        std::string sC = Rcpp::as<std::string>(sR);
        return Rcpp::wrap(atof(sC));
}

tsmall应该只是将字符串转换为浮点数。Rf_PrintValue用于调试。现在在 OSX 10.16.7 上的 R 中,我得到

> dyn.load("min.so")
> a = .Call("tsmall","0.213245")
[1] "0.213245"
[1] 0
[1] 0
> a
[1] 0

在另一台机器(Ubuntu)上,它按预期工作:

> dyn.load("min.so")
> a = .Call("tsmall","0.213245")
[1] "0.213245"
[1] 1.401298e-45
[1] 0.213245
> a
[1] 0.213245

我在 OSX 上尝试了一个小型普通 C++ 程序,当然使用 stringstream 转换字符串和浮点数效果很好。

OSX 上使用的编译器是 MacPorts g++-mp-4.4。

更新: 我发现早先在 _GLIBCXX_DEBUG enabled 时,Stringstream 上的 stringstream 和 OSX 不使用双打的问题。但是,当我使用 /usr/bin/g++-4.2 中的默认 gcc-4.2 编译该问题中的测试程序时,我得到了错误,但使用 /opt/local/bin/g++-mp-4.4 编译可以正常工作。

但是,我已将 Rcpp 代码编译为

$ PKG_CPPFLAGS=`Rscript -e 'Rcpp:::CxxFlags()'` \
         PKG_LIBS=`Rscript -e 'Rcpp:::LdFlags()'` \
         R CMD SHLIB min.cpp

其中使用了 gcc-4.4:

/opt/local/bin/g++-mp-4.4 -I/opt/local/lib/R/include -I/opt/local/lib/R/include/x86_64 -I/opt/local/lib/R/library/Rcpp/include -I/opt/local/include    -fPIC  -pipe -O2 -m64 -c min.cpp -o min.o
/opt/local/bin/g++-mp-4.4 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/opt/local/lib -o min.so min.o /opt/local/lib/R/library/Rcpp/lib/x86_64/libRcpp.a -L/opt/local/lib/R/lib/x86_64 -lR

所以我不确定这是否是同一个问题。

更新 2:https://discussions.apple.com/thread/2166586?threadID=2166586&tstart=0的讨论之后,我在代码顶部添加了以下内容:

#ifdef GLIBCXXDEBUG
#define GLIBCXX_DEBUGDEFINED "1"
#else
#define GLIBCXX_DEBUGDEFINED "<undefined>"
#endif

并根据@Kerrek 的建议进行初始化f(尽管这不应该改变任何东西)float f=0;stof

Mac 上的输出还是一样的。

4

2 回答 2

1

使用常规的 Xcode 附带的编译器套件,这对我来说在 Lion 上工作得很好。

> require(inline)
Le chargement a nécessité le package : inline
> require(Rcpp)
Le chargement a nécessité le package : Rcpp
Le chargement a nécessité le package : int64
> 
> inc <- '
+ float atof(std::string a) {
+         std::stringstream ss(a);
+         Rf_PrintValue(Rcpp::wrap(a));
+         float f = 0. ;
+         Rf_PrintValue(Rcpp::wrap(f));
+         ss >> f;
+         Rf_PrintValue(Rcpp::wrap(f));
+         return (f);
+ }
+ '
> 
> fx <- cxxfunction( signature( sR = "character" ), '
+     std::string sC = as<std::string>(sR);
+     return wrap(atof(sC));
+ ', plugin = "Rcpp", includes = inc )
> fx( "1.2" )
[1] "1.2"
[1] 0
[1] 1.2
[1] 1.2

你的 R 也是用 gcc 4.4 编译的吗?

于 2011-12-14T12:15:36.683 回答
1

我不知道 R 或 RCPP,但我敢打赌以下代码会触发未定义的行为:

    float f;
    Rf_PrintValue(Rcpp::wrap(f));

在使用它之前你永远不会初始化 f,读取一个未初始化的变量是 UB。说些float f = 0;安全的话。

于 2011-12-12T19:46:58.067 回答