I'm just starting to experiment with the Rcpp package, so please be patient with me.
I read the Rcpp vignettes and I've tried a few given examples in the Rcpp documentation. Now, I thought I'd start by wrapping some functions from the GLFW libray, a library written in C.
So I thought I'd start with the function glfwInit
. I wrote this simple cpp source file in a package named pixel to wrap it under the name glfw_init
:
#include <Rcpp.h>
#include <GLFW/glfw3.h>
using namespace Rcpp;
//' @export
// [[Rcpp::export]]
int glfw_init() {
return(glfwInit());
}
I am working in RStudio, using the usual rinse and repeat cycle of generating documentation and building the package (I am using roxygen2).
The function is exported to R and the user space, so if I type glfw_init
in the R console I get:
function() {
.Call('_pixel_glfw_init', PACKAGE = 'pixel')
}
<bytecode: 0x5558ece6f398>
<environment: namespace:pixel>
But if I try to run it with glfw_init()
and get the error:
Error in .Call("_pixel_glfw_init", PACKAGE = "pixel") :
"_pixel_glfw_init" not available for .Call() for package "pixel"
I feel I'm missing some minor setup detail for this to work... Any help is appreciated!
P.S.: As per Konrad's comment I am posting here the output I am getting in RStudio's build pane:
==> 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: Here's my NAMESPACE:
# 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: After creating a new file R/pixel.R
:
#' @useDynLib pixel, .registration = TRUE
#' @importFrom Rcpp sourceCpp
NULL
I get now this error when I try to generate the documentation:
==> 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.
The new error indicates that you didn’t link the C++ code against the GLFW library. You need to provide a src/Makevars
file that adds the library to the PKG_LIBS
. See Using Makevars in the Writing R Extensions manual.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With