【问题标题】:Package Compilation fails when package name contain a "dot" and Rcpp function当包名称包含“点”和 Rcpp 函数时,包编译失败
【发布时间】:2013-12-21 15:57:52
【问题描述】:

我正在构建一个新包:

  • 包名包含一个点:例如my.package
  • 包导出一个Rcpp函数:例如rcppfunction

当我使用Rcmd INSTALL构建包时,在后台使用compileAttributes自动生成导出函数,

RcppExport SEXP my.package_rcppfunction(...)

由于导出名称中的点而导致编译错误:

RcppExports.cpp:10:19: error: expected initializer before '.' token

作为一种解决方法,我可以更改包名称以从中删除点,但我想要更好的解决方案并了解符号的导出方式。所以我的问题是:

  1. 例如,我如何参数化生成的代码以将点替换为“_”(可能通过为导出属性提供一些参数)。
  2. 或如何更改 g++ 调用以强制其编译此类带点符号..

我不知道这是否有帮助,但这里是我的 g++ 调用:

g++ -m32 -I"PATH_TO_R/R-30~1.2/include" -DNDEBUG    -
I"PATH_To_R/3.0/Rcpp/include" -
I"d:/RCompile/CRANpkg/extralibs64/local/include"     
-O2 -Wall  -mtune=core2 -c RcppExports.cpp -o RcppExports.o

【问题讨论】:

    标签: r g++ rcpp


    【解决方案1】:

    你不能这样做——在 C 或 C++ 函数名中根本不允许使用点:

    即为

    #include <stdlib.h>
    
    int foo.bar(int x) {
        return(2*x);
    }
    
    int main(void) {
        foo.bar(21);
        exit(0);
    }
    

    我们得到

    edd@max:/tmp$ gcc -c foo.c
    foo.c:4: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
    foo.c: In function ‘main’:
    foo.c:9: error: ‘foo’ undeclared (first use in this function)
    foo.c:9: error: (Each undeclared identifier is reported only once
    foo.c:9: error: for each function it appears in.)
    edd@max:/tmp$ 
    

    edd@max:/tmp$ g++ -c foo.c
    foo.c:4: error: expected initializer before ‘.’ token
    foo.c: In function ‘int main()’:
    foo.c:9: error: ‘foo’ was not declared in this scope
    edd@max:/tmp$ 
    

    在 C++ 中,foo.bar() 正在调用对象 foo 的成员函数 bar()

    【讨论】:

    • 谢谢德克。我将更改包名称。
    • 或者您可以保留名称,不使用 Rcpp 属性并手动编写您的函数(接口):) 开个玩笑——没有下划线的名称确实可以避免这里的问题。
    • :)。我猜你的意思是没有“点”而不是下划线。我认为compileAttributes 可以在生成名称之前进行一些检查。强化难吗?
    • 是的。一个点太虎头蛇尾了,我已经在心理上转换成了下划线:)
    • :) 但有时将它放在包名中是很自然的。
    猜你喜欢
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2015-04-27
    • 2012-11-09
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    相关资源
    最近更新 更多