在 Rcpp 中编译多个源文件 [英] Compiling multiple source files in Rcpp

查看:43
本文介绍了在 Rcpp 中编译多个源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下目录结构

my_func
    - my_func_r.cpp
    - my_func.c
    - my_func.h
    - my_func_test.c
    - matrix/
      - matrix.h
      - matrix.c

matrix 目录在matrix.h 中包含一些矩阵结构,在matrix.c 中包含一些初始化、自由、打印等功能.my_func.h 文件类似于

The matrix directory contains some matrix structures in matrix.h and some initialisation, free, print etc. functions in matrix.c. The my_func.h file is something like

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "matrix/matrix.h"

... some structures and templates ...

my_func.c 文件就是

#include "my_func.h"

... helper functions ...

int my_func(...) {
    ... my_func stuff ...     
    return 0;
}

my_func_test.c 类似于

#include "my_func.h"

int main() {
    ... some test ...
    return 0;
}

使用 gcc/g++ 我可以很好地运行

With gcc/g++ I can run this fine with

gcc my_func_test.c my_func.c matrix/matrix.c -o test -lm

最终文件my_func_r.cppRcpp 结构和my_func.c 中使用的结构之间的接口.目前是这样的

The final file my_func_r.cpp is an interface between the Rcpp structures and the structures used in my_func.c. It is currently something like

#include "my_func.h"
#include <Rcpp.h>

// [[Rcpp::export]]
int my_func_r(Rcpp::List x, ...) {
    ... convert inputs to structure recognised by my_func.h ...       
    ... run my_func.c ...
    ... put returned objects back into one of the R structure ...

    return 0;
}

我的问题是如果我现在运行

The problem I have is if I now run

sourceCpp('my_func_r.cpp', verbose=TRUE, rebuild=TRUE)

它抱怨位于 matrix/matrix.c 中的函数缺少符号.解决方法是简单地将 my_funcmatrix 文件中的所有标头和源代码粘贴到 my_func_r.cpp 顶部.

It complains about missing symbols for functions located in matrix/matrix.c. A workaround is to simply paste all my header and source code from both the my_func and matrix files at the top of my_func_r.cpp.

然而,这感觉是一个非常不令人满意的解决方案,尤其是对于代码维护.完成我想要做的事情的最简单方法是什么?

This however feels a very unsatisfactory solution especially for code maintenance. What is the easiest way to accomplish what I am trying to do?

推荐答案

快速解决方案:

  1. 这对于 Rcpp 本身来说并不是很特别
  2. 您只是在为 R 构建中更高级/更复杂的 src/ 目录而苦苦挣扎.
  3. Writing R Extensions 中有关于此的官方文档,问题之前已经在 SO 上出现过.
  4. 可以首先在子目录中编译一个 libmatrix.a 并链接到该目录.这可以通过一个简单的 src/Makevars 来实现,但 仍然不鼓励.所以请继续阅读.
  5. 但这是自己造成的伤口.只需将matrix.hmatrix.c复制到src/中,调整包含路径即可.
  6. 一如既往:创建一个包.不要在较大的设置中使用 sourceCpp().它不是为此而制作的,
  1. This is not really particular to Rcpp per se
  2. You are simply struggling with a more advanced / complicated src/ directory in an R build.
  3. There is official documentation about this in Writing R Extensions, and the questions has come up here on SO before.
  4. You could compile a libmatrix.a first in the subdirectory and link to that. This is doable via a simple src/Makevars but still discouraged. So read on.
  5. But this is a self-inflicted wound. Just copy matrix.h and matrix.c into src/, adjust the include path, and you are done.
  6. As always: Create a package. Don't use sourceCpp() on larger setup. It is not made for that,

这篇关于在 Rcpp 中编译多个源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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