简体   繁体   English

stringstream在OSX 10.6上不与Rcpp一起使用

[英]stringstream not working with Rcpp on OSX 10.6

It seems std::stringstream doesn't work with Rcpp. 似乎std :: stringstream不适用于Rcpp。 To isolate the problem, I wrote a minimal program: 为了解决问题,我写了一个最小程序:

#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 should just convert a string to float. tsmall应该只是将一个字符串转换为float。 The Rf_PrintValue is for debugging. Rf_PrintValue用于调试。 Now in R on a OSX 10.16.7, I get 现在在OSX 10.16.7上的R中,我得到了

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

On another machine (Ubuntu), it works as expected: 在另一台机器(Ubuntu)上,它按预期工作:

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

I tried a small normal C++ program on the OSX, and of course it works fine to use stringstream to convert string and floats. 我在OSX上尝试了一个小的普通C ++程序,当然它可以正常使用stringstream来转换字符串和浮点数。

The compiler used on OSX is MacPorts g++-mp-4.4. OSX上使用的编译器是MacPorts g ++ - mp-4.4。

Update: I found an issue raised earlier about stringstream and OSX at Stringstream not working with doubles when _GLIBCXX_DEBUG enabled . 更新:我发现之前提出的一个问题是,在启用_GLIBCXX_DEBUG时Stringstream上的 stringstream和OSX 无法使用双精度 However, when I compile the test program in that issue with the default gcc-4.2 in /usr/bin/g++-4.2 I get the error, but compiling with /opt/local/bin/g++-mp-4.4 works fine. 但是,当我使用/usr/bin/g++-4.2中的默认gcc-4.2编译该问题中的测试程序时,我得到错误,但使用/opt/local/bin/g++-mp-4.4进行编译工作正常。

However, I had compiled the Rcpp code as 但是,我已将Rcpp代码编译为

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

which used gcc-4.4: 使用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

so I am not sure if this is the same issue. 所以我不确定这是否是同一个问题。

Update 2: Following the discussion at https://discussions.apple.com/thread/2166586?threadID=2166586&tstart=0 , I added the following to the top of my code: 更新2:https://discussions.apple.com/thread/2166586?threadID=2166586&tstart=0上讨论之后,我在代码的顶部添加了以下内容:

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

and also initialized f as float f=0; 并且还将f初始化为float f=0; in stof according to @Kerrek's suggestion (although this should not change anything). stof根据@ Kerrek的建议(虽然这不会改变任何东西)。

The output on the Mac is still the same. Mac上的输出仍然相同。

I don't know R or RCPP, but I bet the following code triggers undefined behaviour: 我不知道R或RCPP,但我敢打赌以下代码会触发未定义的行为:

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

You are never initializing f before using it, and reading an uninitialized variable is UB. 在使用它之前,你永远不会初始化 f ,读取未初始化的变量是UB。 Say something like float f = 0; 说像float f = 0; to be safe. 为了安全起见。

This works just fine for me on Lion with the regular Xcode shipped suite of compilers. 对于我来说,这对于我使用常规Xcode运行的编译器套件来说非常合适。

> 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

Was your R compiled with gcc 4.4 as well ? 你的R是用gcc 4.4编译的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM