0

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) {...}
bert
  • 332
  • 1
  • 9
  • Did this C file actually compile? – user253751 Aug 19 '20 at 15:58
  • @user253751 I'm not sure how to check that... I have an .o file with the same name as the .c file --- does that mean it compiled? – bert Aug 19 '20 at 15:59
  • That is one of _two_ steps. The package also needs to produce a `.so` (on Linux) with the name of the package. Your linking error prevents that. You most likely lack linking instruction to an external resource (maybe `simplexproj_Condat` ?) you reference but apparently not supply. Hence "no symbol". – Dirk Eddelbuettel Aug 19 '20 at 16:02
  • @DirkEddelbuettel is there an `.so` equivalent on Windows? I don't completely understand what you mean by a linking instruction, could you elaborate? – bert Aug 19 '20 at 16:08
  • You probably want to take a look at the _Writing R Extensions_ manual that came with your copy of R. – Dirk Eddelbuettel Aug 19 '20 at 16:15
  • Well how did you make the .o file? – user253751 Aug 19 '20 at 16:27
  • Thanks @DirkEddelbuettel. I thought the instructions in [_R packages_](http://r-pkgs.had.co.nz/src.html) would be sufficient. Guess I have to dig in a lot more :x – bert Aug 20 '20 at 03:44
  • @user253751 After saving the .c file, I ran `devtools::document()`. The .o and .dll files then magically appeared in the src/ directory ;) – bert Aug 20 '20 at 03:46
  • @DirkEddelbuettel, replying to your earlier comment, there is actually a `.dll` file in src/ (Windows equivalent of `.so`). Now I don't know what is causing the error message, any idea? – bert Aug 20 '20 at 08:25
  • found the problem. I should not have used a __static__ function. Thanks both DirkEddelbuettel and user253751 for your time :D – bert Aug 20 '20 at 09:34

1 Answers1

0

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.

bert
  • 332
  • 1
  • 9