我不明白为什么我使用Rcpp(版本1.0.2)会出现此编译错误:
来自文件/home/rmagno/R/x86_64-pc-linux-gnu-library/3.6/Rcpp/include/Rcpp/internal/wrap.h
:
Line 523 static assertion failed: cannot convert type to SEXP
我正在尝试从GLFW库包装此C函数glfwSetKeyCallback。
我知道该错误是由于以下CPP源文件引起的,但我不明白我在做什么错:
#ifndef RCPP_GLFW_TYPES_H
#define RCPP_GLFW_TYPES_H
#include <Rcpp.h>
#include <GLFW/glfw3.h>
// https://stackoverflow.com/questions/41210595/s4-object-with-a-pointer-to-a-c-struct
typedef Rcpp::XPtr<GLFWwindow, Rcpp::PreserveStorage, glfwDestroyWindow> GLFWwindow_ptr;
#endif
#include "glfw_types.h"
using namespace Rcpp;
namespace {
std::unique_ptr<Rcpp::Function> key_callback_func_ptr;
}
void glfw_set_key_callback_wrapper(GLFWwindow* window, int key, int scancode, int action, int modes)
{
(*key_callback_func_ptr)(window, key, scancode, action, modes);
}
// [[Rcpp::export]]
void glfw_set_key_callback(GLFWwindow_ptr window, Rcpp::Function key_callback) {
key_callback_func_ptr = std::make_unique<Rcpp::Function>(key_callback);
glfwSetKeyCallback((GLFWwindow*)window, (GLFWkeyfun) glfw_set_key_callback_wrapper);
}
下面的编译函数glfw_destroy_window
并将其导出为glfwDestroyWindow
的源代码使用类型GLFWwindow_ptr
并从R编译并正常工作。
#include "glfw_types.h"
using namespace Rcpp;
//' @export
// [[Rcpp::export("glfwDestroyWindow")]]
void glfw_destroy_window(GLFWwindow_ptr window) {
glfwDestroyWindow((GLFWwindow*)window);
R_ClearExternalPtr(window);
}
此功能也可以正常工作:
#include "glfw_types.h"
using namespace Rcpp;
//' @export
// [[Rcpp::export("glfwCreateWindow")]]
GLFWwindow_ptr glfw_create_window(int width, int height, std::string title) {
const char *title_c = title.c_str();
return GLFWwindow_ptr(glfwCreateWindow(width, height, title_c, NULL, NULL), true);
}
这似乎在起作用...
#include "glfw_types.h"
using namespace Rcpp;
namespace {
std::unique_ptr<Rcpp::Function> key_callback_func_ptr;
}
void glfw_set_key_callback_wrapper(GLFWwindow* window, int key, int scancode, int action, int modes)
{
(*key_callback_func_ptr)(GLFWwindow_ptr(window, true), key, scancode, action, modes);
}
// [[Rcpp::export]]
void glfw_set_key_callback(GLFWwindow_ptr window, Rcpp::Function key_callback) {
key_callback_func_ptr = std::make_unique<Rcpp::Function>(key_callback);
glfwSetKeyCallback((GLFWwindow*)window, (GLFWkeyfun) glfw_set_key_callback_wrapper);
}
非常感谢您的帮助!
答案 0 :(得分:0)
#include "glfw_types.h"
using namespace Rcpp;
namespace {
std::unique_ptr<Rcpp::Function> key_callback_func_ptr;
}
void glfw_set_key_callback_wrapper(GLFWwindow* window, int key, int scancode, int action, int modes)
{
(*key_callback_func_ptr)(GLFWwindow_ptr(window, false), key, scancode, action, modes);
}
// [[Rcpp::export]]
void glfw_set_key_callback(GLFWwindow_ptr window, Rcpp::Function key_callback) {
key_callback_func_ptr = std::make_unique<Rcpp::Function>(key_callback);
glfwSetKeyCallback((GLFWwindow*)window, (GLFWkeyfun) glfw_set_key_callback_wrapper);
}