[R-pkg-devel] R package does not find DLL routine

Lisa GM ||@@|nconnect|cut @end|ng |rom gm@||@com
Sun Jun 28 12:43:53 CEST 2020


I would like to create an R package that calls a DLL (in my case a .dylib)
which is already compiled. If I load and then call the DLL in a regular .R
file it works fine but when I try to add the DLL to the package it doesn't
work anymore. I'm using devtools and roxygen2 to update and compile the
package but it fails to add the routine from the DLL. If I generate the
NAMESPACE file of the package manually I get the same error, just later
when I try to run a function from my package that calls the DLL routine. To
better understand the issue, I created the following minimal working

I called the package "sum", and it has a sum_R.R that contains the
following code:

# R function to call sum_R.so
#' @useDynLib sum sum_c
#' @name sum
#' @export

sum_call <- function(p, n, Y){
  shared_lib <- system.file("libs/sum.dylib", package="sum", mustWork =
  #shared_lib <- "~/Desktop/mysum/inst/libs/sum.dylib"

  rtn <- .C("sum_c", as.integer(p), as.integer(n), as.double(Y), PACKAGE =

And a DLL called sum.dylib that comes from the c++ source code sum.cpp and
looks as follows:

#include <cstdio>
#include <ostream>
#include <iostream>

extern "C"
    void sum_c(int& p, int& n, double* array) {
        double res = 0;
        for (int i = 0; i < p * n; ++i) {
            res += array[i];
        std::cout << "result : " << res << std::endl;

and which I compile with:

g++-9 -Wall -O3 -shared sum.cpp -o sum.dylib

If I now add sum.dylib to my package and try to run devtools::document() to
update the package I get the following error:

Updating sum documentation

Loading sum

Error in .C("sum_c", as.integer(p), as.integer(n), as.double(Y)) (from
sum_fct.R#12) :

  "sum_c" not resolved from current namespace (sum)

Calls: document ... source_many -> source_one -> eval -> eval -> sum_call
-> .C

Execution halted

I'm using R 3.6. and a Mac Catalina but I get the same error message when
doing the above on a Linux machine.

Does anyone have an idea what might be causing this issue? I would be happy
to hear your suggestions.


	[[alternative HTML version deleted]]

More information about the R-package-devel mailing list