简体   繁体   中英

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. 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>) .

However, after running devtools::document() , I get the following error:

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. 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.)

(It may also be related with this question , except that I use devtools::document() instead of R CMD in that question.)


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. 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).

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.

Removing the 'static' keyword solves the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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