使用 Rcpp 将目标文件链接到函数的简化示例 [英] Simplified example using Rcpp to link an object file to a function

查看:53
本文介绍了使用 Rcpp 将目标文件链接到函数的简化示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现有的 C 代码由三个文件组成:头文件(.h"文件)、库文件(.o"文件)和源文件.它们目前在 UNIX 下运行,并在 Matlab 中作为编译的mex 文件"运行.我想使用 Rcpp 将它们移植到 R.它们都很长而且很复杂,所以我做了一个最小的例子来帮助我理解如何将它们移植到 R.

I have existing C code that consists of three files: a header file ( a ".h" file), a library file (a ".o" file), and a source file. They currently run under UNIX and as compiled "mex files" in Matlab. I would like to port them to R using Rcpp. They are all long and complex, so I made a minimal example to help me understand how to port them to R.

简化的头文件(my_header.h)是:

The simplified header file (my_header.h) is:

typedef unsigned int    ui4;
ui4  add_one( ui4 );

简化的库"文件(my_lib.cpp)是:

The simplified "library" file (my_lib.cpp) is:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "my_header.h"

ui4 add_one(ui4 x) {
        return(x+1);
}

简化的函数程序(my_program.cpp)为:

The simplified function program (my_program.cpp) is:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <Rcpp.h>
#include <cmath>

#include "my_header.h"

using namespace Rcpp;

// [[Rcpp::export]]
ui4 my_program_r () {
//int main (int argc, const char * argv[]) {
//
// As a MATLAB mex file, this function calls "main(..."
//
  ui4 value = add_one( (ui4)1 );
  printf( "%d", value );
  return value;
}

从终端(我在 Mac 上),我可以毫无错误地编译这些:

From the terminal (I'm on a Mac), I can compile these without an error:

$ g++ my_lib.cpp -c -o my_lib.o
$ g++ my_program.cpp -o my_program my_lib.o

当我尝试在 RStudio 中编译它们时,我得到:

When I try to compile them in RStudio, I get:

> library(Rcpp)
> sourceCpp( "my_program.cpp" )
Warning message:
In sourceCpp("my_program.cpp") :
  No Rcpp::export attributes or RCPP_MODULE declarations found in source
> 

为什么不能在 Rcpp 下编译?如何在sourceCpp"命令中指定链接文件(.o"库文件)?需要指定头文件吗?

Why isn't this compiling under Rcpp? How do I specify the linked file (the ".o" library file) in the "sourceCpp" command? Do I need to specify the header file?

推荐答案

sourceCpp 命令仅适用于单个文件.如果你有多个文件,你必须使用一个包:

The sourceCpp command is meant for single files only. If you have multiple files, you have to use a package:

  • 调用 Rcpp::Rcpp.package.skeleton(...) 来创建一个骨架包.
  • *.h*.c*.cpp 复制到src 文件夹.立>
  • 调用Rcpp::compileAtrributes().
  • 使用 R CMD build ...R CMD check ...R CMD INSTALL ... 来构建、检查和编译包.(check 会抱怨未记录的函数......)
  • Call Rcpp::Rcpp.package.skeleton(...) to create a skeleton package.
  • Copy *.h, *.c and *.cpp to the src folder.
  • Call Rcpp::compileAtrributes().
  • Use R CMD build ..., R CMD check ... and R CMD INSTALL ... to build, check and compile the package. (The check will complain about undocumented functions ...)

有关更多详细信息,请参阅 Rcpp-package 插图,例如这个问题.顺便说一句,由于 R 没有 unsigned int 类型,我不确定您的返回值是否有效.您可能必须切换到 intdouble.我也收到与您不同的错误消息:

For more details see Rcpp-package vignette and for example this question. BTW, since R does not have an unsigned int type, I am not sure if your return value will work. You might have to switch to an int or double. I also get a different error message than you:

dyn.load("/tmp/RtmpSbvXHx/sourceCpp-i686-pc-linux-gnu-0.12.17/sourcecpp_625ad24a943/sourceCpp_2.so") 中的错误:无法加载共享对象/tmp/RtmpSbvXHx/sourceCpp-i686-pc-linux-gnu-0.12.17/sourcecpp_625ad24a943/sourceCpp_2.so":/tmp/RtmpSbvXHx/sourceCpp-i686-pc-linux-gnu-0.12.17/sourcecpp_625ad24a943/sourceCpp_2.so:未定义符号:_Z7add_onej

Error in dyn.load("/tmp/RtmpSbvXHx/sourceCpp-i686-pc-linux-gnu-0.12.17/sourcecpp_625ad24a943/sourceCpp_2.so") : unable to load shared object '/tmp/RtmpSbvXHx/sourceCpp-i686-pc-linux-gnu-0.12.17/sourcecpp_625ad24a943/sourceCpp_2.so': /tmp/RtmpSbvXHx/sourceCpp-i686-pc-linux-gnu-0.12.17/sourcecpp_625ad24a943/sourceCpp_2.so: undefined symbol: _Z7add_onej

您确定上面的代码正是您使用的吗?

Are you sure the above code is exactly what you used?

这篇关于使用 Rcpp 将目标文件链接到函数的简化示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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