简体   繁体   English

R package 代码为 C,package 中“没有这样的符号” dll

[英]R package with C code, 'no such symbol' in package dll

I am writing an R package, and started to include C code in it.我正在写一个 R package,并开始在其中包含 C 代码。 Following instructions from here , under "getting started with.C()", I created a c function in src/, and an r wrapper to it, linking via the roxygen tag @useDynLib(<package-name>, <name_of_c_function>) .按照这里的说明,在“getting started with.C()”下,我在 src/ 中创建了一个 c function 和一个 r 包装器,通过 roxygen 标签链接@useDynLib(<package-name>, <name_of_c_function>) .

However, after running devtools::document() , I get the following error:但是,在运行devtools::document()之后,出现以下错误:

Error in FUN(X[[i]], ...) :
  no such symbol <name_of_c_function> in package C:/path/to/package/src/<package-name>.dll

I read that updating R and Rtools have fixed the issue for some.我读到更新 R 和 Rtools 已经解决了一些问题。 I have updated both of them yesterday, but to no avail.我昨天更新了他们两个,但无济于事。

Any help will be much appreciated.任何帮助都感激不尽。

(This is similar to the problem in this question , which is currently unanswered.) (这与this question中的问题类似,目前没有答案。)

(It may also be related with this question , except that I use devtools::document() instead of R CMD in that question.) (它也可能与这个问题有关,除了我在那个问题中使用 devtools::document() 而不是 R CMD 。)


Relevant code:相关代码:

# R file
#' @useDynLib <package-name> <name_of_c_function>
#' @export
name_of_func <- function(y) {
  stopifnot(is.numeric(y))
  .C(name_of_c_function, y,y,length(y),1) [[2]]
}
// C file
<#include stdlib.h>
static void name_of_c_function(double* y, double* x, 
const unsigned int length, const double a) {...}

It turns out that the problem is in this line原来问题出在这一行

static void name_of_c_function(...){...}

As mentioned in this post ,正如这篇文章中提到的,

The static keyword is somewhat over used. static 关键字有点被过度使用了。 When it applies to function, it means that the function has internal linkage, ie its scope is limited to within a translation unit (simply as a source file).当它应用于function时,意味着function具有内部链接,即它的scope被限制在一个翻译单元内(简单地作为一个源文件)。

In other words, the 'static' keyword makes the function no longer callable from outside its own unit, thus resulting in the "no such symbol" error.换句话说,“static”关键字使 function 不再可从其自身单元外部调用,从而导致“没有这样的符号”错误。

Removing the 'static' keyword solves the problem.删除“静态”关键字可以解决问题。

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

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