Error: function is not available for .Call() for package X

Error: function is not available for .Call() for package X

我刚开始试用 Rcpp 包,所以请耐心等待。

我阅读了 Rcpp 小插图,并尝试了 Rcpp 文档中的一些给定示例。现在,我想我应该从包装 GLFW 库中的一些函数开始,这是一个用 C 编写的库。

所以我想我应该从函数 glfwInit 开始。我将这个简单的 cpp 源文件写在一个名为 pixel 的包中,并将其包装在名称 glfw_init:

#include <Rcpp.h>
#include <GLFW/glfw3.h>
using namespace Rcpp;


//' @export
// [[Rcpp::export]]
int glfw_init() {

  return(glfwInit());
}

我在 RStudio 中工作,使用通常的冲洗和重复生成文档和构建包的循环(我使用的是 roxygen2)。

该函数导出到 R 和用户 space,所以如果我在 R 控制台中键入 glfw_init,我会得到:

function() {
    .Call('_pixel_glfw_init', PACKAGE = 'pixel')
}
<bytecode: 0x5558ece6f398>
<environment: namespace:pixel>

但是如果我尝试用 glfw_init() 运行 它并得到错误:

Error in .Call("_pixel_glfw_init", PACKAGE = "pixel") : 
  "_pixel_glfw_init" not available for .Call() for package "pixel"

我觉得我缺少一些小的设置细节才能正常工作...感谢任何帮助!

P.S.: 根据 Konrad 的评论,我在这里发布我在 RStudio 的构建窗格中获得的输出:

==> Rcpp::compileAttributes()

* Updated R/RcppExports.R

==> R CMD INSTALL --no-multiarch --with-keep.source pixel

g++ -std=gnu++11 -I"/usr/include/R/" -DNDEBUG  -I"/home/rmagno/R/x86_64-pc-linux-gnu-library/3.6/Rcpp/include" -D_FORTIFY_SOURCE=2  -fpic  -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fno-plt  -c glfw.cpp -o glfw.o
* installing to library ‘/home/rmagno/R/x86_64-pc-linux-gnu-library/3.6’
* installing *source* package ‘pixel’ ...
** using staged installation
** libs
g++ -std=gnu++11 -shared -L/usr/lib64/R/lib -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -o pixel.so RcppExports.o glfw.o -L/usr/lib64/R/lib -lR
installing to /home/rmagno/R/x86_64-pc-linux-gnu-library/3.6/00LOCK-pixel/00new/pixel/libs
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (pixel)

P.S.2:这是我的命名空间:

# Generated by roxygen2: do not edit by hand

export(coords_rectangle)
export(coords_rectangular_lattice)
export(coords_segment)
export(coords_square)
export(display_matrix_int)
export(glfw_init)
export(lgl_matrix_to_coords_grid_segment)
export(lgl_matrix_to_coords_segment)
export(palette_for_quads)

P.S.3: 创建新文件后R/pixel.R:

#' @useDynLib pixel, .registration = TRUE
#' @importFrom Rcpp sourceCpp
NULL

我在尝试生成文档时遇到此错误:

==> Rcpp::compileAttributes()

* Updated R/RcppExports.R

==> devtools::document(roclets = c('rd', 'collate', 'namespace'))

Updating pixel documentation
Loading pixel
Error in dyn.load(dllfile) : 
  unable to load shared object '/home/rmagno/sci/code/R/pkg/pixel/src/pixel.so':
  /home/rmagno/sci/code/R/pkg/pixel/src/pixel.so: undefined symbol: glfwInit
Calls: suppressPackageStartupMessages ... <Anonymous> -> load_dll -> library.dynam2 -> dyn.load
Execution halted

Exited with status 1.

新错误表明您没有link针对 GLFW 库的 C++ 代码。您需要提供将库添加到 PKG_LIBSsrc/Makevars 文件。请参阅 编写 R 扩展 手册中的 Using Makevars