Rcpp:从 Rcpp 中的包调用 C 函数 [英] Rcpp: Call C function from a package within Rcpp

查看:54
本文介绍了Rcpp:从 Rcpp 中的包调用 C 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Rcpp 编写一个 C++ 函数,该函数使用包 hypred 中的 C 函数,该包位于 CRAN 这里.

I would like to write a C++ function with Rcpp that uses a C function found in the package hypred, which is on CRAN here.

我阅读了在 Rcpp 中使用其他包中的 C 函数,但不明白这是否适用于我,如果适用,该怎么做.

I read using C function from other package in Rcpp, but don't understand if this applies for me and if it would apply, what to do.

所需的函数位于源文件 /src/hypredRoutines.c 中,名为 meiosisFUNAllChr.

The desired function is in the the source file /src/hypredRoutines.c and is called meiosisFUNAllChr.

到目前为止我根据这个 SO 问题做了什么这里 是:

What I so far did based on this SO question here is:

  1. 我将函数 meiosisFUNAllChr 与其余代码分开,并将其放在一个名为 meiosisFUNAllChr.c 的新文件中.
  2. 我创建了一个名为 meiosisFUNAllChr.h 的头文件,其中包含:

  1. I separated the function meiosisFUNAllChr from the rest of the code and placed it in a new file called meiosisFUNAllChr.c.
  2. I created a header file called meiosisFUNAllChr.h containing:

#ifndef MEIOSISFUNALLCHR_H
#define MEIOSISFUNALLCHR_H
void meiosisFUNallChr (...);
#endif 

  • 编译它

  • Compiled it with

    gcc -c -o meiosisFUNAllChr.o meiosisFUNAllChr.c
    

  • 创建了一个虚拟的 c++ 函数

  • Created a dummy c++ function

    #include <Rcpp.h>
    using namespace Rcpp;
    
    extern "C" {
     #include "meiosisFUNallChr.h"
    }
    
    // [[Rcpp::export]]
    int timesTwo(int x) {
     return x * 2;
    }
    

  • 此时用sourceCpp编译是不行的.你能告诉我如何让它工作吗?

    At this point, compiling with sourceCpp does not work. Can you show me how to get it working?

    非常感谢!

    编辑

    使用 sourceCpp 编译给了我:

    Compiling with sourceCpp gives me:

    meiosisFUNallChr.h: file or directory not found
    

    推荐答案

    hypred 注册了它的两个函数,在 R 级别你可以这样弄一个你想要的:

    hypred registers its two functions, at the R level you can get hold of the one you want like this:

    xp <- getDLLRegisteredRoutines( getLoadedDLLs()[["hypred"]] )[[".C"]][["meiosisFUNallChr"]]$address
    

    我相信你可以像这样在 C++ 中访问函数指针:

    I believe you can then access the function pointer in C++ like this:

    DL_FUNC meiosisFUNallChr = reinterpret_cast<DL_FUNC>( R_ExternalPtrAddr(xp) ) ;
    

    但是您最好与软件包作者协商他​​们实施 这个方案 如果你能证明你真的需要直接调用 C 函数.

    But you'd be better off negotiating with the package authors that they implement this scheme if you can make a case that you really need to be calling the C function directly.

    这篇关于Rcpp:从 Rcpp 中的包调用 C 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆